better message catalog editing
[freeside.git] / bin / ping
1 #!/usr/bin/perl
2
3 use Net::Ping;
4 use Net::SSH qw( ssh_cmd );
5 use Email::Send;
6
7 my @other_hosts = ( 'freeside.biz', 'saturn5.com' );
8
9 my( $machine, @emails ) = @ARGV;
10 die "no notification email given" unless @emails;
11
12 my $ping = new Net::Ping; # 'icmp'; #requires root
13
14 my $pong = '';
15 # can't tcp ping... $ping->ping($machine) and
16 $pong = eval { ssh_cmd('freeside@'.$machine, 'echo pong') };
17 #(command ignored if authorized_keys setup w/command=)
18
19 if ( $@ || $pong !~ /pong/ ) { #houston, we may have a problem
20
21   #warn "can't reach $machine, checking @other_hosts\n";
22
23   #let's do a sanity check, can we see some other hosts?
24   exit unless grep $ping->ping($_), @other_hosts;
25
26   #uh-oh, this is bad.
27
28   #warn "checking to see if we've alerted on this recently\n";
29
30   #but we don't want to be too noisy, have we alerted on this in the last 24h?
31   my $file = "/tmp/alert-$machine";
32   exit if -e $file && -M $file < 1;
33
34   open(FILE, ">>$file");
35   print FILE "emailing\n";
36   close FILE;
37
38   #warn "emailing alerts\n";
39
40   foreach my $email ( @emails ) {
41
42     my $message = <<"__MESSAGE__";
43 From: support\@freeside.biz
44 To: $email
45 Subject: ALERT - $machine
46
47 ALERT: $machine appears to be down.
48
49 __MESSAGE__
50
51     my $sender = Email::Send->new({mailer => 'SMTP'});
52     $sender->mailer_args([Host => 'mail.freeside.biz']);
53     $sender->send($message);
54
55   }
56
57 }
58