title: Perl code re-engineering with multithreading for continuous Ping records generated per host
body: <p>A very happy new year to all :D</p>
  
  <p>I have written a piece of code in perl which works fine but I am looking foreward to enhance that code exploiting features like Multithreading and process forking.
  Code is designed to work for the followin tasks:</p>
  
  <ol>
  <li>Continuosly record ping for several servers provided in list.</li>
  <li>MAintain and write logs for each pinged host seperately.</li>
  <li>Display Live stats in real time command window.</li>
  </ol>
  
  <p>Complete code is:</p>
  
  <pre><code>#!/user/perl/bin 
  use strict;
  use Net::Ping;
  use Time::HiRes;
  use IO::Handle;
  use threads;
  use threads::shared;
  
  #       AUTHOR  :       Avi Mehenwal
  #       PURPOSE :       Continuous ping check with PERL iThreads (interpreted threads)
  #       DATE    :       10th-Jan-2013
  
  sub Ping {
  
  #Filer handle generation and open log file for writing in append mode.
  #creating ping object to check ping response
  #Writing Ping results in logs and command window for Real time analysis
  #Close file after logs are witten
  
      my ($host , $protocol) = @_;
      chop $host;
      open my $log, '&gt;&gt;', "Ping_$host.txt" or die "Cannot create log file: $!";
      $log-&gt;autoflush;                                                          #autoflusing to stop output stream buffering.      
      my $p = Net::Ping-&gt;new($protocol) or die "Cannot create new Net::Ping object : $!";
      $p-&gt;hires;
      my ($ret, $duration, $ip) = $p-&gt;ping($host);
      if ($ret=='1')
       {    my $event = sprintf "%s\t%s is alive $protocol (packet RTT: %.3fms)\n",
                               scalar localtime, $host, $duration;
            print STDOUT $event;
            print $log $event;
            sleep(1);  #sleep only when ping is continuous    
        }
      else
       {    my $event = sprintf "%s\t%s is UNAVAILABLE (Timedout/lost $protocol request)\n",
                                              scalar localtime, $host;
            print STDOUT $event;
            print $log $event;               
       }
      close $log; 
  }
  
  my @hostip = qw(10.98.10.253, 10.112.114.10, 10.112.114.11);
  STDOUT-&gt;autoflush;
  print "****************************************************************************\n\ncPING-CHECK -v2.0 By-Avi Mehenwal\n\n";
  print "Kindly enter the protocol to be used by cPing-Probe : tcp / udp / icmp ...\n";
  my $protocol = &lt;STDIN&gt;;
  chomp $protocol;
  if($protocol eq "icmp") {
   print "\nWARNING:For cPing-Probe to use icmp protocol kindly run the program in administrative command prompt\ncPing-Probe v2.0will now exit ...";
   sleep(10);
   exit;
  }      
  
  while(1)      {
   foreach my $thost (@hostip) {
     Ping($thost,$protocol);
   }     
  }     
  
  #END 
  </code></pre>
  
  <p>Now I was thinking to improve my code using one thread per ip host which would check ping and write logs without affecting other host ip threads. For this I tryed several codes but I found some success with this code below:</p>
  
  <pre><code>my @hostip = qw(10.98.10.253, 10.112.114.10, 10.112.114.11);
  STDOUT-&gt;autoflush;
  print "****************************************************************************\n\ncPING-CHECK -v2.0 By-Avi Mehenwal\n\n";
  print "Kindly enter the protocol to be used by cPing-Probe : tcp / udp / icmp ...\n";
  my $protocol = &lt;STDIN&gt;;
  chomp $protocol;
  if($protocol eq "icmp") {
   print "\nWARNING:For cPing-Probe to use icmp protocol kindly run the program in administrative command prompt\ncPing-Probe v2.0will now exit ...";
   sleep(10);
   exit;
  }      
  
  while(1)
  {   my $thr;
  foreach my $thost (@hostip) 
  {       $thr = threads-&gt;new(Ping,$thost,$protocol) or die "Cannot Create thread : $!";  #seperate thread for each IP
      Ping($thost,$protocol);
      }
      if($thr-&gt;is_joinable())
       {       $thr-&gt;join(); 
               print STDOUT "Thread joined\n";   }
      else
        {      $thr-&gt;detach();
               print STDOUT "Thread detached\n"; }     
  }     
  
  #END
  </code></pre>
  
  <p>But this code is suffering from some unwanted symptoms for which I need experts help.</p>
  
  <ul>
  <li>Why do I have a log file named Ping_ generated and logs written to it ! Ideally log file per host is required. I fail to understand the trick here.</li>
  <li>Is my code really doing multitasking with parallel running threads for each host ? How I can I check or be sure of that ?</li>
  <li>IS there any better way of doing my job ?</li>
  <li>IS there any additional feature you can think I could add to my code ?</li>
  </ul>
  
  <p>Thanks everyone in advance :D
  Cheers !!</p>
  
  <p>-Avi Mehenwal</p>