summaryrefslogtreecommitdiff
path: root/scripts/keepalived/VyattaKeepalived.pm
diff options
context:
space:
mode:
authorStig Thormodsrud <stig@uffda.(none)>2007-11-04 11:17:22 -0800
committerStig Thormodsrud <stig@uffda.(none)>2007-11-04 11:17:22 -0800
commitda563fa3127254122f6c4c6e86b852fdb3ca5180 (patch)
tree39e1c64a9e44c9f0d00d106c8db26875313142bf /scripts/keepalived/VyattaKeepalived.pm
parent604a1aaeee3cd3386febf5099936359b3647d56d (diff)
downloadvyatta-cfg-quagga-da563fa3127254122f6c4c6e86b852fdb3ca5180.tar.gz
vyatta-cfg-quagga-da563fa3127254122f6c4c6e86b852fdb3ca5180.zip
Add vrrp to eureka.
Diffstat (limited to 'scripts/keepalived/VyattaKeepalived.pm')
-rwxr-xr-xscripts/keepalived/VyattaKeepalived.pm181
1 files changed, 181 insertions, 0 deletions
diff --git a/scripts/keepalived/VyattaKeepalived.pm b/scripts/keepalived/VyattaKeepalived.pm
new file mode 100755
index 00000000..c8d732d1
--- /dev/null
+++ b/scripts/keepalived/VyattaKeepalived.pm
@@ -0,0 +1,181 @@
+#!/usr/bin/perl
+#
+# Module: VyattaKeepalived.pm
+#
+# **** License ****
+# Version: VPL 1.0
+#
+# The contents of this file are subject to the Vyatta Public License
+# Version 1.0 ("License"); you may not use this file except in
+# compliance with the License. You may obtain a copy of the License at
+# http://www.vyatta.com/vpl
+#
+# Software distributed under the License is distributed on an "AS IS"
+# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+# the License for the specific language governing rights and limitations
+# under the License.
+#
+# This code was originally developed by Vyatta, Inc.
+# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
+# All Rights Reserved.
+#
+# Author: Stig Thormodsrud
+# Date: October 2007
+# Description: Common keepalived definitions/funcitions
+#
+# **** End License ****
+#
+package VyattaKeepalived;
+
+use VyattaConfig;
+use POSIX;
+
+use strict;
+use warnings;
+
+my $daemon = '/usr/sbin/keepalived';
+my $keepalived_conf = '/etc/keepalived/keepalived.conf';
+my $sbin_dir = '/opt/vyatta/sbin';
+my $state_transition = "$sbin_dir/vyatta-vrrp-state.pl";
+my $keepalived_pid = '/var/run/keepalived_vrrp.pid';
+my $state_dir = '/var/log/vrrpd';
+my $vrrp_log = "$state_dir/vrrp.log";
+
+
+sub vrrp_log {
+ my $timestamp = strftime("%Y%m%d-%H:%M.%S", localtime);
+ open my $fh, ">>", $vrrp_log;
+ print $fh "$timestamp: ", @_ , "\n";
+ close $fh;
+}
+
+sub is_running {
+ if (-f $keepalived_pid) {
+ my $pid = `cat $keepalived_pid`;
+ chomp $pid;
+ my $ps = `ps -p $pid -o comm=`;
+
+ if (defined($ps) && $ps ne "") {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+sub start_daemon {
+ my ($conf) = @_;
+
+ my $cmd = "$daemon --vrrp --log-facility 7 --log-detail --dump-conf --use-file $conf";
+ system($cmd);
+ vrrp_log("start_daemon");
+}
+
+sub stop_daemon {
+ if (is_running()) {
+ my $pid = `cat $keepalived_pid`;
+ system("kill $pid");
+ vrrp_log("stop_daemon");
+ } else {
+ vrrp_log("stop daemon called while not running");
+ }
+}
+
+sub restart_daemon {
+ my ($conf) = @_;
+
+ if (VyattaKeepalived::is_running()) {
+ my $pid = `cat $keepalived_pid`;
+ chomp $pid;
+ system("kill -1 $pid");
+ vrrp_log("restart_deamon");
+ } else {
+ start_daemon($conf);
+ }
+}
+
+sub get_conf_file {
+ return $keepalived_conf;
+}
+
+sub get_state_script {
+ return $state_transition;
+}
+
+sub get_state_file {
+ my ($vrrp_intf, $vrrp_group) = @_;
+
+ my $file = "$state_dir/vrrpd_" . "$vrrp_intf" . "_" . "$vrrp_group.state";
+ return $file;
+}
+
+sub get_master_file {
+ my ($vrrp_intf, $vrrp_group) = @_;
+
+ my $file = "$state_dir/vrrpd_" . "$vrrp_intf" . "_" . "$vrrp_group.master";
+ return $file;
+}
+
+sub get_state_files {
+ my $intf = shift;
+
+ # todo: fix sorting for ethX > 9
+ my @state_files;
+ open(my $LS, "ls $state_dir |grep '^vrrpd_$intf.*\.state\$' | sort |");
+ @state_files = <$LS>;
+ close($LS);
+ foreach my $i (0 .. $#state_files) {
+ $state_files[$i] = "$state_dir/$state_files[$i]";
+ }
+ chomp @state_files;
+ return @state_files;
+}
+
+sub vrrp_get_config {
+ my ($intf, $group) = @_;
+
+ my $config = new VyattaConfig;
+
+ $config->setLevel("interfaces ethernet $intf");
+ my $primary_addr = $config->returnOrigValue("address");
+ if ($primary_addr =~ m/(\d+\.\d+\.\d+\.\d+)\/\d+/) {
+ $primary_addr = $1;
+ }
+
+ $config->setLevel("interfaces ethernet $intf vrrp");
+ my $vip = $config->returnOrigValue("virtual-address");
+ my $priority = $config->returnOrigValue("priority");
+ if (!defined $priority) {
+ $priority = 1;
+ }
+ my $preempt = $config->returnOrigValue("preempt");
+ if (!defined $preempt) {
+ $preempt = "true";
+ }
+ my $advert_int = $config->returnOrigValue("advertise-interval");
+ if (!defined $advert_int) {
+ $advert_int = 1;
+ }
+ $config->setLevel("interfaces ethernet $intf vrrp authentication");
+ my $auth_type = $config->returnOrigValue("type");
+ if (!defined $auth_type) {
+ $auth_type = "none";
+ } else {
+ $auth_type = uc($auth_type);
+ }
+ return ($primary_addr, $vip, $priority, $preempt, $advert_int, $auth_type);
+}
+
+sub vrrp_state_parse {
+ my ($file) = @_;
+
+ if ( -f $file) {
+ my $line = `cat $file`;
+ chomp $line;
+ my ($start_time, $intf, $group, $state, $ltime) = split(' ', $line);
+ return ($start_time, $intf, $group, $state, $ltime);
+ } else {
+ return undef;
+ }
+}
+
+#end of file