diff options
author | Alex Harpin <development@landsofshadow.co.uk> | 2015-02-08 13:37:10 +0000 |
---|---|---|
committer | Alex Harpin <development@landsofshadow.co.uk> | 2015-02-08 13:37:10 +0000 |
commit | 628cb48a035c3351ff235e1d5143ac98fda9de89 (patch) | |
tree | bac0fe3ef852197810dc1ce62c0bd6c89af34869 | |
parent | 74e31c7d12a912ec263a38b663c83b18a910a888 (diff) | |
download | vyatta-wanloadbalance-628cb48a035c3351ff235e1d5143ac98fda9de89.tar.gz vyatta-wanloadbalance-628cb48a035c3351ff235e1d5143ac98fda9de89.zip |
vyatta-wanloadbalance: formatting changes for style consistency
Update scripts/vyatta-wanloadbalance.pl and vyatta-wlb-connection.pl to
have consistent identation levels and style throughout.
-rw-r--r-- | scripts/vyatta-wanloadbalance.pl | 503 | ||||
-rw-r--r-- | scripts/vyatta-wlb-connection.pl | 68 |
2 files changed, 277 insertions, 294 deletions
diff --git a/scripts/vyatta-wanloadbalance.pl b/scripts/vyatta-wanloadbalance.pl index a8f69de..7b26092 100644 --- a/scripts/vyatta-wanloadbalance.pl +++ b/scripts/vyatta-wanloadbalance.pl @@ -1,11 +1,11 @@ #!/usr/bin/perl -w # # Module: vyatta-wanloadbalance.pl -# +# # 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. -# +# # **** End License **** # use lib "/opt/vyatta/share/perl5/"; @@ -20,31 +20,31 @@ use POSIX; use File::Copy; sub write_health { -#open conf + + #open conf my $config = new Vyatta::Config; my $valid = "false"; if ($config->exists("load-balancing wan disable-source-nat")) { - print FILE_LCK "disable-source-nat\n"; + print FILE_LCK "disable-source-nat\n"; } if ($config->exists("load-balancing wan enable-local-traffic")) { - print FILE_LCK "enable-local-traffic\n"; + print FILE_LCK "enable-local-traffic\n"; } - + if ($config->exists("load-balancing wan sticky-connections inbound")) { - print FILE_LCK "sticky-connections inbound\n"; + print FILE_LCK "sticky-connections inbound\n"; } if ($config->exists("load-balancing wan flush-connections")) { - print FILE_LCK "flush-conntrack\n"; + print FILE_LCK "flush-conntrack\n"; } - my $hook = $config->returnValue("load-balancing wan hook"); if (defined $hook) { - print FILE_LCK "hook \"" . $hook . "\"\n"; + print FILE_LCK "hook \"" . $hook . "\"\n"; } print FILE_LCK "health {\n"; @@ -52,77 +52,73 @@ sub write_health { $config->setLevel("load-balancing wan interface-health"); my @eths = $config->listNodes(); foreach my $ethNode (@eths) { - $config->setLevel("load-balancing wan interface-health"); - - print FILE_LCK "\tinterface " . $ethNode . " {\n"; - - my $option = $config->returnValue("$ethNode failure-count"); - if (defined $option) { - print FILE_LCK "\t\tfailure-ct " . $option . "\n"; - } - - $option = $config->returnValue("$ethNode success-count"); - if (defined $option) { - print FILE_LCK "\t\tsuccess-ct " . $option . "\n"; - } - - $option = $config->returnValue("$ethNode nexthop"); - if (defined $option) { - print FILE_LCK "\t\tnexthop " . $option . "\n"; - $valid = "true"; - } - else { - print "ERROR: nexthop must be specified\n"; - exit 1; - } - - $config->setLevel("load-balancing wan interface-health $ethNode test"); - my @rules = $config->listNodes(); - foreach my $rule (@rules) { - print FILE_LCK "\t\trule " . $rule . " {\n"; - - my $test_type = $config->returnValue("$rule type"); - if ((defined $test_type) && ($test_type eq "ttl")) { - print FILE_LCK "\t\t\ttype udp {\n"; - my $ttl_limit = $config->returnValue("$rule ttl-limit"); - if (defined $ttl_limit) { - print FILE_LCK "\t\t\t\tttl $ttl_limit\n"; - } - } - elsif (defined($test_type) && ($test_type eq "user-defined")) { - print FILE_LCK "\t\t\ttype user-defined {\n"; - my $test_script = $config->returnValue("$rule test-script"); - if (defined $test_script) { - print FILE_LCK "\t\t\t\ttest-script $test_script\n"; - } - else { - print "ERROR: script must be defined for test-script\n"; - exit 1; - } - } - else { - print FILE_LCK "\t\t\ttype ping {\n"; - } - - my $option = $config->returnValue("$rule target"); - if (defined $option) { - print FILE_LCK "\t\t\t\ttarget " . $option . "\n"; - } - - $option = $config->returnValue("$rule resp-time"); - if (defined $option) { - print FILE_LCK "\t\t\t\tresp-time " . $option*1000 . "\n"; - } - print FILE_LCK "\t\t\t}\n"; - - print FILE_LCK "\t\t}\n"; - } - print FILE_LCK "\t}\n"; + $config->setLevel("load-balancing wan interface-health"); + + print FILE_LCK "\tinterface " . $ethNode . " {\n"; + + my $option = $config->returnValue("$ethNode failure-count"); + if (defined $option) { + print FILE_LCK "\t\tfailure-ct " . $option . "\n"; + } + + $option = $config->returnValue("$ethNode success-count"); + if (defined $option) { + print FILE_LCK "\t\tsuccess-ct " . $option . "\n"; + } + + $option = $config->returnValue("$ethNode nexthop"); + if (defined $option) { + print FILE_LCK "\t\tnexthop " . $option . "\n"; + $valid = "true"; + }else { + print "ERROR: nexthop must be specified\n"; + exit 1; + } + + $config->setLevel("load-balancing wan interface-health $ethNode test"); + my @rules = $config->listNodes(); + foreach my $rule (@rules) { + print FILE_LCK "\t\trule " . $rule . " {\n"; + + my $test_type = $config->returnValue("$rule type"); + if ((defined $test_type) && ($test_type eq "ttl")) { + print FILE_LCK "\t\t\ttype udp {\n"; + my $ttl_limit = $config->returnValue("$rule ttl-limit"); + if (defined $ttl_limit) { + print FILE_LCK "\t\t\t\tttl $ttl_limit\n"; + } + }elsif (defined($test_type) && ($test_type eq "user-defined")) { + print FILE_LCK "\t\t\ttype user-defined {\n"; + my $test_script = $config->returnValue("$rule test-script"); + if (defined $test_script) { + print FILE_LCK "\t\t\t\ttest-script $test_script\n"; + }else { + print "ERROR: script must be defined for test-script\n"; + exit 1; + } + }else { + print FILE_LCK "\t\t\ttype ping {\n"; + } + + my $option = $config->returnValue("$rule target"); + if (defined $option) { + print FILE_LCK "\t\t\t\ttarget " . $option . "\n"; + } + + $option = $config->returnValue("$rule resp-time"); + if (defined $option) { + print FILE_LCK "\t\t\t\tresp-time " . $option*1000 . "\n"; + } + print FILE_LCK "\t\t\t}\n"; + + print FILE_LCK "\t\t}\n"; + } + print FILE_LCK "\t}\n"; } print FILE_LCK "}\n\n"; - + if ($valid eq "false") { - print "WARNING: A valid WAN load-balance configuration requires an interface with a nexthop\n"; + print "WARNING: A valid WAN load-balance configuration requires an interface with a nexthop\n"; } return $valid; } @@ -134,185 +130,178 @@ sub write_rules { $config->setLevel('load-balancing wan rule'); my @rules = $config->listNodes(); - + #destination foreach my $rule (@rules) { - print FILE_LCK "rule " . $rule . " {\n"; - - my $exclude = "false"; - - $config->setLevel('load-balancing wan rule'); - - if ($config->exists("$rule exclude")) { - $exclude = "true"; - print FILE_LCK "\texclude\n"; - } - - if ($config->exists("$rule failover")) { - print FILE_LCK "\tfailover\n"; - } - - if ($config->exists("$rule per-packet-balancing")) { - print FILE_LCK "\tper-packet-balancing\n"; - } - - if ($config->exists("$rule failover") && $config->exists("$rule exclude")) { - print "ERROR: failover cannot be configured with exclude\n"; - exit 1; - } - - if ($config->exists("$rule limit") && $config->exists("$rule exclude")) { - print "ERROR: limit cannot be used with exclude\n"; - exit 1; - } - - if ($config->exists("$rule limit")) { - print FILE_LCK "\tlimit {\n"; - my $limit_burst = $config->returnValue("$rule limit burst"); - if (defined $limit_burst) { - print FILE_LCK "\t\tburst " . $limit_burst . "\n"; - } - - my $limit_rate = $config->returnValue("$rule limit rate"); - if (defined $limit_rate) { - print FILE_LCK "\t\trate " . $limit_rate . "\n"; - } - - my $limit_period = $config->returnValue("$rule limit period"); - if (defined $limit_period) { - print FILE_LCK "\t\tperiod " . $limit_period . "\n"; - } - - my $limit_thresh = $config->returnValue("$rule limit threshold"); - if (defined $limit_thresh) { - print FILE_LCK "\t\tthresh " . $limit_thresh . "\n"; - } - print FILE_LCK "\t}\n"; - } - - my $protocol = $config->returnValue("$rule protocol"); - if (defined $protocol) { - print FILE_LCK "\tprotocol " . $protocol . "\n"; - } - else { - $protocol = ""; - } - - #destination - print FILE_LCK "\tdestination {\n"; - my $daddr = $config->returnValue("$rule destination address"); - if (defined $daddr) { - if (Vyatta::TypeChecker::validate_iptables4_addr($daddr) eq "1") { - print FILE_LCK "\t\taddress \"" . $daddr . "\"\n"; - } - else { - print "Error in destination address configuration\n"; - exit 1; - } - } - - my $option = $config->returnValue("$rule destination port"); - if (defined $option) { - my $can_use_port; - my $port_str; - my $port_err; - - if ($protocol eq "tcp" || $protocol eq "udp") { - $can_use_port = "yes"; - } - ($port_str, $port_err) = Vyatta::Misc::getPortRuleString($option, $can_use_port, "d", $protocol); - if (defined $port_str) { - print FILE_LCK "\t\tport-ipt \"" . $port_str . "\"\n"; - } - else { - print $port_err; - exit 1; - } - } - - print FILE_LCK "\t}\n"; - - #source - $config->setLevel('load-balancing wan rule'); - - - print FILE_LCK "\tsource {\n"; - my $saddr = $config->returnValue("$rule source address"); - if (defined $saddr) { - if (Vyatta::TypeChecker::validate_iptables4_addr($saddr) eq "1") { - print FILE_LCK "\t\taddress \"" . $saddr . "\"\n"; - } - else { - print "Error in source address configuration\n"; - exit 1; - } - } - - $option = $config->returnValue("$rule source port"); - if (defined $option) { - my $can_use_port; - my $port_str; - my $port_err; - - if ($protocol eq "tcp" || $protocol eq "udp") { - $can_use_port = "yes"; - } - ($port_str, $port_err) = Vyatta::Misc::getPortRuleString($option, $can_use_port, "s", $protocol); - if (defined $port_str) { - print FILE_LCK "\t\tport-ipt \"" . $port_str . "\"\n"; - } - else { - print $port_err; - exit 1; - } - } - print FILE_LCK "\t}\n"; - - #inbound-interface - my $inbound = $config->returnValue("$rule inbound-interface"); - if (defined $inbound) { - print FILE_LCK "\tinbound-interface " . $inbound . "\n" - } - else { - print "ERROR: inbound-interface must be specified\n"; - exit 1; - } - - #interface - $config->setLevel("load-balancing wan rule $rule interface"); - my @eths = $config->listNodes(); - - if ($#eths < 0 && $exclude eq "false") { - print "WARNING: rule $rule will be inactive because no (outbound) interfaces have been defined for this rule\n"; - } - elsif ($#eths >= 0 && $exclude eq "true") { - print "WARNING: interfaces (outbound) are not used when exclude has been defined for rule $rule\n"; - } - - foreach my $ethNode (@eths) { - if ($inbound eq $ethNode) { - print "WARNING: inbound interface is the same as the outbound interface\n"; - } - - if ($exclude ne "true") { - $outbound_defined = "true"; - } - - print FILE_LCK "\tinterface " . $ethNode . " {\n"; - - $option = $config->returnValue("$ethNode weight"); - if (defined $option) { - print FILE_LCK "\t\tweight " . $option . "\n"; - } - print FILE_LCK "\t}\n"; - } - print FILE_LCK "}\n"; + print FILE_LCK "rule " . $rule . " {\n"; + + my $exclude = "false"; + + $config->setLevel('load-balancing wan rule'); + + if ($config->exists("$rule exclude")) { + $exclude = "true"; + print FILE_LCK "\texclude\n"; + } + + if ($config->exists("$rule failover")) { + print FILE_LCK "\tfailover\n"; + } + + if ($config->exists("$rule per-packet-balancing")) { + print FILE_LCK "\tper-packet-balancing\n"; + } + + if ($config->exists("$rule failover") && $config->exists("$rule exclude")) { + print "ERROR: failover cannot be configured with exclude\n"; + exit 1; + } + + if ($config->exists("$rule limit") && $config->exists("$rule exclude")) { + print "ERROR: limit cannot be used with exclude\n"; + exit 1; + } + + if ($config->exists("$rule limit")) { + print FILE_LCK "\tlimit {\n"; + my $limit_burst = $config->returnValue("$rule limit burst"); + if (defined $limit_burst) { + print FILE_LCK "\t\tburst " . $limit_burst . "\n"; + } + + my $limit_rate = $config->returnValue("$rule limit rate"); + if (defined $limit_rate) { + print FILE_LCK "\t\trate " . $limit_rate . "\n"; + } + + my $limit_period = $config->returnValue("$rule limit period"); + if (defined $limit_period) { + print FILE_LCK "\t\tperiod " . $limit_period . "\n"; + } + + my $limit_thresh = $config->returnValue("$rule limit threshold"); + if (defined $limit_thresh) { + print FILE_LCK "\t\tthresh " . $limit_thresh . "\n"; + } + print FILE_LCK "\t}\n"; + } + + my $protocol = $config->returnValue("$rule protocol"); + if (defined $protocol) { + print FILE_LCK "\tprotocol " . $protocol . "\n"; + }else { + $protocol = ""; + } + + #destination + print FILE_LCK "\tdestination {\n"; + my $daddr = $config->returnValue("$rule destination address"); + if (defined $daddr) { + if (Vyatta::TypeChecker::validate_iptables4_addr($daddr) eq "1") { + print FILE_LCK "\t\taddress \"" . $daddr . "\"\n"; + }else { + print "Error in destination address configuration\n"; + exit 1; + } + } + + my $option = $config->returnValue("$rule destination port"); + if (defined $option) { + my $can_use_port; + my $port_str; + my $port_err; + + if ($protocol eq "tcp" || $protocol eq "udp") { + $can_use_port = "yes"; + } + ($port_str, $port_err) = Vyatta::Misc::getPortRuleString($option, $can_use_port, "d", $protocol); + if (defined $port_str) { + print FILE_LCK "\t\tport-ipt \"" . $port_str . "\"\n"; + }else { + print $port_err; + exit 1; + } + } + + print FILE_LCK "\t}\n"; + + #source + $config->setLevel('load-balancing wan rule'); + + print FILE_LCK "\tsource {\n"; + my $saddr = $config->returnValue("$rule source address"); + if (defined $saddr) { + if (Vyatta::TypeChecker::validate_iptables4_addr($saddr) eq "1") { + print FILE_LCK "\t\taddress \"" . $saddr . "\"\n"; + }else { + print "Error in source address configuration\n"; + exit 1; + } + } + + $option = $config->returnValue("$rule source port"); + if (defined $option) { + my $can_use_port; + my $port_str; + my $port_err; + + if ($protocol eq "tcp" || $protocol eq "udp") { + $can_use_port = "yes"; + } + ($port_str, $port_err) = Vyatta::Misc::getPortRuleString($option, $can_use_port, "s", $protocol); + if (defined $port_str) { + print FILE_LCK "\t\tport-ipt \"" . $port_str . "\"\n"; + }else { + print $port_err; + exit 1; + } + } + print FILE_LCK "\t}\n"; + + #inbound-interface + my $inbound = $config->returnValue("$rule inbound-interface"); + if (defined $inbound) { + print FILE_LCK "\tinbound-interface " . $inbound . "\n"; + }else { + print "ERROR: inbound-interface must be specified\n"; + exit 1; + } + + #interface + $config->setLevel("load-balancing wan rule $rule interface"); + my @eths = $config->listNodes(); + + if ($#eths < 0 && $exclude eq "false") { + print "WARNING: rule $rule will be inactive because no (outbound) interfaces have been defined for this rule\n"; + }elsif ($#eths >= 0 && $exclude eq "true") { + print "WARNING: interfaces (outbound) are not used when exclude has been defined for rule $rule\n"; + } + + foreach my $ethNode (@eths) { + if ($inbound eq $ethNode) { + print "WARNING: inbound interface is the same as the outbound interface\n"; + } + + if ($exclude ne "true") { + $outbound_defined = "true"; + } + + print FILE_LCK "\tinterface " . $ethNode . " {\n"; + + $option = $config->returnValue("$ethNode weight"); + if (defined $option) { + print FILE_LCK "\t\tweight " . $option . "\n"; + } + print FILE_LCK "\t}\n"; + } + print FILE_LCK "}\n"; } if ($outbound_defined eq "false") { - print "WARNING: At least one rule with an (outbound) interface must be defined for WAN load balancing to be active\n"; - #allow this configuration, just generate the warning - return "true"; + print "WARNING: At least one rule with an (outbound) interface must be defined for WAN load balancing to be active\n"; + + #allow this configuration, just generate the warning + return "true"; } return $outbound_defined; } @@ -323,8 +312,7 @@ sub usage { exit 1; } -GetOptions("valid-nexthop=s" => \$nexthop, - ) or usage(); +GetOptions("valid-nexthop=s" => \$nexthop,) or usage(); ####main my $conf_file = '/var/run/load-balance/wlb.conf'; @@ -335,14 +323,14 @@ my $conf_lck_file = '/var/run/load-balance/wlb.conf.lck'; if (defined $nexthop) { my $rc = Vyatta::TypeChecker::validateType('ipv4', $nexthop, 1); if (!$rc && $nexthop ne "dhcp") { - exit 1; + exit 1; } exit 0; } #open file `touch $conf_file`; #creates file on first access -open(FILE, "<$conf_file") or die "Can't open wlb config file"; +open(FILE, "<$conf_file") or die "Can't open wlb config file"; open(FILE_LCK, "+>$conf_lck_file") or die "Can't open wlb lock file"; my $success = write_health(); @@ -358,10 +346,9 @@ if ($success eq "false") { close FILE; close FILE_LCK; -copy ($conf_lck_file,$conf_file); +copy($conf_lck_file,$conf_file); unlink($conf_lck_file); - #finally kick the process system "/opt/vyatta/sbin/vyatta-wanloadbalance.init restart $conf_file 2>/dev/null"; diff --git a/scripts/vyatta-wlb-connection.pl b/scripts/vyatta-wlb-connection.pl index c67e8bf..2f59545 100644 --- a/scripts/vyatta-wlb-connection.pl +++ b/scripts/vyatta-wlb-connection.pl @@ -1,25 +1,25 @@ #!/usr/bin/perl # # Module: vyatta-show-wlb-connection.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. -# +# # This code was originally developed by Vyatta, Inc. # Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc. # All Rights Reserved. -# +# # Author: Michael Larson # Date: December 2008 # Description: Script to display wlb connection information -# +# # **** End License **** # @@ -32,12 +32,11 @@ if (!open($CONFFILE, "<", "/var/run/load-balance/wlb.conf")) { $_ = <$CONFFILE>; if (/disable-source-nat/) { if (!open($FILE, "<", "/proc/net/ip_conntrack")) { - return; + return; } -} -else { +} else { if (!open($FILE, "/usr/sbin/conntrack -L -n|")) { - return; + return; } } @@ -45,40 +44,37 @@ print "Type\tState\t\tSrc\t\t\tDst\t\t\tPackets\tBytes\n"; @line = <$FILE>; foreach (@line) { - $_ =~ s/\[\S+\]\s//; - + $_ =~ s/\[\S+\]\s//; - my $proto,$tmp,$state,$src,$dst,$sport,$dport,$pkts,$bytes; + my $proto,$tmp,$state,$src,$dst,$sport,$dport,$pkts,$bytes; + if (/tcp/) { + ($proto,$tmp,$tmp,$state,$src,$dst,$sport,$dport,$pkts,$bytes) = split(' ',$_); + } elsif (/udp/) { + $state = ""; + ($proto,$tmp,$tmp,$src,$dst,$sport,$dport,$pkts,$bytes) = split(' ',$_); + } - if (/tcp/) { - ($proto,$tmp,$tmp,$state,$src,$dst,$sport,$dport,$pkts,$bytes) = split(' ',$_); - } - elsif (/udp/) { - $state = ""; - ($proto,$tmp,$tmp,$src,$dst,$sport,$dport,$pkts,$bytes) = split(' ',$_); - } - ($tmp,$src) = split('=',$src); - ($tmp,$dst) = split('=',$dst); - ($tmp,$dport) = split('=',$dport); - ($tmp,$sport) = split('=',$sport); - ($tmp,$pkts) = split('=',$pkts); - ($tmp,$bytes) = split('=',$bytes); + ($tmp,$src) = split('=',$src); + ($tmp,$dst) = split('=',$dst); + ($tmp,$dport) = split('=',$dport); + ($tmp,$sport) = split('=',$sport); + ($tmp,$pkts) = split('=',$pkts); + ($tmp,$bytes) = split('=',$bytes); - my $snet = sprintf("%s:%s%-10s",$src,$sport); - $snet = substr($snet,0,18); + my $snet = sprintf("%s:%s%-10s",$src,$sport); + $snet = substr($snet,0,18); - my $dnet = sprintf("%s:%s%-10s",$dst,$dport); - $dnet = substr($dnet,0,18); + my $dnet = sprintf("%s:%s%-10s",$dst,$dport); + $dnet = substr($dnet,0,18); - $state = sprintf("%s%-12s",$state); - $state = substr($state,0,12); + $state = sprintf("%s%-12s",$state); + $state = substr($state,0,12); - #mark=[1-9] - if (/ mark=[1-9]/) { - print "$proto\t$state\t$snet\t$dnet\t$pkts\t$bytes\n"; - } + #mark=[1-9] + if (/ mark=[1-9]/) { + print "$proto\t$state\t$snet\t$dnet\t$pkts\t$bytes\n"; + } } #now dump out results - |