0: #!/usr/bin/perl -w
1: # pinger for AGate200 link
2:
3: use strict;
4: use POSIX;
5: use Net::Ping;
6: use Net::Telnet;
7:
8: my $host = '192.168.1.1'; # other end of the link
9: my $agate = '192.168.1.2'; # AGate to (try to) reset
10: my $admin = 'admin@domain.org'; # someone who cares
11: my $maxfail = 3; # his patience
12: my $pause = 60; # secs to sleep between pings
13: my $fail = 0; # consider it pingable apriori
14: my $log = '/var/log/pinger.log'; # log file
15:
16: my $pid = fork;
17: exit if $pid;
18: die "Couldn't fork: $!" unless defined($pid);
19: POSIX::setsid() or die "Can't start a new session: $!";
20: my $time_to_die = 0;
21: $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;
22:
23: open (LOG, "> $log") or die "Can't open logfile: $!\n";
24: select(LOG);
25: $|++;
26: print scalar localtime, " Started succesfully\n";
27:
28: my $ping = Net::Ping->new("icmp") or die "Can't create new ping: $!\n";
29:
30: until ($time_to_die) {
31: if ($ping->ping($host)) {
32: # print scalar localtime, " Ping $host: success\n";
33: $fail = 0;
34: } else {
35: print scalar localtime, " Ping $host: fail\n";
36: $fail++;
37: }
38: if ($fail == $maxfail) {
39: my $result = fix();
40: $result ||= 'success';
41: print scalar localtime, " Reset: $result\n";
42: notify($result);
43: }
44: if ($fail > $maxfail) {
45: fix();
46: }
47: sleep($pause);
48: }
49:
50: sub signal_handler {
51: $ping->close();
52: print scalar localtime, " Exiting\n";
53: close LOG;
54: $time_to_die = 1;
55: }
56:
57: sub notify {
58: my $result = shift;
59: my $minutes = ($maxfail*$pause)/60;
60:
61: open(MAIL, "| mail -s 'Wake up!' $admin") or die "Couldn't fork: $!\n";
62: print MAIL <<MESSAGE;
63:
64: $host is unpingable more than $minutes minutes!
65: Result of the reset: $result.
66: Examine the situation ASAP!
67:
68: Love, Pinger
69: MESSAGE
70:
71: close(MAIL) or die "Couldn't close: $!\n";
72: print scalar localtime, " Notification sent: $admin\n";
73: }
74:
75: sub fix {
76: print scalar localtime, " Trying to reset: $agate\n";
77: my $t = Net::Telnet->new(Errmode => "return");
78: $t->open($agate);
79: $t->waitfor('/A Choice :/');
80: $t->print('9');
81: $t->waitfor('/reenter.*$/');
82: $t->print('');
83: $t->waitfor('/Password :/');
84: $t->print('figatebe');
85: $t->waitfor('/\(y\/n\):/');
86: $t->print('y');
87: $t->close($agate);
88: return $t->errmsg;
89: }
In reply to Ping and reset (AGate200) by Kolyan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |