Michael Vincent > Net-Syslogd-0.11 > Net::Syslogd

Download:
Net-Syslogd-0.11.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.11   Source  

NAME ^

Net::Syslogd - Perl implementation of Syslog Listener

SYNOPSIS ^

  use Net::Syslogd;

  my $syslogd = Net::Syslogd->new()
    or die "Error creating Syslogd listener: ", Net::Syslogd->error;

  while (1) {
      my $message = $syslogd->get_message();

      if (!defined($message)) {
          printf "$0: %s\n", Net::Syslogd->error;
          exit 1
      } elsif ($message == 0) {
          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->remoteaddr,
                 $message->remoteport,
                 $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 attempt to decode them according to RFC 3164.

METHODS ^

new() - create a new Net::Syslogd object

  my $syslogd = Net::Syslogd->new([OPTIONS]);

Create a new Net::Syslogd object with OPTIONS as optional parameters. Valid options are:

  Option     Description                            Default
  ------     -----------                            -------
  -Family    Address family IPv4/IPv6                  IPv4
               Valid values for IPv4:
                 4, v4, ip4, ipv4, AF_INET (constant)
               Valid values for IPv6:
                 6, v6, ip6, ipv6, AF_INET6 (constant)
  -LocalAddr Interface to bind to                       any
  -LocalPort Port to bind server to                     514
  -timeout   Timeout in seconds for socket               10
             operations and to wait for request

NOTE: IPv6 requires IO::Socket::IP. Failback is IO::Socket::INET and only IPv4 support.

Allows the following accessors to be called.

server() - return IO::Socket::IP object for server

  $syslogd->server();

Return IO::Socket::IP object for the created server. All IO::Socket::IP accessors can then be called.

get_message() - listen for Syslog message

  my $message = $syslogd->get_message([OPTIONS]);

Listen for Syslog messages. Timeout after default or user specified timeout set in new method and return '0'. If message is received before timeout, return is defined. Return is not defined if error encountered.

Valid options are:

  Option     Description                            Default
  ------     -----------                            -------
  -maxsize   Max size in bytes of acceptable          65467
             message.
             Value can be integer 1 <= # <= 65467.
             Keywords: 'RFC'         = 1024
                       'recommended' = 2048
  -timeout   Timeout in seconds to wait for              10
             request.  Overrides value set with
             new().

Allows the following accessors to be called.

remoteaddr() - return remote address from Syslog message

  $message->remoteaddr();

Return remote address value from a received (get_message()) Syslog message. This is the address from the IP header on the UDP datagram.

remoteport() - return remote port from Syslog message

  $message->remoteport();

Return remote port value from a received (get_message()) Syslog message. This is the port from the IP header on the UDP datagram.

datagram() - return datagram from Syslog message

  $message->datagram();

Return the raw datagram from a received (get_message()) Syslog message.

process_message() - process received Syslog message

  $message->process_message([OPTIONS]);

Process a received Syslog message according to RFC 3164 - or as close as possible. RFC 3164 format is as follows:

  <###>Mmm dd hh:mm:ss hostname tag content
  |___||_____________| |______| |_________|
    |     Timestamp    Hostname   Message
    |
   Priority -> (facility and severity)

NOTE: This module parses the tag and content as a single field.

Called with one argument, interpreted as the datagram to process. Valid options are:

  Option     Description                            Default
  ------     -----------                            -------
  -datagram  Datagram to process                    -Provided by
                                                     get_message()-
  -regex     Regular expression to parse received   -Provided in
             syslog message.                         this method-
             Keywords: 'RFC' = Strict RFC 3164
             Must include ()-matching:
               $1 = priority
               $2 = time
               $3 = hostname
               $4 = message

NOTE: This uses a regex that parses RFC 3164 compliant syslog messages. It will also recoginize Cisco syslog messages (not fully RFC 3164 compliant) sent with 'timestamp' rather than 'uptime'.

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::IP->new( blah blah blah );
  $sock->recv($datagram, 1500);
  # process datagram in $datagram variable
  $message = Net::Syslogd->process_message($datagram);

In either instantiation, allows the following accessors to be called.

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 ^

This distribution comes with several scripts (installed to the default "bin" install directory) that not only demonstrate example uses but also provide functional execution.

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

http://www.VinsWorld.com

All rights reserved

syntax highlighting: