#!/opt/local/bin/perl5.005
# OCF Web-ruptime script. Copyright Stephen McCamant 2001,2003
# All rights reserved; This program may be distributed under the same terms
# as perl itself.
#@hp_xterms = ("tempest", "tsunami", "whirlwind");
#@oldsparcs = ("sandstorm", "hailstorm");
@south_ultras1 = qw(firestorm sandstorm blizzard);
@south_ultras2 = ("hurricane", "fallingrocks", "monsoon");
@south_row = ("firestorm", "sandstorm", "pileup", "blizzard", @south_ultras2);
@north_ultras = qw(whirlwind tornado flood);
@south_gways = qw(pox plague outbreak madcow locusts);
@north_gways = qw(anthrax biohazard blight epidemic jaws);
@west_dells = qw(volcano earthquake tsunami typhoon tempest cataclysm);
@OCFs = ("apocalypse", "famine", "conquest");
@services = ("war", "death", "pestilence", "supernova");
@lab_ultras = (@south_ultras1, @south_ultras2, @north_ultras);
@rwhoables = (@lab_ultras, @OCFs, @services);
@windows = qw(anthrax biohazard blight epidemic jaws
locusts madcow outbreak plague pox
pileup
volcano earthquake tsunami typhoon tempest cataclysm);
#%dual_boot = qw(locusts windows plague windows);
%dual_boot = qw();
@allhosts = (@windows, @rwhoables);
if (-o "/tmp/spool") {
$spoolfile = "/tmp/spool/fingerdir/rwho/whod";
} else {
$spoolfile = "/var/spool/fingerdir/rwho/whod";
}
use CGI;
use Net::Ping;
use OCF::FingerInfo;
$pinger = Net::Ping->new("tcp", 1);
$pinger->{"port_num"} = 139;
$q = new CGI;
$table = $q->param("table");
print $q->header, $q->start_html(-title => "OCF machine status",
-BGCOLOR => "#ffffff");
$sep = $table ? "
" : "";
%descr =
(
"fallingrocks" => "Ultra 5$sep 270MHz",
"avalanche" => "Ultra 5$sep 270MHz",
"mudslide" => "Ultra 5$sep 270MHz",
"sandstorm" => "Blade 100$sep 500MHz",
"hailstorm" => "Blade 100$sep 500MHz",
"blizzard" => "Blade 100$sep 500MHz",
"whirlwind" => "Blade 100$sep 500MHz",
"tornado" => "Blade 100$sep 500MHz",
"flood" => "Blade 100$sep 500MHz",
"hurricane" => "Blade 100$sep 500MHz",
"monsoon" => "Blade 100$sep 500MHz",
"volcano" => "Dell Dimension 2400$sep 2+ GHz",
"earthquake" => "Dell Dimension 2400$sep 2+ GHz",
"tsunami" => "Dell Dimension 2400$sep 2+ GHz",
"typhoon" => "Dell Dimension 2400$sep 2+ GHz",
"tempest" => "Dell Dimension 2400$sep 2+ GHz",
"cataclysm" => "Dell Dimension 2400$sep 2+ GHz",
"pileup" => "Apple iMac",
"locusts" => "Gateway Athlon$sep 1GHz",
"plague" => "Gateway Athlon$sep 1GHz",
"anthrax" => "Gateway Athlon$sep 1GHz",
"biohazard" => "Gateway Athlon$sep 1GHz",
"blight" => "Gateway Athlon$sep 1GHz",
"epidemic" => "Gateway Athlon$sep 1GHz",
"jaws" => "Gateway Athlon$sep 1GHz",
"madcow" => "Gateway Athlon$sep 1GHz",
"outbreak" => "Gateway Athlon$sep 1GHz",
"pox" => "Gateway Athlon$sep 1GHz",
"apocalypse" => "Ultra 1$sep 167MHz",
"famine" => "Ultra 1$sep 167MHz",
"conquest" => "Ultra 1$sep 167MHz",
"war" => "Blade 100$sep 500 MHz",
"death" => "Sun Enterprise 250$sep 2 x 400Mhz",
"pestilence" => "Ultra1$sep 167MHz",
"supernova" => "Ultra1$sep 167MHz",
);
$now = time;
sub nice_time {
my($secs) = @_;
my $ret = "";
my $mins = $secs / 60;
if ($mins > 24*60) {
my $days = int($mins/(24*60));
$ret = ($days == 1) ? "1 day, " : "$days days, ";
$mins %= 24*60;
}
$ret .= sprintf("%d:%02d", int($mins / 60), $mins % 60);
return $ret;
}
sub nice_users {
my($users) = @_;
if ($users == 0) {
return "no users";
} elsif ($users == 1) {
return "1 user";
} else {
return "$users users";
}
}
for $h (@windows) {
if ($pinger->ping($h)) {
$state{$h} = "0";
$text{$h} = $table ? "up | Windows users" : "is up";
} else {
$state{$h} = "down";
$text{$h} = $table ? "down | no users" : "is down";
}
}
for $d (OCF::FingerInfo->hostdata_real) {
if ($d->ttyname eq "dtremote" and $d->ttyloc =~ /^>(\w+):0$/) {
$state{$1} = "con";
$text{$1} = $table ? "up | 1 user" : "has 1 user";
}
}
for $h (@rwhoables) {
$state{$h} = -M "$spoolfile.$h" < 1/3000 ? "up" : "down";
if ($state{$h} eq "up" and -e "$spoolfile.$h") {
open(FILE, "<$spoolfile.$h");
read(FILE, $data, 1+1+2+4+4+32+12+4);
($vers, $type, $sendt, $recvt, $hostname, @loadavg[0,1,2], $boott)
= unpack("CCxxNNa32NNNN", $data);
$users = 0;
while (read(FILE, $data, 8+8+4+4)) {
($tty, $login, $time, $idle) = unpack("a8a8NN", $data);
$users++ if $idle <= 60*60;
}
close FILE;
$text{$h} = "up " . nice_time($now - $boott);
$text{$h} .= $table ? " | " : " ";
$text{$h} .= nice_users($users);
$text{$h} .= " | " if $table;
$text{$h} .= " load " . sprintf("%.2f", @loadavg[0] / 100);
$state{$h} = $users ? "con" : "0";
} elsif ($dual_boot{$h} and $pinger->ping($h)) {
$state{$h} = "win";
$text{$h} = $table ? "up | running Windows" : "running Windows";
} else {
$text{$h} = "is down";
}
}
sub show_row {
my($header, @hosts) = @_;
if ($table) {
print " | | ";
print "$header:" if $header;
print " | ";
for $h (@hosts) {
print qq[ ];
}
print " |
\n";
for $h (@hosts) {
print qq[| $h | ];
print qq[$descr{$h} | $text{$h} |
\n];
}
} else {
print "$header:
\n" if $header;
print "";
for $h (@hosts) {
print qq[ ];
print qq[$h ];
print qq[$descr{$h} $text{$h} | \n];
}
print "
\n";
}
}
if ($table) {
print "\n";
show_row("Lab Workstations", @south_ultras1, "pileup");
show_row("", @south_ultras2);
show_row("", @west_dells);
show_row("", @north_ultras);
show_row("", @south_gways);
show_row("", @north_gways);
show_row("Login Servers", @OCFs);
show_row("Other Servers", @services);
print "
\n";
} else {
show_row("Lab Workstations", @south_row);
show_row("", @west_dells);
show_row("", @north_ultras);
show_row("", @south_gways);
show_row("", @north_gways);
show_row("Servers", @OCFs);
show_row("", @services);
}
print $q->end_html;