Hi!!!
  I have written this script (see below) to check when the server or database 
is not running...but I can not seem to work around the error it is giving. The 
result it gave me is:
  "Website is up and running
Can't connect to data source"
   
   
  #!/usr/bin/perl
  use strict;
use LWP::UserAgent;
use DBI;
use Net::Ping;
use MIME::Lite;
  
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     =>'[EMAIL PROTECTED]',
                 To       =>'[EMAIL PROTECTED]',
                 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.example.com
    my $ua = LWP::UserAgent->new();
    my $request = HTTP::Request->new('GET', 'http://www.example.com');
    my $response = $ua->request($request);
    
    
  # if database is up, then status return should be TRUE
  if ($response->is_success()) {
        # This should eventually be logged -  open FILE (">>monitor.log") || 
die "some error";
        print "Website is up and running\n";
    }
    else {
        # This should eventually be logged -  open FILE (">>monitor.log") || 
die "some error";
        print "Website is down\n";
        return 0;
    }
      return 1;
}
   
   
  sub check_database {
      # use dbi to check anne (DBI->ping())
    my $dbh = DBI->connect()  #need to rectify error message problem
            or die "Cannot connect to database! Error found: $DBI::errstr"; 
       $dbh->ping();
     
 
#    my $dbh;
#   eval {
#    alarm(5); # seconds before time out
#   $dbh = DBI->connect("dbi:Oracle:$dsn" ... );
#  alarm(0); # cancel alarm (if connect worked fast)
#     };
# alarm(0);    # cancel alarm (if eval failed)
      
    # return status of running database
  
if ($dbh-> is_success())
    {
         warn "database is up";
        
    }
    else
    {
        warn 'database is not connected [$ping_val]\n';
}
  
# if chat is up, then status return should be TRUE
sub check_chat {
   
   
  # use net ping to check emily on port 4674
  }
  sub check_prsd {
   
   
   # use net ping to check emily on port 2323
      
}
}
   

                
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.  Great rates 
starting at 1¢/min.

Reply via email to