<?xml version="1.0" encoding="windows-1252"?>
<node id="760960" title="Net::Syslog Patch" created="2009-04-29 15:36:40" updated="2009-04-29 15:36:40">
<type id="1980">
snippet</type>
<author id="749850">
VinsWorldcom</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
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.&lt;p&gt;

Updated:  12-Jun-2009&lt;p&gt;

Reference:  [http://rt.cpan.org/Public/Bug/Display.html?id=46898]&lt;p&gt;</field>
<field name="snippetcode">
&lt;code&gt;
--- 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   =&gt; 0,
+	emergency     =&gt; 0,
 	alert   =&gt; 1,
 	crit    =&gt; 2,
+	critical      =&gt; 2,
 	err     =&gt; 3,
+	error         =&gt; 3,
 	warning =&gt; 4,
         notice  =&gt; 5,
         info    =&gt; 6,
+        informational =&gt; 6,
 	debug   =&gt; 7
 );
 
 my %syslog_facilities=(
 	kern	=&gt; 0,
+	kernel    =&gt; 0,
 	user	=&gt; 1,
 	mail	=&gt; 2,
 	daemon	=&gt; 3,	
+	system    =&gt; 3,
 	auth	=&gt; 4,
+	security  =&gt; 4,
 	syslog	=&gt; 5,
+	internal  =&gt; 5,
 	lpr	=&gt; 6,
+	printer   =&gt; 6,
 	news 	=&gt; 7,
 	uucp 	=&gt; 8,
 	cron	=&gt; 9,
+	clock     =&gt; 9,
 	authpriv=&gt; 10,
+	security2 =&gt; 10,
 	ftp	=&gt; 11,
+	FTP       =&gt; 11,
+	NTP       =&gt; 12,
+	audit     =&gt; 13,
+	alert     =&gt; 14,
+	clock2    =&gt; 15,
 	local0	=&gt; 16,
 	local1	=&gt; 17,
 	local2	=&gt; 18,
@@ -48,8 +65,10 @@
 	local4	=&gt; 20,
 	local5	=&gt; 21,
 	local6	=&gt; 22,
+	local7    =&gt; 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     =&gt; $name,
                Facility =&gt; 'local5',
-               Priority =&gt; 'err',
+               Priority =&gt; 'error',
                SyslogPort    =&gt; 514,
                SyslogHost    =&gt; '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&lt;&lt;3)|($priority_i));
-  my $message = "&lt;$d&gt;$local{Name}\[$pid\]: $msg";
+
+  my $host = inet_ntoa((gethostbyname(hostname))[4]);
+  my @time = localtime();
+  my $ts   = $month[$time[4]] . " " . (($time[3] &lt; 10)?(" " . $time[3]):$time[3]) . " " . (($time[2] &lt; 10)?("0" . $time[2]):$time[2]) . ":" . (($time[1] &lt; 10)?("0" . $time[1]):$time[1]) . ":" . (($time[0] &lt; 10)?("0" . $time[0]):$time[0]);
+  my $message = '';
+
+  if ($local{rfc3164}) {
+      $message = "&lt;$d&gt;$ts $host $local{Name}\[$pid\]: $msg"
+  } else {
+      $message = "&lt;$d&gt;$local{Name}\[$pid\]: $msg"
+  }
 
   my $sock=new IO::Socket::INET(PeerAddr =&gt; $local{SyslogHost},
                         PeerPort =&gt; $local{SyslogPort},
@@ -133,18 +155,22 @@
 
 	Name		&lt;calling script name&gt;
 	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, local6
+	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 =&gt; 1
+to enable RFC 3164 messages including timestamp and hostname.
 
 =head1 AUTHOR
 
&lt;/code&gt;</field>
</data>
</node>
