#!/usr/local/bin/perl -w # # $Id: pinggw 356 2011-09-24 23:25:47Z svnuser $ # # $Log: pinggw,v $ # Revision 1.6 2000/07/09 02:26:39 merlin # Fixed y2K display # # Revision 1.5 1998/09/12 00:40:03 merlin # Added handling of common errors # # Revision 1.4 1998/09/11 22:26:13 merlin # Modified to ping the gateway, and other hosts on the internet # # Revision 1.3 1998/09/11 16:58:35 merlin # Added handling of hours # # Revision 1.2 1998/09/11 16:35:39 merlin # Show when log begin and how long the connection was down # # Revision 1.1 1998/09/11 07:20:01 merlin # Tool to test connectivity to an IP address # # # /etc/network/interfaces: # down bash -c "pkill -f '/usr/local/sbin/pinggw' &>/dev/null" # up bash -c "ipmasq; pkill -f '[Pp]erl /usr/local/sbin/pinggw' &>/dev/null; nohup /usr/local/sbin/pinggw /var/log/connectlog 198.144.201.217 198.144.206.1 213.228.62.38 209.81.13.136 >>/root/connectlog.out 2>&1 &" # # speed test: echo -n "`date '+%Y/%M/%d %H:%M:%S'` | "; wget ftp://ftp.tsoft.com/pub/mdurkin/bigzerofile 2>&1 | awk '/bigzerofile.*saved/ { print $2 $3 }'; /bin/rm bigzerofile # use FileHandle; use strict; my @IP; my %IP; my %IP_timedown; my $gwip; my $logfile; my @weekday=qw (Sun Mon Tue Wed Thu Fri Sat); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $year+=1900; $mon++; die "$0 logfile gateway's_ip_to_ping [Other ips on the internet]" if (! defined $ARGV[0] or ! defined $ARGV[1]); $logfile=shift @ARGV; open (LOG, ">>$logfile") or die "Can't open $logfile: $!"; LOG->autoflush(1); while ($_=shift @ARGV) { die "$_ is not an IP in numerical form\n" if (! /^[0-9.]+$/); push(@IP, $_); $IP{$_}='up'; } $gwip=shift @IP; printf LOG ("%.4d/%.2d/%.2d ($weekday[$wday]) %.2d:%.2d:%.2d - Begin. Gateway IP is: $gwip", $year,$mon,$mday,$hour,$min,$sec); if (defined $IP[0]) { print LOG ", and other IPs to monitor are: ".join (" ", @IP); } print LOG "\n"; while(1) { my $endsec; my $debsec; my $showsec; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $year+=1900; $mon++; ($endsec = $sec) =~ s/.*(.)$/$1/; # Since a sleep 1 can sleep for 2 seconds, we take that into account if ($endsec != 0 and $endsec != 1) { sleep 1; } else { ($showsec = $sec) =~ s/^(.*).$/${1}0/; printf LOG ("%.4d/%.2d/%.2d ($weekday[$wday]) %.2d:%.2d:%.2d - Mark\n", $year,$mon,$mday,$hour,$min,$sec) if ($min == 0 and $showsec == 0); ($debsec = $sec) =~ s/(.)$//; if ($debsec =~ /^[135]/) { # If we sleep 8 after starting at xx:y1 secs, this might bring us # all the way to xx:z0, so we don't want to sleep any longer than 8 sleep 8; } else { my @ping; # We need to sleep at least 2 secs to make sure we don't run twice # in the same 20 sec time slice sleep 2; open (PING, "fping ".join(" ",$gwip,@IP)." 2>&1 |") or die "fping failed: $!"; @ping=; die "fping not found on your system, get it from http://ftpsearch.ntnu.no/cgi-bin/search?type=Exact+search&query=fping-2%2e2b1%2etar%2egz\n" if (grep (/command not found/, @ping)); die "Unexpected result from fping. fping outputted the following:\n".join("", @ping)."\n" if (! grep (/alive/, @ping) and ! grep (/unreachable/, @ping)); if ($IP{$gwip} eq "down") { if (grep (/$gwip is alive/, @ping)) { my $downsec=time-$IP_timedown{$gwip}; my $downmin=int ($downsec/60); my $downhour=int ($downmin/60); $downsec-=$downmin*60; $downmin-=$downhour*60; printf LOG ("%.4d/%.2d/%.2d ($weekday[$wday]) %.2d:%.2d:%.2d - Connection to gateway ($gwip) came back up. Downtime was about: $downhour:%.2d:%.2d\n", $year,$mon,$mday,$hour,$min,$sec,$downmin,$downsec); $IP{$gwip}="up"; } } else { if (grep (/$gwip is unreachable/, @ping) or grep (/sendto : Network is unreachable/, @ping)) { printf LOG ("%.4d/%.2d/%.2d ($weekday[$wday]) %.2d:%.2d:%.2d - Connection to $gwip went down", $year,$mon,$mday,$hour,$min,$sec); print LOG " (network unreachable error)" if (grep (/sendto : Network is unreachable/, @ping)); print LOG "\n"; $IP{$gwip}="down"; $IP_timedown{$gwip}=time; } } if ($IP{$gwip} eq "up") { my $otherip; foreach $otherip (@IP) { if ($IP{$otherip} eq "down" and grep (/$otherip is alive/, @ping)) { my $downsec=time-$IP_timedown{$otherip}; my $downmin=int ($downsec/60); my $downhour=int ($downmin/60); $downsec-=$downmin*60; $downmin-=$downhour*60; printf LOG ("%.4d/%.2d/%.2d ($weekday[$wday]) %.2d:%.2d:%.2d - Remote host ($otherip) is reachable again (it was unreachable for about: $downhour:%.2d:%.2d)\n", $year,$mon,$mday,$hour,$min,$sec,$downmin,$downsec); $IP{$otherip}="up"; } elsif ($IP{$otherip} eq "up" and grep (/$otherip is unreachable/, @ping)) { printf LOG ("%.4d/%.2d/%.2d ($weekday[$wday]) %.2d:%.2d:%.2d - Remote host ($otherip) just became unreachable\n", $year,$mon,$mday,$hour,$min,$sec); $IP{$otherip}="down"; $IP_timedown{$otherip}=time; } } } } } }