eventping.in 1.95 KB
Newer Older
Mac Newbold's avatar
Mac Newbold committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

# eventping - ping a node until it is reachable, then send ISUP event

# Configure variables
use lib '@prefix@/lib';
my $TB = "@prefix@";
my $BOSSNODE = "@BOSSNODE@";
my $TBOPS = "@TBOPSEMAIL@";

16 17
sub fatal ( $ );

Mac Newbold's avatar
Mac Newbold committed
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
$| = 1;

use event;
use libdb; # event constants
use libtestbed; # sendmail

sub usage {
    fatal("Usage: eventping <node>\n".
	  "Ping node until reachable, then send ISUP event.\n");
}

my $cmdline = "$0 ".join(" ",@ARGV);
if (@ARGV != 1) { usage(); }

my $d = 0;

my $node = shift;

my $maxtime=600;  # Set the timer for 10 minutes
my $starttime = time();
my $endtime = $starttime + $maxtime;

while( time() <= $endtime ) {
    my $status=system("/sbin/ping -c 1 -t 1 $node ".
		      "> /dev/null 2>&1 > /dev/null");
    my $rv = $status >> 8;
    my $sig = $status & 127;
    debug("ping returned $status ($rv / $sig)\n");
    if ($rv==0) {
	# we got a response
	EventSendFatal(host      => $BOSSNODE ,
		       objtype   => TBDB_TBEVENT_NODESTATE ,
		       eventtype => TBDB_NODESTATE_ISUP ,
		       objname   => $node);
	debug("Sent event ". TBDB_NODESTATE_ISUP ." for $node\n");
	exit(0);
    } elsif ($rv==2) {
	# no response
    } elsif ($rv==68) {
	# ping: cannot resolve $node: Unknown host
	fatal("eventping: cannot resolve $node: Unknown host\n");
    } else {
	# Unknown error
	fatal("eventping: ping returned unknown error $rv ($sig)\n");
    }
    # sleep just a little bit so we can catch a ^C while debugging
    if ($d) { select(undef,undef,undef,0.5); }
}

sub debug { if ($d) { print @_; } }

sub fatal ( $ ) {
  my $msg = shift;
  notify("FATAL: ".$msg);
  die($msg);
}

sub notify ( $ ) {
  my $msg = shift;
  $msg .= "\ndate=".`date`."\ncmdline=\n$cmdline\n\npid=$$\n\n";
  if (!$d) {
    SENDMAIL($TBOPS,"eventping failure",$msg,$TBOPS);
  } else {
    debug("notify: Not sending mail in debug mode\n");
  }
  debug($msg);
}