eventping.in 2.81 KB
Newer Older
Mac Newbold's avatar
Mac Newbold committed
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Mac Newbold's avatar
Mac Newbold committed
23 24 25 26 27 28 29 30 31 32
#

# 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@";

33 34
sub fatal ( $ );

Mac Newbold's avatar
Mac Newbold committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
$| = 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;
56
my $noresolve = 0;
Mac Newbold's avatar
Mac Newbold committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

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
74
	$noresolve = 0;
Mac Newbold's avatar
Mac Newbold committed
75 76
    } elsif ($rv==68) {
	# ping: cannot resolve $node: Unknown host
77 78 79 80 81 82
	# Lets view this as a transient error, since named_setup might
	# be running for a swap.
	$noresolve++;
	if ($noresolve > 10) {
	    fatal("eventping: cannot resolve $node: Unknown host\n");
	}
Mac Newbold's avatar
Mac Newbold committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    } 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);
}