Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Service Health Scanner

by penguinfuz (Pilgrim)
on May 19, 2002 at 11:23 UTC ( #167633=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info penguinfuz at gmail dot com
Description: Basically the script first tries to resolve the domain name, if it failes you get email notification telling you which domain could not be resolved. If the domain resolves ok, the domain is then scanned for whatever service is specified, if this fails you recieve a notification of which domain is having trouble.

The subject line of the email notifications have been tailored for output to a mobile telephone and/or pager.

UPDATE: Complete rewrite, still checking for DNS resolution before moving forward, but now using LWP::Simple to check web/SSL connectivity, and email notifications are more user-friendly in that the failed service and hostname is listed in the SOS.
#! /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";
}

Comment on Service Health Scanner
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://167633]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-08-22 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls