#!/usr/bin/perl -w use strict; use Getopt::Long; my $host_file = "$ENV{HOME}/etc/hostlist.monitor"; my $nc = '/usr/pkg/sbin/nc'; my $mail_host = 'localhost'; my @mail_recip = (); my $debug = 0; GetOptions('file=s' => \$host_file, 'debug' => \$debug, 'mailhost=s' => \$mail_host, 'recipients=s' => \@mail_recip); @mail_recip = split(/,/, join(',', @mail_recip)); @mail_recip = qw(change@me.dom) if ($#mail_recip < 0); open HFILE, "<$host_file"; my ($host, $port, $send, $recv, $state); my $report = ''; while () { chomp($_); ($host, $port, $send, $recv) = split(':', $_); chomp($state = `/bin/echo $send | $nc $host $port`); print "[$state][$recv]\n" if $debug; if ($state ne $recv) { $report .= "Services at port $port on $host seem to be down.\n"; } } close HFILE; if ($report) { use Net::SMTP; if ($debug) { print $report; exit 1; } my $smtp = Net::SMTP->new($mail_host); chomp(my $mail_subject = `basename $0`); $mail_subject .= " report at " . localtime; my $from = (split('\(', (split('\)', `id`))[0]))[1] . '@' . `hostname`; $smtp->mail($from); foreach (@mail_recip) { $smtp->recipient($_); } $smtp->data(); $smtp->datasend("To: " . join(', ', @mail_recip) . "\n"); $smtp->datasend("Subject: $mail_subject\n\n"); $smtp->datasend("There appears to be a system outtage problem:\n\n"); $smtp->datasend($report); $smtp->datasend("\nComputer operator, please page the Unix sysadmin.\n"); $smtp->dataend; $smtp->quit; }