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


In reply to Script to monitor by onomejiro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.