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

A while back I wrote a script that listens on a UDP port for syslog type messages from a bunch of sonicwall firewalls. It parses interesting information out and saves it into a sql database from which we do reports. My problem is that the NT service that sends the syslog messages to the machine that my script lives on, locks up, dies or otherwise ceases to send info. If I restart the service on the other machine everything works again. My question is how do I get the script to tell me it hasn't received any info in a while so I know when to do something about this service? I looked into using timeout but it doesn't seem to do what I want. I am about to look into noting the time between messages coming in and when the time is more than X notify someone (or maybe remotely restart the service on the other machine, but that is another problem). Does anyone know a feature of IO:Socket or some other method of noticing the LACK of traffic on a UDP port?

Replies are listed 'Best First'.
Re: Checking for no traffic on a port
by Corion (Patriarch) on Mar 25, 2002 at 19:27 UTC

    As you are putting the data into a database already, I would simply implement the whole stuff as a rule-checker on the database, that is, a SQL query is run against the database every 5 minutes to see if there has been a syslog update from the NT machine. If there is no such update, then either your script or the NT service is not running, and the support people are to be alerted - you could also send your service an UDP ping packet to wake it up and get it to put a "I'm alive" entry into the database.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Checking for no traffic on a port
by cfreak (Chaplain) on Mar 25, 2002 at 19:20 UTC
    I've been playing with UDP lately... you might be able to use the alarm function, like this:
    # initialize your IO::Socket etc... $SIG{ALRM} = sub { # code here to do something interesting # if your program isn't getting + # connections } my $minutes_to_wait = 5; # wait five minutes my $alarm_seconds = $minutes_to_wait * 60; alarm $alarm_seconds; # set the first alarm while($server->recv($msg,$MAX_LEN) { # if we're here we got something # do some interesting stuff with the message #reset the alarm alarm $alarm_seconds; }
    I put the server code in place but I don't have my client code in front of me to make sure the server doesn't execute that block even though its getting connections. In theory it should work though :) and if you don't receive something for 5 minutes it will execute the sig alarm block at the top.

    Hope that helps.
    Chris
      This looks like it is exactly what I am looking for but it looks like alarm() is not available under NT 4.0 at least.