These two Perl scripts fork to the background and call each other in turn to help track important systems.
-------------------------- ping2failure --------------------
#!/usr/bin/perl
#
$MAIL_CMD = "/bin/mail";
$PING = "/bin/ping";
$dofork = 1;
$target = $ARGV[0];
die "No target.\n" if ($target eq "");
if ($dofork) {
if (open(PS, "/bin/ps ax | /bin/grep \"ping2failure $target\" | /bin/grep -v grep |")) {
@psax = <PS>;
close(PS);
#print "@psax";
for(@psax) {
s/^\s+//g;
#print $_;
if (/ping2failure $target/) {
#print "$_";
($pid, @stuff) = split(/\s+/);
#print "$$ pid is $pid and $stuff[0]\n";
if ($pid != $$) {
die "$target already being waited on\n";
# no reason to run if already doing this
}
}
}
}
# deal with stdin, stdout, stderr, control terminal, and put self
# in background
$TIOCNOTTY = 0x20007471;
close(stdin);
open(stdout, ">/dev/null");
open(stderr, ">/dev/null");
if (open(tty, "/dev/tty")) {
ioctl(tty, $TIOCNOTTY, 0);
close(tty);
}
if (fork) {
exit 0;
}
}
$pings = 0;
$go = 1;
while ($go) {
$xrand = system("/usr/bin/shuf -i 7-25 -n1");
chomp($xrand);
sleep($xrand);
open(DOPING,"$PING -c 5 -W 45 $target |");
@hello = <DOPING>;
close(DOPING);
for (@hello) {
chomp;
$x = $_;
if ($x =~ /0 received/) {
open(DOPING,"$PING -c 5 -W 45 $target |");
@hello2 = <DOPING>;
close(DOPING);
for (@hello2) {
chomp;
$x = $_;
if ($x =~ /0 received/) {
$go = 0;
}
}
}
}
$pings++;
}
#system("/usr/bin/logger -p local1.emerg ping2failure $target is now off-line");
system("/usr/bin/logger -p local1.emerg ping2failure $target is now off-line ");
system("/usr/bin/wall ping2failure $target is now off-line ");
------------------ wait4ping ---------------------------
#!/usr/bin/perl
#
$MAIL_CMD = "/usr/bin/mail";
$PINGCMD = "/usr/bin/ping";
$target = $ARGV[0];
die "No target.\n" if ($target eq "");
if (open(PS, "/bin/ps ax | /bin/grep \"wait4ping $target\" | /bin/grep -v grep |")) {
@psax = <PS>;
close(PS);
#print "@psax";
for(@psax) {
s/^\s+//g;
#print $_;
if (/wait4ping $target/) {
#print "$_";
($pid, @stuff) = split(/\s+/);
#print "$$ pid is $pid and $stuff[0]\n";
if ($pid != $$) {
die "$target already being waited on\n";
# no reason to run if already doing this
}
}
}
}
# deal with stdin, stdout, stderr, control terminal, and put self
# in background
$TIOCNOTTY = 0x20007471;
close(stdin);
open(stdout, ">/dev/null");
open(stderr, ">/dev/null");
if (open(tty, "/dev/tty")) {
ioctl(tty, $TIOCNOTTY, 0);
close(tty);
}
if (fork) {
exit 0;
}
$pings = 0;
while (1) {
open(DOPING,"$PINGCMD -c 1 -W 45 $target |");
@hello = <DOPING>;
close(DOPING);
last if ($hello[4] =~ /\,\s+1\s+received\,/);
$pings++;
$xrand = system("/usr/bin/shuf -i 17-37 -n1");
chomp($xrand);
sleep($xrand);
}
#system("/usr/bin/logger -p local1.emerg wait4ping $target is now on-line");
system("/usr/bin/logger -p local1.emerg wait4ping $target is now on-line ");
system("/usr/bin/wall wait4ping $target is now on-line ");
No comments:
Post a Comment