#!/usr/bin/perl -w use strict; use LWP::UserAgent; use DBI; use Net::Ping; use MIME::Lite; my $is_prsd_ok = &check_prsd('sample'); my $is_httpd_ok = &check_httpd(); my $is_db_ok = &check_database(); my $is_chat_ok = &check_chat(); my $is_prsd_ok = &check_prsd(); # if any one is not ok then send an email if ( (!$is_httpd_ok) || (!$is_db_ok) || (!$is_chat_ok) || (!$is_prsd_ok) ) { # use MIME::Lite to send an email. my $msg = MIME::Lite->new( From =>'server@example.com', To =>'admin@example.com', Subject =>"Server error report", Type => 'text', Data =>"Server error report for scalar(localtime()), - " . time(), #timestamp?? ); # also add record to monitor.log that email was sent $msg->send(); open (FILE, ">>monitor.log"); print FILE "This is a test\n"; close (FILE); } exit(); # if httpd is up, then status return should be TRUE sub check_httpd { # use LWP to check www.catholicmatch.com my $ua = LWP::UserAgent->new(); my $request = HTTP::Request->new('GET', 'http://www.example.com'); my $response = $ua->request($request); if ($response->is_success()) { # This should eventually be logged - open FILE (">>monitor.log") || die "some error"; print "Website is up and running\n"; return 1; } # This should eventually be logged - open FILE (">>monitor.log") || die "some error"; print "Website is down\n"; return 0; } #sub ping { # my($dbh) = @_; # my $ret = 0; # eval { # local $SIG{__DIE__} = sub { return (0); }; # local $SIG{__WARN__} = sub { return (0); }; # adapt the select statement to your database: # $ret = $dbh->do('select 1'); # }; # return ($@) ? 0 : $ret; # } sub check_database { # use dbi to check sample2 (DBI->ping()) my ($database,$user,$password) = @_; my $dbh = DBI->connect("DBI:mysql:sample2" , undef , undef , {PrintError => 1, RaiseError => 0} ) or db_err("Unable to connect to $database", $DBI::errstr); # return status of running database eval { alarm(5); # seconds before time out $dbh = DBI->ping("dbi:mysql:anne" ,"", ""); alarm(0); # cancel alarm (if connect worked fast) }; alarm(0); # cancel alarm (if eval failed) } sub db_err { my($msg, $errstr) = @_; open (FILE, ">>monitor.log"); print FILE "This is a test\n"; print FILE->$msg; print FILE->($errstr); close (FILE); } # if chat is up, then status return should be TRUE sub check_chat { # use net ping to check sample on port 5678 # my $dbh = ping(); my $pinghost = 'sample.example.com'; my $p = Net::Ping->new("tcp", 2); for (;;) { unless ($p->ping($pinghost)) { print "Fail: ", scalar(localtime), "\n"; return 1; } print "Success: ", scalar(localtime), "\n"; return 0; } } # a better way would be to send this, but ping is okay sub check_prsd { # use net ping to check sample on port 5959 my ($host) = @_; my $p = Net::Ping->new("tcp", 2); $p->{'port_num'} = 5959; if ($p->ping($host, 2)) { print "$host is alive.\n"; return 1; } print "$host is not reachable.\n"; return 0; }