summaryrefslogtreecommitdiff
path: root/bin/ping
blob: 605a2047e6a1c79e78b36689a25e2cbd8631dd9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl

use Net::Ping;
use Net::SSH qw( ssh_cmd );
use Email::Send;

my @other_hosts = ( 'freeside.biz', 'saturn5.com' );

my( $machine, @emails ) = @ARGV;
die "no notification email given" unless @emails;

my $ping = new Net::Ping; # 'icmp'; #requires root

my $pong = '';
# can't tcp ping... $ping->ping($machine) and
$pong = eval { ssh_cmd('freeside@'.$machine, 'echo pong') };
#(command ignored if authorized_keys setup w/command=)

if ( $@ || $pong !~ /pong/ ) { #houston, we may have a problem

  #warn "can't reach $machine, checking @other_hosts\n";

  #let's do a sanity check, can we see some other hosts?
  exit unless grep $ping->ping($_), @other_hosts;

  #uh-oh, this is bad.

  #warn "checking to see if we've alerted on this recently\n";

  #but we don't want to be too noisy, have we alerted on this in the last 24h?
  my $file = "/tmp/alert-$machine";
  exit if -e $file && -M $file < 1;

  open(FILE, ">>$file");
  print FILE "emailing\n";
  close FILE;

  #warn "emailing alerts\n";

  foreach my $email ( @emails ) {

    my $message = <<"__MESSAGE__";
From: support\@freeside.biz
To: $email
Subject: ALERT - $machine

ALERT: $machine appears to be down.

__MESSAGE__

    my $sender = Email::Send->new({mailer => 'SMTP'});
    $sender->mailer_args([Host => 'mail.freeside.biz']);
    $sender->send($message);

  }

}