diff options
author | Stig Thormodsrud <stig@vyatta.com> | 2008-03-31 16:11:47 -0700 |
---|---|---|
committer | Stig Thormodsrud <stig@vyatta.com> | 2008-03-31 16:11:47 -0700 |
commit | 388d5ed2580bc9ef7ee7a8cdc1d75126ea97a53e (patch) | |
tree | 643c914b62c93cc37ae70a7e2e652b24fb006246 /scripts/vyatta-watchlink-exclude.pl | |
parent | 76aae0216871f8c4f3b1c2ebad6761e67ee3e799 (diff) | |
download | vyatta-cfg-388d5ed2580bc9ef7ee7a8cdc1d75126ea97a53e.tar.gz vyatta-cfg-388d5ed2580bc9ef7ee7a8cdc1d75126ea97a53e.zip |
Add utilitly script to add/removed entries to watchlink exclude file.
Diffstat (limited to 'scripts/vyatta-watchlink-exclude.pl')
-rwxr-xr-x | scripts/vyatta-watchlink-exclude.pl | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/scripts/vyatta-watchlink-exclude.pl b/scripts/vyatta-watchlink-exclude.pl new file mode 100755 index 0000000..9805463 --- /dev/null +++ b/scripts/vyatta-watchlink-exclude.pl @@ -0,0 +1,144 @@ +#!/usr/bin/perl +# +# Module: vyatta-watchlink-exclude.pl +# +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# A copy of the GNU General Public License is available as +# `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution +# or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'. +# You can also obtain it by writing to the Free Software Foundation, +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# 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: March 2008 +# Description: Script to update watchlink exclude file +# +# **** End License **** +# + +# +# parameters: +# --id="" : owner of exclude line (e.g. vrrp, ha) [required] +# --action="" : add or remove [required] +# --intf="" : interface [required] +# --ipaddr="" : ip address or network to execlude [optional] +# --signal : should watchlink get signaled [optional] +# +# Expected format of exclude file: +# +# [interface] ([ipv4addr]|ipv4net]) # id +# + +use Getopt::Long; +use POSIX; + +use strict; +use warnings; + +my $exclude_file = '/var/linkstatus/exclude'; +my $watchlink_pid = '/var/run/vyatta/quagga/watchlink.pid'; + +sub read_exclude_file { + my $FILE; + my @lines = (); + if (! -e $exclude_file) { + return @lines; + } + open($FILE, "<", $exclude_file) or die "Error: read() $!"; + @lines = <$FILE>; + close($FILE); + chomp @lines; + return @lines; +} + +sub write_exclude_file { + my @lines = @_; + + my $FILE; + open($FILE, ">", $exclude_file) or die "Error: write() $!"; + if (scalar(@lines) > 0) { + print $FILE join("\n", @lines), "\n"; + } + close($FILE); +} + +sub remove_exclude_line { + my ($remove_line, @lines) = @_; + + my @new_lines; + my $match = 0; + foreach my $line (@lines) { + if ($line eq $remove_line) { + $match++; + } else { + push @new_lines, $line; + } + } + if ($match < 1) { + die "Error: no match found for $remove_line"; + } + return @new_lines; +} + + +# +# main +# + +my ($opt_id, $opt_action, $opt_intf, $opt_ipaddr, $opt_ipnet, $opt_signal); + +GetOptions("id=s" => \$opt_id, + "action=s" => \$opt_action, + "intf=s" => \$opt_intf, + "ipaddr=s" => \$opt_ipaddr, + "signal!" => \$opt_signal, + ); + +if (!(defined $opt_id and defined $opt_action and defined $opt_intf) ) { + die "Error: parameters --id --intf --action must be set"; +} + +if ($opt_action ne "add" and $opt_action ne "remove") { + die "Error: --action must be \"add\" or \"remove\" "; +} + +my @lines = read_exclude_file(); +my $new_line = "$opt_intf "; +if (defined $opt_ipaddr) { + $new_line .= "$opt_ipaddr "; +} +if (defined $opt_id) { + $new_line .= "# $opt_id"; +} + +if ($opt_action eq "add") { + push @lines, $new_line; +} else { + @lines = remove_exclude_line($new_line, @lines); +} +write_exclude_file(@lines); + +if (defined $opt_signal) { + if (! -e $watchlink_pid) { + die "Error: missing pid file [$watchlink_pid]\n"; + } + my $pid = `cat $watchlink_pid`; + chomp $pid; + system("kill -10 $pid"); +} + +# end of file |