| 1 | package Net::SyslogNg; | ||
| 2 | |||
| 3 | use 5.14.2; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | use Carp qw/croak carp/; | ||
| 8 | use IO::Socket::INET; | ||
| 9 | use Sys::Hostname; | ||
| 10 | use DateTime; | ||
| 11 | |||
| 12 | our $VERSION = '0.02'; | ||
| 13 | |||
| 14 | my %SYSLOG_PRIORITIES = ( | ||
| 15 | 'emerg' => 0, | ||
| 16 | 'emergency' => 0, | ||
| 17 | 'alert' => 1, | ||
| 18 | 'crit' => 2, | ||
| 19 | 'critical' => 2, | ||
| 20 | 'err' => 3, | ||
| 21 | 'error' => 3, | ||
| 22 | 'warning' => 4, | ||
| 23 | 'notice' => 5, | ||
| 24 | 'info' => 6, | ||
| 25 | 'informational' => 6, | ||
| 26 | 'debug' => 7 | ||
| 27 | ); | ||
| 28 | |||
| 29 | my %SYSLOG_FACILITIES = ( | ||
| 30 | 'kern' => 0, | ||
| 31 | 'kernel' => 0, | ||
| 32 | 'user' => 1, | ||
| 33 | 'mail' => 2, | ||
| 34 | 'daemon' => 3, | ||
| 35 | 'system' => 3, | ||
| 36 | 'auth' => 4, | ||
| 37 | 'syslog' => 5, | ||
| 38 | 'internal' => 5, | ||
| 39 | 'lpr' => 6, | ||
| 40 | 'printer' => 6, | ||
| 41 | 'news' => 7, | ||
| 42 | 'uucp' => 8, | ||
| 43 | 'cron' => 9, | ||
| 44 | 'clock' => 9, | ||
| 45 | 'authpriv' => 10, | ||
| 46 | 'security2' => 10, | ||
| 47 | 'ftp' => 11, | ||
| 48 | 'FTP' => 11, | ||
| 49 | 'NTP' => 11, | ||
| 50 | 'audit' => 13, | ||
| 51 | 'alert' => 14, | ||
| 52 | 'clock2' => 15, | ||
| 53 | 'local0' => 16, | ||
| 54 | 'local1' => 17, | ||
| 55 | 'local2' => 18, | ||
| 56 | 'local3' => 19, | ||
| 57 | 'local4' => 20, | ||
| 58 | 'local5' => 21, | ||
| 59 | 'local6' => 22, | ||
| 60 | 'local7' => 23, | ||
| 61 | ); | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my ($class, %opt) = @_; | ||
| 65 | my $self = {}; | ||
| 66 | |||
| 67 | #Params | ||
| 68 | $self->{'facility'} = $opt{'-facility'} // 'local5'; | ||
| 69 | $self->{'priority'} = $opt{'-priority'} // 'error'; | ||
| 70 | $self->{'syslog_port'} = $opt{'-syslog_port'} // 514; | ||
| 71 | $self->{'syslog_host'} = $opt{'-syslog_host'} // '127.0.0.1'; | ||
| 72 | $self->{'debug'} = $opt{'-debug'}; | ||
| 73 | |||
| 74 | return bless $self, $class; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub send { | ||
| 78 | my ($self, %opt) = @_; | ||
| 79 | |||
| 80 | my $pid = $opt{'-pid'} // $$; | ||
| 81 | my $msg = $opt{'-msg'} // ''; | ||
| 82 | my $version = $opt{'-version'} // 1; | ||
| 83 | my $timestamp = $opt{'-timestamp'} // DateTime->now()->iso8601 . '.000Z'; | ||
| 84 | my $hostname = $opt{'-hostname'} // inet_ntoa( ( gethostbyname(hostname) )[4] ); | ||
| 85 | my $message_id = $opt{'-message_id'} // '-'; | ||
| 86 | my $structured_data = $opt{'-structured_data'} // '-'; | ||
| 87 | my $application = $opt{'-application'} // '-'; | ||
| 88 | my $facility = $opt{'-facility'} // $self->{'facility'}; | ||
| 89 | my $priority = $opt{'-priority'} // $self->{'priority'}; | ||
| 90 | |||
| 91 | my $facility_i = $SYSLOG_FACILITIES{$facility} // croak "Wrong facility: '$facility'"; | ||
| 92 | my $priority_i = $SYSLOG_PRIORITIES{$priority} // croak "Wrong priority: '$priority'"; | ||
| 93 | |||
| 94 | my $priority_raw = ( ( $facility_i << 3 ) | ($priority_i) ); | ||
| 95 | my $msg_raw = "<$priority_raw>" . join(' ', $version, $timestamp, $hostname, $application, $pid, $message_id, $structured_data, $msg); | ||
| 96 | |||
| 97 | my $sock = IO::Socket::INET->new( | ||
| 98 | 'PeerAddr' => $self->{'syslog_host'}, | ||
| 99 | 'PeerPort' => $self->{'syslog_port'}, | ||
| 100 | 'Proto' => 'udp', | ||
| 101 | ) or croak "Can't connect to $self->{'syslog_host'}:$self->{'syslog_port'} $@"; | ||
| 102 | |||
| 103 | print $sock $msg_raw; | ||
| 104 | close $sock; | ||
| 105 | |||
| 106 | if ($self->{'debug'}) { | ||
| 107 | print STDOUT 'Syslog raw message: ' . $msg_raw, "\n"; | ||
| 108 | } | ||
| 109 | |||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | =pod | ||
| 116 | |||
| 117 | =encoding UTF-8 | ||
| 118 | |||
| 119 | =head1 NAME | ||
| 120 | |||
| 121 | B<Net::SyslogNg> - client module for writing to syslog server (rfc5424) | ||
| 122 | |||
| 123 | =head1 VERSION | ||
| 124 | |||
| 125 | version 0.02 | ||
| 126 | |||
| 127 | =head1 SYNOPSYS | ||
| 128 | |||
| 129 | use Net::SyslogNg; | ||
| 130 | |||
| 131 | # Create Net::SyslogNg object | ||
| 132 | my $syslog = Net::SyslogNg->new( | ||
| 133 | '-syslog_host' => '127.0.0.1', | ||
| 134 | '-syslog_port' => 514, | ||
| 135 | ); | ||
| 136 | |||
| 137 | # Send message to syslog | ||
| 138 | $syslog->send( | ||
| 139 | '-facility' => 'daemon', | ||
| 140 | '-priority' => 'error', | ||
| 141 | '-msg' => 'Syslog error message', | ||
| 142 | ); | ||
| 143 | |||
| 144 | =head1 METHODS | ||
| 145 | |||
| 146 | =head2 new(%opt) | ||
| 147 | |||
| 148 | Create Net::SyslogNg object | ||
| 149 | |||
| 150 | Options: | ||
| 151 | -syslog_host => Syslog host address | ||
| 152 | -syslog_port => Syslog port number | ||
| 153 | -facility => Facility name | ||
| 154 | -priority => Priority name | ||
| 155 | -debug => Enable printing debug messages (default: 0) | ||
| 156 | |||
| 157 | =head2 send(%opt) | ||
| 158 | |||
| 159 | Send message to syslog daemon | ||
| 160 | |||
| 161 | Options: | ||
| 162 | -facility => Facility name | ||
| 163 | -priority => Priority name | ||
| 164 | -pid => Process id number (default: current process id) | ||
| 165 | -msg => String of message | ||
| 166 | -version => Version number (default: 1) | ||
| 167 | -timestamp => Timestamp of message (default: current time in UTC) | ||
| 168 | -hostname => Hostname of syslog client (default: current hostname) | ||
| 169 | -message_id => Message id | ||
| 170 | -structured_data => Structured data field | ||
| 171 | -application => Application name | ||
| 172 | |||
| 173 | =head1 DEPENDENCE | ||
| 174 | |||
| 175 | L<Sys::Hostname>, L<IO::Socket::INET>, L<DateTime> | ||
| 176 | |||
| 177 | =head1 AUTHORS | ||
| 178 | |||
| 179 | =over 4 | ||
| 180 | |||
| 181 | =item * | ||
| 182 | |||
| 183 | Pavel Andryushin <vrag867@gmail.com> | ||
| 184 | |||
| 185 | =back | ||
| 186 | |||
| 187 | =head1 COPYRIGHT AND LICENSE | ||
| 188 | |||
| 189 | This software is copyright (c) 2020 by Pavel Andryushin. | ||
| 190 | |||
| 191 | This is free software; you can redistribute it and/or modify it under | ||
| 192 | the same terms as the Perl 5 programming language system itself. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | 1; |