| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | load_balance.pl Load balance a host or network across two or more Internet connections. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | # get status report | ||
| 10 | % sudo load_balance.pl -s | ||
| 11 | |||
| 12 | # start link monitoring and load balance across all up ISPs | ||
| 13 | % sudo load_balance.pl | ||
| 14 | |||
| 15 | # load balance across "CABLE" and "DSL" only | ||
| 16 | % sudo load_balance.pl CABLE DSL | ||
| 17 | |||
| 18 | # print out the routing and firewall commands that would | ||
| 19 | # ordinarily be executed | ||
| 20 | % load_balance.pl -d | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This script can be run on a Linux-based home router or standalone | ||
| 25 | computer to load balance your network connection among two or more | ||
| 26 | Internet Service Providers (ISPs). When aggregated across multiple | ||
| 27 | simultaneous connections, you will achieve the sum of the bandwidth of | ||
| 28 | all ISP connections. In addition, the script will continuously ping | ||
| 29 | each outgoing interface and adjust routing in the event that one or | ||
| 30 | more ISPs become unavailable. This provides failover. | ||
| 31 | |||
| 32 | The script can be called with no arguments, in which case it will mark | ||
| 33 | all known ISPs as being up and launch the "lsm" link monitor to test | ||
| 34 | each one periodically for connectivity. It can also be called with one | ||
| 35 | or more symbolic names for ISP connections, as defined in | ||
| 36 | load_balance.conf. These will be forced "up" and other ISP connections | ||
| 37 | will be forced "down". | ||
| 38 | |||
| 39 | Other command-line options allow you to view the status of your ISP | ||
| 40 | connections, kill a running lsm, and more. | ||
| 41 | |||
| 42 | Generally this script must be run as root, since it alters the routing | ||
| 43 | table and firewall rules. | ||
| 44 | |||
| 45 | For full installation and configuration instructions, please see | ||
| 46 | L<http://lstein.github.io/Net-ISP-Balance/>. | ||
| 47 | |||
| 48 | =head1 COMMAND-LINE OPTIONS | ||
| 49 | |||
| 50 | Each command-line option can be abbreviated or used in long-form. | ||
| 51 | |||
| 52 | --debug, -d Turn on debugging. In this mode, no firewall or | ||
| 53 | routing commands will be executed, but instead | ||
| 54 | will be printed to standard output for inspection. | ||
| 55 | |||
| 56 | --verbose, -v Verbose output. Echo all route and iptables commands | ||
| 57 | to STDERR before executing them. | ||
| 58 | |||
| 59 | --Version, -V Print version of Net::ISP::Balance. | ||
| 60 | |||
| 61 | --status,-s Print current status of each monitored ISP interface | ||
| 62 | to STDOUT. | ||
| 63 | |||
| 64 | --kill,-k Kill any running lsm process. | ||
| 65 | |||
| 66 | --flush,-f Flush all firewall chains and rules. If not given, then | ||
| 67 | the default is to try to keep custom iptables rules | ||
| 68 | introduced by firewall utilities such as fail2ban and | ||
| 69 | miniunpnpd. | ||
| 70 | |||
| 71 | --help,-h Print this message. | ||
| 72 | |||
| 73 | =head1 COMMON USAGE | ||
| 74 | |||
| 75 | This section describes common usage patterns. Note that | ||
| 76 | load_balance.pl must always be run as root. | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item Mark all ISPs as up and start link monitoring: | ||
| 81 | |||
| 82 | % sudo load_balance.pl | ||
| 83 | |||
| 84 | =item Mark just the listed ISP(s) as up. Do not start link monitoring: | ||
| 85 | |||
| 86 | % sudo load_balance.pl CABLE | ||
| 87 | |||
| 88 | =item Bring up the listed ISP service, leaving the other(s) in their current | ||
| 89 | state. This is usually done behind the scenes by lsm. | ||
| 90 | |||
| 91 | % sudo load_balance.pl up CABLE | ||
| 92 | |||
| 93 | =item Bring down the listed ISP service, leaving the other(s) in the | ||
| 94 | current state. This is usually done behind the scenes by lsm: | ||
| 95 | |||
| 96 | % sudo load_balance.pl down CABLE | ||
| 97 | |||
| 98 | =item Print current status of the defined ISPs: | ||
| 99 | |||
| 100 | % sudo load_balance.pl -s | ||
| 101 | |||
| 102 | =item Kill lsm and discontinue the link monitoring: | ||
| 103 | |||
| 104 | % sudo load_balance.pl -k | ||
| 105 | |||
| 106 | =item Print to standard output all the routing and firewall commands | ||
| 107 | that would ordinarily be issued on startup. Do not launch lsm or | ||
| 108 | actually change anything: | ||
| 109 | |||
| 110 | % sudo load_balance.pl -d | ||
| 111 | |||
| 112 | =back | ||
| 113 | |||
| 114 | =head1 FILES | ||
| 115 | |||
| 116 | This section gives locations of important files. | ||
| 117 | |||
| 118 | =head2 Debian/Ubuntu/Mint systems | ||
| 119 | |||
| 120 | /etc/network/balance.conf # Main configuration file | ||
| 121 | /etc/network/balance/firewall/ # Additional firewall rules | ||
| 122 | /etc/network/balance/routes/ # Additional routing rules | ||
| 123 | |||
| 124 | =head2 RedHat/CentOS systems | ||
| 125 | |||
| 126 | /etc/sysconfig/network-scripts/balance.conf # Main configuration file | ||
| 127 | /etc/sysconfig/network-scripts/balance/firewall/ # Additional firewall rules | ||
| 128 | /etc/sysconfig/network-scripts/balance/routes/ # Additional routing rules | ||
| 129 | |||
| 130 | =head2 Format of the balance.conf: | ||
| 131 | |||
| 132 | balance.conf is the main configuration file. It defines the interfaces | ||
| 133 | connected to the ISPs and to the LAN (if running on a router). Here is | ||
| 134 | a typical example: | ||
| 135 | |||
| 136 | #service device role ping-ip | ||
| 137 | CABLE eth0 isp 173.194.43.95 | ||
| 138 | DSL eth1 isp 173.194.43.95 | ||
| 139 | LAN eth2 lan | ||
| 140 | |||
| 141 | # name=value pairs define lsm configuration variables | ||
| 142 | warn_email=fred@gmail.com | ||
| 143 | max_packet_loss=10 | ||
| 144 | min_packet_loss=5 | ||
| 145 | |||
| 146 | There are two parts of the configuration file. The first part, which | ||
| 147 | is required, is a four-column table that defines interfaces to be | ||
| 148 | monitored. | ||
| 149 | |||
| 150 | The first column is a service name that is used to bring up or down | ||
| 151 | the needed routes and firewall rules. | ||
| 152 | |||
| 153 | The second column is the name of the network interface device that | ||
| 154 | connects to that service. | ||
| 155 | |||
| 156 | The third column is either "isp" or "lan". There may be any number of | ||
| 157 | these. The script will load balance traffic across all ISPs, and will | ||
| 158 | act as a firewall between the LAN (if any) and the Internet. You do | ||
| 159 | not need to have a "lan" entry if this is a standalone host. | ||
| 160 | |||
| 161 | The fourth and last column is the IP address of a host that can be | ||
| 162 | periodically pinged to test the integrity of each ISP connection. If | ||
| 163 | too many pings failed, the service will be brought down and all | ||
| 164 | traffic routed through the remaining ISP(s). The service will continue | ||
| 165 | to be monitored and will be brought up when it is once again | ||
| 166 | working. Choose a host that is not likely to go offline for reasons | ||
| 167 | unrelated to your network connectivity, such as google.com, or the | ||
| 168 | ISP's web site. | ||
| 169 | |||
| 170 | The second (optional) part of the configuration file is a series of | ||
| 171 | name=value pairs that allow you to customize the behavior of lsm, such | ||
| 172 | as where to send email messages when a link's status changes. Please | ||
| 173 | see L<http://lsm.foobar.fi/> for the comprehensive list. | ||
| 174 | |||
| 175 | =head1 SEE ALSO | ||
| 176 | |||
| 177 | L<Net::ISP::Balance> | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Lincoln Stein, lincoln.stein@gmail.com | ||
| 182 | |||
| 183 | Copyright (c) 2014 Lincoln D. Stein | ||
| 184 | |||
| 185 | This package and its accompanying libraries is free software; you can | ||
| 186 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 187 | version 1, or at your option, any later version) or the Artistic | ||
| 188 | License 2.0. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | use strict; | ||
| 193 | use Net::ISP::Balance; | ||
| 194 | use Sys::Syslog; | ||
| 195 | use Fcntl ':flock'; | ||
| 196 | use File::Spec; | ||
| 197 | use Getopt::Long qw(:config no_ignore_case); | ||
| 198 | use Carp 'croak'; | ||
| 199 | use Pod::Usage 'pod2usage'; | ||
| 200 | use constant LOCK_TIMEOUT => 30; # if two processes running simultaneously, length of time second will wait for first | ||
| 201 | |||
| 202 | my ($DEBUG,$VERBOSE,$STATUS,$KILL,$HELP,$FLUSH,$VERSION); | ||
| 203 | my $result = GetOptions('debug' => \$DEBUG, | ||
| 204 | 'verbose'=>\$VERBOSE, | ||
| 205 | 'Version'=>\$VERSION, | ||
| 206 | 'status' => \$STATUS, | ||
| 207 | 'kill' => \$KILL, | ||
| 208 | 'flush' => \$FLUSH, | ||
| 209 | 'help' => \$HELP, | ||
| 210 | ); | ||
| 211 | if (!$result || $HELP) { | ||
| 212 | pod2usage(-message => "Usage: $0", | ||
| 213 | -exitval => -1, | ||
| 214 | -verbose => 2); | ||
| 215 | exit 0; | ||
| 216 | } | ||
| 217 | if ($VERSION) { | ||
| 218 | print $Net::ISP::Balance::VERSION,"\n"; | ||
| 219 | exit 0; | ||
| 220 | } | ||
| 221 | |||
| 222 | my $lock_fh = do_lock(); | ||
| 223 | END { do_unlock($lock_fh); } | ||
| 224 | |||
| 225 | # command line arguments correspond to the ISP services (defined in the config file) | ||
| 226 | # that are "up". LAN services are assumed to be always up. | ||
| 227 | |||
| 228 | openlog('load_balance.pl','ndelay,pid','local0'); | ||
| 229 | |||
| 230 | my $bal = eval {Net::ISP::Balance->new()}; | ||
| 231 | |||
| 232 | fatal_error("Could not initialize balancer; maybe some interfaces are unavailable? Error message=$@") | ||
| 233 | unless $bal;; | ||
| 234 | |||
| 235 | $bal->echo_only($DEBUG); | ||
| 236 | $bal->verbose($VERBOSE); | ||
| 237 | $bal->keep_custom_chains(!$FLUSH); | ||
| 238 | |||
| 239 | # these two subroutines exit | ||
| 240 | do_status() if $STATUS; | ||
| 241 | do_kill_lsm() if $KILL; | ||
| 242 | |||
| 243 | my %SERVICES = map {$_=>1} $bal->isp_services; | ||
| 244 | |||
| 245 | my %LSM_STATE = (up => 'up', | ||
| 246 | down => 'down', | ||
| 247 | long_down => 'down', | ||
| 248 | long_down_to_up => 'up'); | ||
| 249 | |||
| 250 | if (exists $LSM_STATE{$ARGV[0]}) { | ||
| 251 | do_unlock($lock_fh); # to allow eventd to call us recursively | ||
| 252 | $bal->run_eventd(@ARGV); | ||
| 253 | $lock_fh = do_lock(); | ||
| 254 | if (($SERVICES{my $name = $ARGV[1]})) { | ||
| 255 | my $device = $ARGV[3] || $bal->dev($name); | ||
| 256 | syslog('warning',"$name ($device) is now in state '$ARGV[0]'."); | ||
| 257 | $bal->event($name => $LSM_STATE{$ARGV[0]}) if $LSM_STATE{$ARGV[0]}; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | else { | ||
| 262 | my @up = @ARGV ? @ARGV : $bal->isp_services; | ||
| 263 | my %up_services = map {uc($_) => 1} @up; | ||
| 264 | @up = keys %up_services; # uniqueify | ||
| 265 | my @down = grep {!$up_services{$_}} $bal->isp_services; | ||
| 266 | $bal->event($_ => 'up') foreach @up; | ||
| 267 | $bal->event($_ => 'down') foreach @down; | ||
| 268 | } | ||
| 269 | |||
| 270 | syslog('info',"adjusting routing tables..."); | ||
| 271 | $bal->set_routes_and_firewall(); | ||
| 272 | start_lsm($bal) unless @ARGV || $DEBUG; | ||
| 273 | |||
| 274 | exit 0; | ||
| 275 | |||
| 276 | sub do_status { | ||
| 277 | my $state = $bal->event(); | ||
| 278 | for my $svc (sort $bal->isp_services) { | ||
| 279 | printf("%-15s %8s\n",$svc,$state->{$svc}||'unknown'); | ||
| 280 | } | ||
| 281 | if ($< == 0) { # running as root | ||
| 282 | kill(USR1 => `cat /var/run/lsm.pid`); | ||
| 283 | print STDERR "See syslog for detailed link monitoring information from lsm.\n"; | ||
| 284 | } | ||
| 285 | exit 0; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub do_kill_lsm { | ||
| 289 | my $lsm_running = -e '/var/run/lsm.pid' && kill(0=>`cat /var/run/lsm.pid`); | ||
| 290 | if ($lsm_running) { | ||
| 291 | kill(TERM => `cat /var/run/lsm.pid`); | ||
| 292 | print STDERR "lsm process killed\n"; | ||
| 293 | } else { | ||
| 294 | print STDERR "lsm does not seem to be running\n"; | ||
| 295 | } | ||
| 296 | exit 0; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub start_lsm { | ||
| 300 | my $bal = shift; | ||
| 301 | |||
| 302 | my $lsm_conf = $bal->lsm_conf_file; | ||
| 303 | my $bal_conf = $bal->bal_conf_file; | ||
| 304 | |||
| 305 | # kill existing lsm | ||
| 306 | $bal->signal_lsm('TERM'); | ||
| 307 | |||
| 308 | # Create config file | ||
| 309 | open my $fh,'>',$lsm_conf or die "$lsm_conf: $!"; | ||
| 310 | print $fh $bal->lsm_config_text(); | ||
| 311 | close $fh or die "$lsm_conf: $!"; | ||
| 312 | |||
| 313 | # now start the process | ||
| 314 | syslog('info',"Starting lsm link status monitoring daemon"); | ||
| 315 | $bal->start_lsm(); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub fatal_error { | ||
| 319 | my $msg = shift; | ||
| 320 | syslog('crit',$msg); | ||
| 321 | croak $msg,"\n"; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub do_lock { | ||
| 325 | my $lockfile = File::Spec->catfile(File::Spec->tmpdir, | ||
| 326 | "load_balance.$>.lock"); | ||
| 327 | open (my $fh,'>>',$lockfile) or fatal_error("Can't open $lockfile for locking"); | ||
| 328 | eval { | ||
| 329 | local $SIG{ALRM} = sub { die "timeout" }; | ||
| 330 | alarm(LOCK_TIMEOUT); | ||
| 331 | flock($fh,LOCK_EX) or fatal_error("Can't lock $lockfile"); | ||
| 332 | alarm(0); | ||
| 333 | }; | ||
| 334 | fatal_error("Lock timed out") if $@ =~ /timeout/; | ||
| 335 | return $fh; | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | sub do_unlock { | ||
| 340 | my $fh = shift; | ||
| 341 | return unless $fh; | ||
| 342 | flock($fh,LOCK_UN); | ||
| 343 | my $lockfile = get_lockfile(); | ||
| 344 | unlink $lockfile; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub get_lockfile { | ||
| 348 | return File::Spec->catfile(File::Spec->tmpdir, | ||
| 349 | "load_balance.$>.lock"); | ||
| 350 | } |