#!/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 Page # 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 been 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", $target, "SLOW", "Response $time seconds"; } else { $out_format = sprintf "| %-50.50s %-10s %-20s |\n", $target, "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", $target, "DOWN", " N/A"; push(@errors, "$target is DOWN." . $res->status_line) or error("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; }