#! /usr/bin/perl -w # monitor http,https,smtp on remote servers. use strict; use LWP::Simple; my ($http,$https,$smtp); chomp (my $hostname = `/bin/uname -n`); $http = { "ja.hallolucy.net" => 'http://ja.hallolucy.net', "www.coolwebsite.com" => 'http://www.coolwebsite.com', "notsocoolwebsite.com" => 'http://notsocoolwebsite.com', }; $https = { "eat.atjoes.org" => 'https://eat.atjoes.org', "secure.bigtopstuff.com" => 'https://secure.bigtopstuff.com', "www.iliketoshophere.com" => 'https://www.iliketoshophere.com', }; $smtp = { "mail.atsomewhere.com" => 'mail.atsomewhere.com', "imap.overheretoo.net" => 'imap.overheretoo.net', }; resolve_host($http,$https,$smtp); # -------------------------------- # SUBROUTINES # -------------------------------- sub resolve_host { my (@hosts) = @_; my (%records,$record,$notfound,$matches,$proto,$count); $notfound = '\s{1}not\s{1}found\:'; $matches = '^(:?\S+).*?(\d+\.\d+\.\d+\.\d+)$'; $count = @hosts; for $proto (@hosts) { $count--; for (keys %{$proto}) { chomp($record = `/usr/bin/host $_`); if ($record =~ /$notfound/o) { email_resolution_error($_,$hostname); } else { if ($count == 0) { scan_mail($_,$2,'[SMTP]') if $record =~ /$matches/o; } elsif ($count == 1) { scan_web($proto->{$_},'[SSL]'); } elsif ($count == $#hosts) { scan_web($proto->{$_},'[WEB]'); } else { die "Was args passed proper? [ex: resolve_host(\$http,\$https,\$smtp)]: $!\n"; } } } } } sub scan_web { my ($hosts,$proto) = @_; email_connection_error($_,$proto,$hostname) unless head($hosts); } sub scan_mail { my ($rserver,$ip,$proto) = @_; my $port = "25"; my $result; $result = `/usr/bin/nmap -P0 -p$port -oG - $ip | grep 'Status: Up'`; email_connection_error($rserver,$proto,$hostname) if $result; } sub email_resolution_error { my ($rnode,$hostname) = @_; open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Cannot fork for sendmail: $!\n"; print SENDMAIL "From: multimon SOS \n"; print SENDMAIL "To: SysAdmin \n"; print SENDMAIL "Cc: emergency\@mobilepager.com\n"; print SENDMAIL "Subject: [DNS]: $rnode cannot be resolved!\n"; print SENDMAIL "\n"; print SENDMAIL "Email generated by the multimon script on $hostname\n"; print SENDMAIL "[DNS]: $rnode cannot be resolved!\n"; close(SENDMAIL) or warn "Oops, sendmail did not close nicely"; } sub email_connection_error { my ($rserver,$proto,$hostname) = @_; open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Cannot fork for sendmail: $!\n"; print SENDMAIL "From: multimon SOS \n"; print SENDMAIL "To: SysAdmin \n"; print SENDMAIL "Cc: emergency\@mobilepager.com\n"; print SENDMAIL "Subject: $proto: $rserver cannot be contacted!\n"; print SENDMAIL "\n"; print SENDMAIL "Email generated by the multimon script on $hostname\n"; print SENDMAIL "$proto: $rserver cannot be contacted!\n"; close(SENDMAIL) or warn "Oops, sendmail did not close nicely"; }