Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Net::Syslog Patch

by VinsWorldcom (Priest)
on Apr 29, 2009 at 19:36 UTC ( #760960=snippet: print w/ replies, xml ) Need Help??

Description: I've been doing a little work coding a Syslog message receiver and needed a Syslog message sender to test with. I used Net::Syslog and it did the trick but it is a bit "lean". I updated it to generate Syslog messages according to RFC 3164 format. This is the patch.

Updated: 12-Jun-2009

Reference: http://rt.cpan.org/Public/Bug/Display.html?id=46898

--- Syslog.pm    Mon Apr 27 13:18:10 2009
+++ Syslog.pm    Fri Jun 12 15:22:49 2009
@@ -2,6 +2,7 @@
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
 use IO::Socket;
+use Sys::Hostname;
 
 require Exporter;
 
@@ -12,35 +13,51 @@
 @EXPORT = qw(
     
 );
-$VERSION = '0.03';
+$VERSION = '0.04';
 
 
 # Preloaded methods go here.
 
 my %syslog_priorities=(
     emerg   => 0,
+    emergency     => 0,
     alert   => 1,
     crit    => 2,
+    critical      => 2,
     err     => 3,
+    error         => 3,
     warning => 4,
         notice  => 5,
         info    => 6,
+        informational => 6,
     debug   => 7
 );
 
 my %syslog_facilities=(
     kern    => 0,
+    kernel    => 0,
     user    => 1,
     mail    => 2,
     daemon    => 3,    
+    system    => 3,
     auth    => 4,
+    security  => 4,
     syslog    => 5,
+    internal  => 5,
     lpr    => 6,
+    printer   => 6,
     news     => 7,
     uucp     => 8,
     cron    => 9,
+    clock     => 9,
     authpriv=> 10,
+    security2 => 10,
     ftp    => 11,
+    FTP       => 11,
+    NTP       => 12,
+    audit     => 13,
+    alert     => 14,
+    clock2    => 15,
     local0    => 16,
     local1    => 17,
     local2    => 18,
@@ -48,8 +65,10 @@
     local4    => 20,
     local5    => 21,
     local6    => 22,
+    local7    => 23
 );
 
+my @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
 
 sub new{
   my $class = shift;
@@ -59,7 +78,7 @@
   }
   my $self = { Name     => $name,
                Facility => 'local5',
-               Priority => 'err',
+               Priority => 'error',
                SyslogPort    => 514,
                SyslogHost    => '127.0.0.1'};
   bless $self,$class;
@@ -80,18 +99,21 @@
   }
 
   my $pid=$$;
-  my $facility_i=$syslog_facilities{$local{Facility}};
-  my $priority_i=$syslog_priorities{$local{Priority}};  
-
-  if(!defined $facility_i){
-    $facility_i=21;
-  }
-  if(!defined $priority_i){
-    $priority_i=4;
-  }
+  my $facility_i = $syslog_facilities{$local{Facility}} || 21;
+  my $priority_i = $syslog_priorities{$local{Priority}} || 3;  
 
   my $d=(($facility_i<<3)|($priority_i));
-  my $message = "<$d>$local{Name}\[$pid\]: $msg";
+
+  my $host = inet_ntoa((gethostbyname(hostname))[4]);
+  my @time = localtime();
+  my $ts   = $month[$time[4]] . " " . (($time[3] < 10)?(" " . $time[3
+]):$time[3]) . " " . (($time[2] < 10)?("0" . $time[2]):$time[2]) . ":
+" . (($time[1] < 10)?("0" . $time[1]):$time[1]) . ":" . (($time[0] < 
+10)?("0" . $time[0]):$time[0]);
+  my $message = '';
+
+  if ($local{rfc3164}) {
+      $message = "<$d>$ts $host $local{Name}\[$pid\]: $msg"
+  } else {
+      $message = "<$d>$local{Name}\[$pid\]: $msg"
+  }
 
   my $sock=new IO::Socket::INET(PeerAddr => $local{SyslogHost},
                         PeerPort => $local{SyslogPort},
@@ -133,18 +155,22 @@
 
     Name        <calling script name>
     Facility     local5
-    Priority     err
+    Priority     error
     SyslogPort        514
     SyslogHost        127.0.0.1
 
 Valid Facilities are:
-    kern, user, mail, daemon, auth, syslog, lpr, news, uucp, cron,
-    authpriv, ftp, local0, local1, local2, local3, local4, local5, lo
+cal6
+    kernel, user, mail, system, security, internal, printer, news, 
+    uucp, clock, security2, FTP, NTP, audit, alert, clock2, local0,
+    local1, local2, local3, local4, local5, local6, local7
 
 Valid Priorities are:
-    emerg, alert, crit, err, warning, notice, info, debug
-
+    emergency, alert, critical, error, warning, notice, informational
+, 
+        debug
 
+Use:
+        rfc3164 => 1
+to enable RFC 3164 messages including timestamp and hostname.
 
 =head1 AUTHOR
 
Comment on Net::Syslog Patch
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://760960]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-09-30 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (384 votes), past polls