]> pere.pagekite.me Git - homepage.git/blob - netwatch-0.1.pl
Generated.
[homepage.git] / netwatch-0.1.pl
1 #!/store/bin/perl
2 ##
3 # Tests the possibility to reach the hosts listed in config-file
4 #
5 # #author Petter Reinholdtsen
6 # #date 1996-09-05
7 sub about {}
8
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";
13
14 # Send note if this is the first change in # seconds
15 $notelimit = 60*5; # five minutes
16
17 use Getopt::Std;
18
19 ##
20 # return true if 2 or more ICMP ping packets are returnet from $host
21 # OS-depentendent!
22 sub reachable {
23 ($host) = @_;
24 # HP/UX 9.05
25 # open(PING, "/etc/ping $host 64 5 2>&1 |") || die "Unable to start ping";
26 # Irix 5.3
27 # open(PING, "/usr/etc/ping -c 5 $host 2>&1 |") || die "Unable to start ping";
28 # OSF/1, NetBSD
29 open(PING, "/sbin/ping -c 5 $host 2>&1 |") || die "Unable to start ping";
30 while (<PING>) {
31 if (/packets transmitted, (\d*) packets received/) {
32 $packets = $1;
33 }
34 }
35 close(PING);
36 return ($packets > 1);
37 }
38
39 ##
40 # Print out help on usage.
41 sub usage {
42 print <<"EOT";
43 netwatch.pl [-l] [-c <logfile>]
44 -l Print dates in local-time instead of GMT
45 -c Convert logfile to readable format
46 -h Show usage info
47 EOT
48 }
49
50 &getopts('c:lh');
51
52 if ($opt_h) {
53 &usage();
54 exit(0);
55 }
56
57 if ($opt_c) {
58 &convert_print_logfile($opt_c);
59 exit(0);
60 }
61
62 @hosts = &load_hostfile($hostsfile);
63 %status = &load_status;
64
65 %mailinfo = ();
66
67 foreach $host (@hosts) {
68 $status{$host} = "unknown 0 0" if (! $status{$host} );
69 ($oldhoststat, $oldtimestamp, $oldnotifiedstamp) =
70 $status{$host} =~ m/(.*) (.*) (.*)/;
71 $timestamp = time();
72 if ( reachable($host) ) {
73 $hoststat = "up";
74 } else {
75 $hoststat = "down";
76 }
77
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;
84 }
85 } else { # same status, should we report?
86 if ( ($timestamp - $oldnotifiedstamp > $notelimit) &&
87 ($oldtimestamp > $oldnotifiedstamp) ) {
88 $mailinfo{$host} = "$hoststat $oldtimestamp";
89 $notifiedstamp = $timestamp;
90 }
91 }
92
93 if ($oldhoststat ne $hoststat) {
94 $newstatus{$host} = "$hoststat $timestamp $notifiedstamp";
95 &log_line("$host $hoststat");
96 } else {
97 $newstatus{$host} = "$oldhoststat $oldtimestamp $notifiedstamp";
98 }
99 }
100
101 &save_status(%newstatus);
102
103 # Send mail if the link go down and this is the first change in more
104 # then $notelimit seconds.
105
106 if (%mailinfo) {
107 print "Linkstatus endret:\n";
108 foreach $host (sort keys %mailinfo) {
109 ($status, $oldtimestamp) = $mailinfo{$host} =~ m/([^\s]*) (.*)/;
110 write_hostline($host, $status, $oldtimestamp);
111 }
112 }
113
114 ##
115 # record a line to the $logfile
116 # #params $info
117 sub log_line {
118 local($line) = @_;
119 open(LOG, ">>$logfile");
120
121 print LOG "$timestamp $line\n";
122
123 close(LOG);
124 }
125
126 ##
127 # load list of hosts
128 # XXX This should handle comments and blank lines
129 # XXX This should convert to lower-case
130 sub load_hostfile {
131 local($hostsfile) = @_;
132 open(HOSTS, "<$hostsfile") || die "unable to open $hostsfile";
133 @hosts = <HOSTS>;
134 close(HOSTS);
135 chop(@hosts); # remove trailing newline
136 return @hosts;
137 }
138
139 ##
140 # #return %status
141 sub load_status {
142 local(%status, $host, $status);
143 if ( -r $statusfile ) {
144 open(STATUS, "<$statusfile") || die "unable to open $statusfile";
145 while (<STATUS>) {
146 ($host, $status) = m/^([^\s]*) (.*)$/;
147 $status{$host} = $status;
148 }
149 close(STATUS);
150 } else {
151 open(STATUS, ">$statusfile") || die "Unable to create $statusfile";
152 close(STATUS);
153 }
154 return %status;
155 }
156
157 ##
158 # #params %status
159 sub save_status {
160 local(%status) = @_;
161 open(STATUS, ">$statusfile") || die "Unable to open $statusfile";
162 foreach $host (keys %status) {
163 print STATUS "$host $status{$host}\n";
164 }
165 close(STATUS);
166 }
167
168 sub write_hostline {
169 local($host, $status, $timestamp) = @_;
170 local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
171 ($opt_l ? localtime($timestamp) : gmtime($timestamp));
172 $mon++;
173 $year += 1900;
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);
179 write;
180 }
181
182 format GMTHOSTLIST =
183 @<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< GMT @<<<-@<-@< @>:@>:@>
184 $host, $status, $year, $mon, $mday, $hour, $min, $sec
185 .
186
187 format HOSTLIST =
188 @<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< @<<<-@<-@< @>:@>:@>
189 $host, $status, $year, $mon, $mday, $hour, $min, $sec
190 .
191
192 ##
193 #
194 sub convert_print_logfile {
195 ($filename) = @_;
196
197 open(LOGFILE, "< $filename") || die "Unable to open logfile $filename";
198 while (<LOGFILE>) {
199 ($timestamp, $host, $status) = m/^(\d*) ([^\s]*) ([^\s]*)$/;
200 write_hostline($host, $status, $timestamp);
201 }
202 }