Hi monks,
I was trying to write a snippet to ping multiple hosts(150) (fork implementation).
I want to store the min/avg/max ping response for each host in a file and if there is no response print NULL for that host.(along with gmt timestamps irrespective of the current machines zone settings)
2 ways i came across:
a)Article by merlyn dealing with the same.
b)Parallel::forkmanager
I read and used merlyn's article and came up with the following
#!/usr/bin/perl use strict; use warnings; my $timestamp=(`date -u +%Y-%m-%d,%T`); chomp($timestamp); print "$timestamp"; open(FI,">forkdata") || die ("Could not open file:$!"); sub ping_a_host { my $host = shift; @ping_output=`ping $host 16 -n 4 2>/dev/null`; if (grep { /round-trip\s+\(ms\)/ ; if ($_ =~ m#min/avg/max\s+=\s ++(\d+)/(\d+)/(\d+)#) {print FI "$host,$timestamp,$1,$2,$3\n"}} @ping +_output){ return 1;} elsif (grep {/0 packets rec/} @ping_output){ print FI "$host,$timestamp,NULL,NULL,NULL\n"; return 0; } } my %pid_to_host; sub wait_for_a_kid { my $pid = wait; return 0 if $pid < 0; my $host = delete $pid_to_host{$pid} or warn("Why did I see $pid ($?)\n"), next; warn "reaping $pid for $host\n"; 1; } my @hosts = map "192.198.192.$_", "221".."251"; for (@hosts) { wait_for_a_kid() if keys %pid_to_host > 10; if (my $pid = fork) { ## parent does... $pid_to_host{$pid} = $_; warn "$pid is processing $_\n"; } else { # child does ## child does... exit !ping_a_host($_); } } ## final reap: 1 while wait_for_a_kid(); close FI;

i get the desired output,but since i tampered with the script i was wondering if i unknowingly broke something or if there is a better way to do this.
code review anyone,any comments are welcome :)(asking to be shot)
regards,
chimni

In reply to Ping multiple hosts (fork implementation) by chimni

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.