Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Ping log terminates unexpectedly

by Scrat (Monk)
on Mar 23, 2006 at 13:41 UTC ( #538737=perlquestion: print w/replies, xml ) Need Help??

Scrat has asked for the wisdom of the Perl Monks concerning the following question:

Good day,

I run a small program that performs an ICMP ping every 5 seconds or so from server A over a remote subnet to Server B. The result of each ping is appended to a log file on Server A.

My problem is that the log file would gather data for about 2 to 3 days and then terminate itself. Is there a way I can change it so that it continues to run until I terminate it manually? Below is some code i use:

use strict; use Time::Local; use Net::Ping; my ($time); my @hostnames = ("10.0.0.1"); my $p = Net::Ping->new("icmp"); my $hostname; print "\nWriting data to ping.log\n"; print "Press CTRL-C to exit\n"; while(1) { foreach my $hostname (@hostnames) { $time = localtime; open (LogFile, ">>ping.log") || die "Sorry, cannot open logfil +e\n"; print (LogFile "$time - "); print (LogFile "$hostname is "); print (LogFile "NOT ") unless $p->ping($hostname, 2); print (LogFile "reachable.\n"); close Logfile; sleep(5); } }

Replies are listed 'Best First'.
Re: Ping log terminates unexpectedly
by AcidHawk (Vicar) on Mar 23, 2006 at 14:07 UTC
    I am not sure why the code terminates, however I do have a comment about your code.

    You open a handle to the log file each time you process a host from your @hostnames array. Perhaps you could look at opening the file handle once before processing the entire array. Also you may want to only sleep after the foreach...

    Something like

    use strict; use Time::Local; use Net::Ping; my ($time); my @hostnames = ("10.0.0.1", "10.0.0.2"); my $p = Net::Ping->new("icmp"); my $hostname; print "\nWriting data to ping.log\n"; print "Press CTRL-C to exit\n"; while(1) { open (LogFile, ">>ping.log") || die "Sorry, cannot open logfile\n" +; foreach my $hostname (@hostnames) { $time = localtime; print (LogFile "$time - "); print (LogFile "$hostname is "); print (LogFile "NOT ") unless $p->ping($hostname, 2); print (LogFile "reachable.\n"); } close Logfile; sleep(5); }

    These changes will not affect you unless you @hostnames array grows to more than 1 host though.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      I also thought it was a server reboot, but when I connect to the terminal session (I forgot to add it is a Windows2000 Server), everything is exactly as it was, except for my script window. No errors. Apologies - I should have said "my script terminates", not the log file.

      I thought there might be something wrong with my code.. or something I could add..

      Thanks for the comments AcidHawk.
Re: Ping log terminates unexpectedly
by eXile (Priest) on Mar 23, 2006 at 14:55 UTC
    I'm pretty sure Windows 2000 has a 'cron'-type of facility (scheduled task, or whatever it is called), to have the OS call your script periodically. With a little modification you could have your script do the probing and terminate within 60 seconds, and have the OS startup an new script every 60 seconds.

    From your last description it sounds like you run your stuff from a terminal window. It should also be possible to run it as a service/in the background so it is not dependant on somebody/something terminating your terminal window. This doesn't exclude something/somebody terminating your script itself ofcourse.

    Also, for network performance/failure type of measurements there are some excellent pieces of software available (written in perl):

      Windows 2000 has an 'at' utility as well as a Scheduled Task feature. I'm not sure if the scheduled tasks can be as fine as every 5 seconds though. In my opinion, pinging every 5 seconds might be a bit excessive (and depending on your environment, might set off intrusion alarms). Perhaps sending a ping every couple of minutes might be a bit easier. Just a thought :-)

        The Scheduled Task feature only allows a process to be run every one minute. I can't tell for sure but the at command looks to be the same. I will agree with the above comments that pinging every 5 seconds could degrade network performance if the server is critial enoungh to need to be checked so often.

        Thank you,
        Greg W
        Update: changed some spelling.
Re: Ping log terminates unexpectedly
by monkey_boy (Priest) on Mar 23, 2006 at 13:49 UTC
    I seriously doubt it is "terminating itself", more likely some external factor like a server reboot causes the termination.


    This is not a Signature...
Re: Ping log terminates unexpectedly
by timos (Beadle) on Mar 23, 2006 at 13:50 UTC
    What do you mean by "... log file would gather data for about 2 to 3 days and then terminate itself."? A (log) file can't terminate itself. Is there any error message?
    You also may have a look at https://rt.cpan.org/Public/Dist/Display.html?Name=Net-Ping, there are some bugs reported, maybe one apllies to your situation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538737]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2021-07-30 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?