NAME
Net::Syslogd - Perl implementation of Syslog Listener
SYNOPSIS
use Net::Syslogd;
my $syslogd = Net::Syslogd->new()
or die "Error creating Syslogd listener: %s", Net::Syslogd->error;
while (1) {
my $message;
if (!($message = $syslogd->get_message())) { next }
if (!(defined($message->process_message()))) {
printf "$0: %s\n", Net::Syslogd->error
} else {
printf "%s\t%i\t%s\t%s\t%s\t%s\t%s\n",
$message->peeraddr,
$message->peerport,
$message->facility,
$message->severity,
$message->time,
$message->hostname,
$message->message
}
}
DESCRIPTION
Net::Syslogd is a class implementing a simple Syslog listener in Perl. Net::Syslogd will accept messages on the default Syslog port (UDP 514) and attempts to decode them according to RFC 3164.
METHODS
new() - create a new Net::Syslogd object
my $syslogd = new Net::Syslogd([OPTIONS]);
or
my $syslogd = Net::Syslogd->new([OPTIONS]);
Create a new Net::Syslogd object with OPTIONS as optional parameters. Valid options are:
Option Description Default
------ ----------- -------
-LocalAddr Interface to bind to any
-LocalPort Port to bind server to 514
-Timeout Timeout in seconds to wait for request 10
get_message() - listen for Syslog message
my $message = $syslogd->get_message();
Listen for a Syslog message. Timeout after default or user specified timeout set in new
method and return '0'; else, return is defined.
process_message() - process received Syslog message
$message->process_message();
Process a received Syslog message by RFC 3164 - or as close as possible. RFC 3164 format is as follows:
<###>Mmm dd hh:mm:ss hostname tag msg
|___||_____________|
| Time
Priority
NOTE: This module parses the tag and msg as a single field.
This can also be called as a procedure if one is inclined to write their own UDP listener instead of using get_message()
. For example:
$sock = IO::Socket::INET->new( blah blah blah );
$sock->recv($buffer, 1500);
$message = Net::Syslogd->process_message($buffer);
In either instantiation, allows the following methods to be called.
datagram() - return datagram from Syslog message
$message->datagram();
Return the raw datagram received from a processed (process_message()
) Syslog message.
peeraddr() - return remote address from Syslog message
$message->peeraddr();
Return peer address value from a received and processed (process_message()
) Syslog message. This is the address from the IP header on the UDP datagram.
peerport() - return remote port from Syslog message
$message->peerport();
Return peer port value from a received and processed (process_message()
) Syslog message. This is the port from the IP header on the UDP datagram.
priority() - return priority from Syslog message
$message->priority();
Return priority value from a received and processed (process_message()
) Syslog message. This is the raw priority number not decoded into facility and severity.
facility() - return facility from Syslog message
$message->facility([1]);
Return facility value from a received and processed (process_message()
) Syslog message. This is the text representation of the facility. For the raw number, use the optional boolean argument.
severity() - return severity from Syslog message
$message->severity([1]);
Return severity value from a received and processed (process_message()
) Syslog message. This is the text representation of the severity. For the raw number, use the optional boolean argument.
time() - return time from Syslog message
$message->time();
Return time value from a received and processed (process_message()
) Syslog message.
hostname() - return hostname from Syslog message
$message->hostname();
Return hostname value from a received and processed (process_message()
) Syslog message.
message() - return message from Syslog message
$message->message();
Return message value from a received and processed (process_message()
) Syslog message. Note this is the tag and msg field from a properly formatted RFC 3164 Syslog message.
error() - return last error
printf "Error: %s\n", Net::Syslogd->error;
Return last error.
EXPORT
None by default.
EXAMPLES
Simple Syslog Server
This example implements a simple Syslog server that listens on the default port and prints received messages to the console.
use Net::Syslogd;
my $syslogd = Net::Syslogd->new()
or die "Error creating Syslogd listener: %s", Net::Syslogd->error;
while (1) {
my $message;
if (!($message = $syslogd->get_message())) { next }
if (!(defined($message->process_message()))) {
printf "$0: %s\n", Net::Syslogd->error
} else {
printf "%s\t%i\t%s\t%s\t%s\t%s\t%s\n",
$message->peeraddr,
$message->peerport,
$message->facility,
$message->severity,
$message->time,
$message->hostname,
$message->message
}
}
Forking Syslog Server
use Net::Syslogd;
my $syslogd = Net::Syslogd->new()
or die "Error creating Syslogd listener: %s", Net::Syslogd->error;
while (1) {
my $message;
if (!($message = $syslogd->get_message())) { next }
my $pid = fork();
if (!defined($pid)) {
print "fork() Error!\n";
exit
} elsif ($pid == 0) {
if (!(defined($message->process_message()))) {
printf "$0: %s\n", Net::Syslogd->error
} else {
printf "%s\t%i\t%s\t%s\t%s\t%s\t%s\n",
$message->peeraddr,
$message->peerport,
$message->facility,
$message->severity,
$message->time,
$message->hostname,
$message->message
}
exit
} else {
# parent
}
}
LICENSE
This software is released under the same terms as Perl itself. If you don't know what that means visit http://perl.com/.
AUTHOR
Copyright (C) Michael Vincent 2010
All rights reserved