Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Net::Syslog Patch by VinsWorldcom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found