| 1 | package Net::SyslogNg; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use 5.14.2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw/croak carp/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Sys::Hostname; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use DateTime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | 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 | 1 | 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 | 1 | ▶ | my ($class, %opt) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||
| 65 | 1 | my $self = {}; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | #Params | ||||||||||||||||||||
| 68 | 1 | ▶ | $self->{'facility'} = $opt{'-facility'} // 'local5'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-facility"} // "local5"
MC/DC0%0/1
$opt{"-facility"} // "local5"
$opt{"-facility"}
Truth table50%1/2
A
A= $opt{"-facility"}
| |||||||||||||||||||||
| 69 | 1 | ▶ | $self->{'priority'} = $opt{'-priority'} // 'error'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-priority"} // "error"
MC/DC0%0/1
$opt{"-priority"} // "error"
$opt{"-priority"}
Truth table50%1/2
A
A= $opt{"-priority"}
| |||||||||||||||||||||
| 70 | 1 | ▶ | $self->{'syslog_port'} = $opt{'-syslog_port'} // 514; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-syslog_port"} // 514
MC/DC0%0/1
$opt{"-syslog_port"} // 514
$opt{"-syslog_port"}
Truth table50%1/2
A
A= $opt{"-syslog_port"}
| |||||||||||||||||||||
| 71 | 1 | ▶ | $self->{'syslog_host'} = $opt{'-syslog_host'} // '127.0.0.1'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-syslog_host"} // "127.0.0.1"
MC/DC0%0/1
$opt{"-syslog_host"} // "127.0.0.1"
$opt{"-syslog_host"}
Truth table50%1/2
A
A= $opt{"-syslog_host"}
| |||||||||||||||||||||
| 72 | 1 | $self->{'debug'} = $opt{'-debug'}; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1 | return bless $self, $class; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | sub send { | ||||||||||||||||||||
| 78 | 1 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||
|
Subroutine
send: called CC=15 SCAR=32.5
Pod
documented
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 1 | ▶ | my $pid = $opt{'-pid'} // $$; | ||||||||||||||||||
|
Condition33%1/3
$opt{"-pid"} // $$
MC/DC0%0/2
$opt{"-pid"} // $$
$opt{"-pid"}
$$
Truth table33%1/3
A // B
A= $opt{"-pid"}B= $$
| |||||||||||||||||||||
| 81 | 1 | ▶ | my $msg = $opt{'-msg'} // ''; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-msg"} // ""
MC/DC0%0/1
$opt{"-msg"} // ""
$opt{"-msg"}
Truth table50%1/2
A
A= $opt{"-msg"}
| |||||||||||||||||||||
| 82 | 1 | ▶ | my $version = $opt{'-version'} // 1; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-version"} // 1
MC/DC0%0/1
$opt{"-version"} // 1
$opt{"-version"}
Truth table50%1/2
A
A= $opt{"-version"}
| |||||||||||||||||||||
| 83 | 1 | ▶ | my $timestamp = $opt{'-timestamp'} // DateTime->now()->iso8601 . '.000Z'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-timestamp"} // "DateTime"->now->iso8601 . '.000Z'
MC/DC0%0/1
$opt{"-timestamp"} // "DateTime"->now->iso8601 . '.000Z'
$opt{"-timestamp"}
Truth table50%1/2
A
A= $opt{"-timestamp"}
| |||||||||||||||||||||
| 84 | 1 | ▶ | my $hostname = $opt{'-hostname'} // inet_ntoa( ( gethostbyname(hostname) )[4] ); | ||||||||||||||||||
|
Condition33%1/3
$opt{"-hostname"} // inet_ntoa((gethostbyname hostname())[4])
MC/DC0%0/2
$opt{"-hostname"} // inet_ntoa((gethostbyname hostname())[4])
$opt{"-hostname"}
inet_ntoa((gethostbyname hostname())[4])
Truth table33%1/3
A // B
A= $opt{"-hostname"}B= inet_ntoa((gethostbyname hostname())[4])
| |||||||||||||||||||||
| 85 | 1 | ▶ | my $message_id = $opt{'-message_id'} // '-'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-message_id"} // "-"
MC/DC0%0/1
$opt{"-message_id"} // "-"
$opt{"-message_id"}
Truth table50%1/2
A
A= $opt{"-message_id"}
| |||||||||||||||||||||
| 86 | 1 | ▶ | my $structured_data = $opt{'-structured_data'} // '-'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-structured_data"} // "-"
MC/DC0%0/1
$opt{"-structured_data"} // "-"
$opt{"-structured_data"}
Truth table50%1/2
A
A= $opt{"-structured_data"}
| |||||||||||||||||||||
| 87 | 1 | ▶ | my $application = $opt{'-application'} // '-'; | ||||||||||||||||||
|
Condition50%1/2
$opt{"-application"} // "-"
MC/DC0%0/1
$opt{"-application"} // "-"
$opt{"-application"}
Truth table50%1/2
A
A= $opt{"-application"}
| |||||||||||||||||||||
| 88 | 1 | ▶ | my $facility = $opt{'-facility'} // $self->{'facility'}; | ||||||||||||||||||
|
Condition33%1/3
$opt{"-facility"} // $self->{"facility"}
MC/DC0%0/2
$opt{"-facility"} // $self->{"facility"}
$opt{"-facility"}
$self->{"facility"}
Truth table33%1/3
A // B
A= $opt{"-facility"}B= $self->{"facility"}
| |||||||||||||||||||||
| 89 | 1 | ▶ | my $priority = $opt{'-priority'} // $self->{'priority'}; | ||||||||||||||||||
|
Condition33%1/3
$opt{"-priority"} // $self->{"priority"}
MC/DC0%0/2
$opt{"-priority"} // $self->{"priority"}
$opt{"-priority"}
$self->{"priority"}
Truth table33%1/3
A // B
A= $opt{"-priority"}B= $self->{"priority"}
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | ▶ | my $facility_i = $SYSLOG_FACILITIES{$facility} // croak "Wrong facility: '$facility'"; | ||||||||||||||||||
|
Condition33%1/3
$SYSLOG_FACILITIES{$facility} // croak("Wrong facility: '${facility}'")
MC/DC0%0/2
$SYSLOG_FACILITIES{$facility} // croak("Wrong facility: '${facility}'")
$SYSLOG_FACILITIES{$facility}
croak("Wrong facility: '${facility}'")
Truth table33%1/3
A // B
A= $SYSLOG_FACILITIES{$facility}B= croak("Wrong facility: '${facility}'")
| |||||||||||||||||||||
| 92 | 1 | ▶ | my $priority_i = $SYSLOG_PRIORITIES{$priority} // croak "Wrong priority: '$priority'"; | ||||||||||||||||||
|
Condition33%1/3
$SYSLOG_PRIORITIES{$priority} // croak("Wrong priority: '${priority}'")
MC/DC0%0/2
$SYSLOG_PRIORITIES{$priority} // croak("Wrong priority: '${priority}'")
$SYSLOG_PRIORITIES{$priority}
croak("Wrong priority: '${priority}'")
Truth table33%1/3
A // B
A= $SYSLOG_PRIORITIES{$priority}B= croak("Wrong priority: '${priority}'")
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 1 | my $priority_raw = ( ( $facility_i << 3 ) | ($priority_i) ); | |||||||||||||||||||
| 95 | 1 | 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 | 1 | ▶ | 'PeerPort' => $self->{'syslog_port'}, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 'Proto' => 'udp', | ||||||||||||||||||||
| 101 | ) or croak "Can't connect to $self->{'syslog_host'}:$self->{'syslog_port'} $@"; | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1 | print $sock $msg_raw; | |||||||||||||||||||
| 104 | 1 | close $sock; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 1 | ▶ | if ($self->{'debug'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 1 | print STDOUT 'Syslog raw message: ' . $msg_raw, "\n"; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | 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 | 1; | |||||||||||||||||||