onomejiro has asked for the wisdom of the Perl Monks concerning the following question:

I am really new to perl but I am willing to learn. I have to write a perl script to monitor the web server ( and considering chat as well), and database server. The OS for all is Linux and the db is MySQl (/PHP). Now, I have drafted out something from a lot of ideas I have gathered around but it is still not making the expected result...Please please help me. Below is the script:
#!/usr/bin/perl use strict: # my_history =( # 'httpd_alert_time' => 0, # 'httpd_alive_time' => 0, # # # # ); sub check_url { # subroutine who check given URL my $target = $_[0]; my $ua = LWP::UserAgent->new; $ua->agent("$0/0.1 " . $ua->agent); my $req = HTTP::Request->new(GET => "$target"); $req->header('Accept' => 'text/html'); #Accept HTML P +age # send request my $start = time; # Start timer my $res = $ua->request($req); # check the outcome if ($res->is_success) {# Success....all content of page has be +en received my $time = time; # End timer my $out_format; $time = ($time - $start); # Result of timer if ($response_limit && ($response_limit <= $time)) { push(@errors, "Slow response from $target\: $time seconds +"); $out_format = sprintf "| %-50.50s %-10s %-20s |\n", $targ +et, "SLOW", "Response $time seconds"; } else { $out_format = sprintf "| %-50.50s %-10s %-20s |\n", $targ +et, "ACCESSED", "Response $time seconds"; } print OUT $out_format; # write to file print $out_format; # print to console } else { # Error .... Site is DOWN and script +send e-mail to you.. my $out_format = sprintf "| %-50.50s %-10s %-20s |\n", $targ +et, "DOWN", " N/A"; push(@errors, "$target is DOWN." . $res->status_line) or err +or("Cannot push error for DOWN"); print OUT $out_format; # write to file print $out_format; # print to console } } dbmopen(%history, '/home/onome/monitor', 0666); my $alive = %check_httpd(); if ($alive) { $history{'httpd_alive_time'} = time(); } else { my $dead_time = time()- $history{'httpd_alive_time'}; if ($dead_time> 30) { %send_alert("httpd has been dead for $dead_time seconds"); } } dbmclose(%history); # if httpd is up, then status return should be TRUE sub check_httpd { my $alive = rand(10); return ($alive < 1) ? 0: 1; } # if database is up, then status return should be TRUE sub check_database { my $alive = rand(10); return ($alive < 1) ? 0: 1; } # if chat is up, then status return should be TRUE sub check_chat { my $alive = rand(10); return ($alive < 1) ? 0: 1; }

Code tags fixed by GrandFather

Replies are listed 'Best First'.
Re: Script to monitor
by davidrw (Prior) on Mar 30, 2006 at 17:37 UTC
    but it is still not making the expected result
    what is the expected result? what is the observed result?

    a couple observations:
    • use warnings; in addition to use strict;
    • LWP::Simple can make the code a little more, well, simple ;) (at a glance, i don't see anything that requires LWP::UserAgent directly (oh, now i see the user agent string change...)
      use LWP:::Simple; my $res = get( $target ); if( is_success($res) ){ ... }else{ ... }
    • where is check_url() called from? Seems like it should return a boolean based on the result of the check.
    • where is $response_limit defined?
    • can avoid some repetativeness, since the printf format is always the same:
      sub check_url { # subroutine who check given URL my $target = shift; my $response_limit = shift; my $time = time; # Start timer my($result, $rc, $error); my $res = get( $target ); if( is_success($res) ){ $time = sprintf "Response %d seconds", (time - $time); if ($response_limit && ($time >= $response_limit)) { # note + bugfix of direction of inequality $error = "Slow response from $target\: $time seconds"; ($rc, $result) = (-1, "SLOW"); } else { ($rc, $result) = (1, "ACCESSED"); } } else { # Error .... Site is DOWN and script send + e-mail to you.. ($rc, $result, $time) = (0, "DOWN", "N/A"); $error = "$target is DOWN." . $res; } push @errors, $error if defined $error; my $out_format = "| %-50.50s %-10s %-20s |\n"; printf OUT $out_format, $target, $result, $time; printf $out_format, $target, $result, $time; return $rc; }
Re: Script to monitor
by jhourcle (Prior) on Mar 30, 2006 at 18:54 UTC

    I know there's the not-made-here problem, but there are a number of good monitoring programs out there that are free (or free under specific conditions), including Big Brother and Nagios, MRTG and various tools that use RRDTool that would prevent you from needing to write and maintain your own.

    (and for those wishing to interact with the data in perl, there are BigBrother, nagios, MTRG and RRD/RRDTool modules in CPAN)