Want to monitor a DB server for up or down status but need something more that "ping". The idea is to remotely run a query and analyze the result. If the DB server hangs the monitoring script must be able to timeout and alert about the problem. This is what I am trying but my simulated "hung" goes unnoticed to the monitoring script which hangs itself as well. Looking for suggestions. FYI: this is my very first experience with IPC.
#!/usr/bin/perl use strict; use warnings; use IPC::Open2; use IO::Select; # for select #my $cmd = "sqlplus -S apps/******"; # This is the query I really want # to run my $cmd = "ssh oraprod\@remote_server './x 100'"; # "x" executes # sleep $1 to # simulate a process # hunging on the # remote server my $timeout=1; my $infh; my $outfh; my @output; my $pid; eval{ $pid = open2($outfh, $infh, $cmd); }; die "open2: $@\n" if $@; print "PID was $pid\n"; #-------------------------- DEBUG ------------ #print "Timeout was $timeout\n"; my $sel = new IO::Select; # create a select object to notify # us on write ready. $sel->add($infh); # add the std input file handle if ($sel->can_write($timeout)){ # I will base my "DB alive" test off of this query's output #print $infh "set feedback off pages 0\nselect sysdate from dual;\n"; } else { die "Timed out while waiting for the \"Write OK\" to send the query +\n"; } print "PID was $pid\n"; #-------------------------- DEBUG ------------ $sel->add($outfh); # add the std output file handle while ($sel->can_read($timeout)) { # Returns the std output file # handle even while the sleep is # being x. # Would it do the same if "x" were # a truly hung process? # My hope is it won't so the # while's condition would be false # after timeout causing the script # to skip the while. chomp(my $line=<$outfh>); # There is nothing on "x" std output to # be read so $line does not get defined. push(@output,$line); } close $infh; print "PID was $pid\n"; #-------------------------- DEBUG ------------ waitpid $pid,0; $sel->remove($infh,$outfh); # remove std output f handle from the list unless (@output) { die "Timed out while waiting for query output\n"; } print "$_\n" for @output;

In reply to heartbeat script. Hung-server proof. by ecarceller

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.