]>
pere.pagekite.me Git - homepage.git/blob - netwatch-0.1.pl
3 # Tests the possibility to reach the hosts listed in config-file
5 # #author Petter Reinholdtsen
9 # List of hosts to test the connection to
10 $hostsfile = "/home/pere/priv/hostlist";
11 $statusfile = "$hostsfile.status";
12 $logfile = "/home/pere/priv/network.log";
14 # Send note if this is the first change in # seconds
15 $notelimit = 60*5; # five minutes
20 # return true if 2 or more ICMP ping packets are returnet from $host
25 # open(PING, "/etc/ping $host 64 5 2>&1 |") || die "Unable to start ping";
27 # open(PING, "/usr/etc/ping -c 5 $host 2>&1 |") || die "Unable to start ping";
29 open(PING
, "/sbin/ping -c 5 $host 2>&1 |") || die "Unable to start ping";
31 if (/packets transmitted, (\d*) packets received/) {
36 return ($packets > 1);
40 # Print out help on usage.
43 netwatch.pl [-l] [-c <logfile>]
44 -l Print dates in local-time instead of GMT
45 -c Convert logfile to readable format
58 &convert_print_logfile
($opt_c);
62 @hosts = &load_hostfile
($hostsfile);
63 %status = &load_status
;
67 foreach $host (@hosts) {
68 $status{$host} = "unknown 0 0" if (! $status{$host} );
69 ($oldhoststat, $oldtimestamp, $oldnotifiedstamp) =
70 $status{$host} =~ m/(.*) (.*) (.*)/;
72 if ( reachable
($host) ) {
78 # Only report if nothing has happend the last $notelimit seconds
79 $notifiedstamp = $oldnotifiedstamp;
80 if ($hoststat ne $oldhoststat) { # status changed, should we report?
81 if ($timestamp - $oldtimestamp > $notelimit) { # its been a long time
82 $mailinfo{$host} = "$hoststat $timestamp";
83 $notifiedstamp = $timestamp;
85 } else { # same status, should we report?
86 if ( ($timestamp - $oldnotifiedstamp > $notelimit) &&
87 ($oldtimestamp > $oldnotifiedstamp) ) {
88 $mailinfo{$host} = "$hoststat $oldtimestamp";
89 $notifiedstamp = $timestamp;
93 if ($oldhoststat ne $hoststat) {
94 $newstatus{$host} = "$hoststat $timestamp $notifiedstamp";
95 &log_line
("$host $hoststat");
97 $newstatus{$host} = "$oldhoststat $oldtimestamp $notifiedstamp";
101 &save_status
(%newstatus);
103 # Send mail if the link go down and this is the first change in more
104 # then $notelimit seconds.
107 print "Linkstatus endret:\n";
108 foreach $host (sort keys %mailinfo) {
109 ($status, $oldtimestamp) = $mailinfo{$host} =~ m/([^\s]*) (.*)/;
110 write_hostline
($host, $status, $oldtimestamp);
115 # record a line to the $logfile
119 open(LOG
, ">>$logfile");
121 print LOG
"$timestamp $line\n";
128 # XXX This should handle comments and blank lines
129 # XXX This should convert to lower-case
131 local($hostsfile) = @_;
132 open(HOSTS
, "<$hostsfile") || die "unable to open $hostsfile";
135 chop(@hosts); # remove trailing newline
142 local(%status, $host, $status);
143 if ( -r
$statusfile ) {
144 open(STATUS
, "<$statusfile") || die "unable to open $statusfile";
146 ($host, $status) = m/^([^\s]*) (.*)$/;
147 $status{$host} = $status;
151 open(STATUS
, ">$statusfile") || die "Unable to create $statusfile";
161 open(STATUS
, ">$statusfile") || die "Unable to open $statusfile";
162 foreach $host (keys %status) {
163 print STATUS
"$host $status{$host}\n";
169 local($host, $status, $timestamp) = @_;
170 local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
171 ($opt_l ? localtime($timestamp) : gmtime($timestamp));
174 $~ = ($opt_l ? "HOSTLIST" : "GMTHOSTLIST");
175 $mon = "0".$mon if ($mon < 10);
176 $mday = "0".$mday if ($mday < 10);
177 $min = "0".$min if ($min < 10);
178 $sec = "0".$sec if ($sec < 10);
183 @<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< GMT
@<<<-@<-@< @>:@>:@>
184 $host, $status, $year, $mon, $mday, $hour, $min, $sec
188 @<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< @<<<-@<-@< @>:@>:@>
189 $host, $status, $year, $mon, $mday, $hour, $min, $sec
194 sub convert_print_logfile
{
197 open(LOGFILE
, "< $filename") || die "Unable to open logfile $filename";
199 ($timestamp, $host, $status) = m/^(\d*) ([^\s]*) ([^\s]*)$/;
200 write_hostline
($host, $status, $timestamp);