#! /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,\$http +s,\$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 <nobody\@${hostname}>\n"; print SENDMAIL "To: SysAdmin <someone\@somewhere.com>\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 $hostnam +e\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 <nobody\@${hostname}>\n"; print SENDMAIL "To: SysAdmin <someone\@somewhere.com>\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 $hostnam +e\n"; print SENDMAIL "$proto: $rserver cannot be contacted!\n"; close(SENDMAIL) or warn "Oops, sendmail did not close nicely"; }

In reply to Service Health Scanner by penguinfuz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.