#!/usr/local/bin/perl -w # # A quick and simple script that will reset a machine if it # cannot contact the $host for more than 10 minutes. To # reset the machine, this script will have to be run as # root. use Net::Ping; # this modules is available at: # http://www.perl.com/CPAN-local/modules/by-module/Net/ # the current version is Net-Ping-2.02.tar.gz # host to ping $host='200.100.50.25'; # where to write error messages $logFile='/var/log/ping.log'; # seconds to wait between pings $sleepTime=60; # maximum ping failures $maxErrors=10; # command to run when we reach maximum failures $theCommand="/sbin/shutdown -r now"; # monitor loop: $err=0; logMessage("pingAlive starting"); while (1) { # see "man Net::Ping" for more info. $p = Net::Ping->new(); # send a udp ping packet to host, with 10 second timeout unless ($p->ping($host, 10)) { # ping failed! $err++; logMessage("Ping ($err) to $host failed"); } elsif ($err) { # regained contact logMessage("Successfully reached $host after $err failures"); $err=0; } $p->close(); if ($err >= $maxErrors) { logMessage("Too many failures. Taking action: $theCommand"); system($theCommand); $err=0; } sleep $sleepTime; } # writes a line to the log file sub logMessage { unless (open F, ">>$logFile") { warn "(Note: Cannot write to $logFile)\n"; open F, ">&STDERR"; } print F scalar(localtime)." ".join(" ",@_)."\n"; close F; }