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

hi again!

I'm using Sys::Syslog module for writing to Unix syslog as follows:
use Sys::Syslog qw(:DEFAULT setlogsock); setlogsock('unix'); openlog('Test', 'NULL', 'syslog'); syslog($priority, 'This is a test');
The problem is when the Unix syslog is down, every call to syslog() crashes my program. I read the man page and it says that setlogsock returns undef on failure but I guess it assumes that the syslog is up coz' I checked it's return value and came up with nothing.
Is there a way knowing if the syslog is up without using the 'ps' command?

Hotshot

Replies are listed 'Best First'.
Re: syslog problems
by rr (Sexton) on Oct 24, 2002 at 15:57 UTC
    Try doing your connection and initial test to syslog in an eval block. Then check $@ for an error. This will allow you to continue operation if syslog is not down. You can also create a generic log subroutine that eval's every syslog operation.
    sub log { eval { syslog($_[0], $_[1]); }; if ($@) { print STDERR "Syslog error: $@ $!\n"; }
    BTW, it would be helpfull if you would post the OS and version, as well as the errors your program crashes with. Also the contents of $! if possible.
Re: syslog problems
by grinder (Bishop) on Oct 24, 2002 at 14:27 UTC

    You could try seeing if anyone is listening on the syslog port. The beauty of this approach is that it doesn't matter if you're logging on localhost or a remote host.

    #! /usr/bin/perl -w use strict; use IO::Socket; sub syslog_ok { my $s = IO::Socket::INET->new( Timeout => 5, Proto => 'udp', PeerHost => '10.0.0.1', PeerPort => 514, ); if( $s ) { close $s; 1; } else { 0; } } print syslog_ok(), "\n";

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: syslog problems
by sch (Pilgrim) on Oct 24, 2002 at 13:40 UTC

    On my HP-UX box there's a syslogd process - you could probably check for that.

    But today you took me walking, Through a land that we have lost,
    While our children sit at websites, With no access to the cost