blib/lib/File/Syslogger.pm

filter: Statementstmt 26.7% 19 / 71 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 100% 1 / 1 totaltotal 23.4% 26 / 111 CC 17 SCAR 53.4
CC
17
Cov
19%
run
306.0
__ANON__
2.0
__ANON__
2.0
CRAP 208.5
↑ Summary
1 package File::Syslogger;
2  
3 1 ▶use 5.006;
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 POE qw(Wheel::FollowTail);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Syslog::Fast ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 File::Syslogger - Use POE to tail a file and read new lines into syslog.
13  
14 =head1 VERSION
15  
16 Version 0.0.3
17  
18 =cut
19  
20 1 our $VERSION = '0.0.3';
21  
22 =head1 SYNOPSIS
23  
24 use File::Syslogger;
25  
26 File::Syslogger->run(
27 pri=>'alert',
28 facility=>'daemon',
29 files=>{
30 {'sagan_eve'}=>{file=>'/var/log/sagan/eve', program=>'saganEve'},
31 {'suricata_eve'}=>{file=>'/var/log/suricata/eve', program=>'suricataEve'},
32 },
33 );
34  
35 =head1 METHODS
36  
37 =head2 run
38  
39 Initiates POE sessions and run them.
40  
41 This will die if there are any config issues.
42  
43 The following options are optionaal.
44  
45 - priority :: The priority of the logged item.
46 Default :: notice
47  
48 - facility :: The facility for logging.
49 Default :: daemon
50  
51 - program :: Name of the program logging.
52 Default :: fileSyslogger
53  
54 - socket :: The syslogd socket.
55 Default :: /var/run/log
56  
57 The option files is a hash of hashes. It has one mandatory
58 key, 'file', which is the file to follow. All the above
59 options may be used in the sub hashes.
60  
61 For priority, below are the various valid values.
62  
63 emerg
64 emergency
65 alert
66 crit
67 critical
68 err
69 error
70 warning
71 notice
72 info
73  
74 For facility, below are the various valid values.
75  
76 kern
77 user
78 mail
79 daemon
80 auth
81 syslog
82 lpr
83 news
84 uucp
85 cron
86 authpriv
87 ftp
88 local0
89 local1
90 local2
91 local3
92 local4
93 local5
94 local6
95 local7
96  
97 File rotation should be picked up POE::Wheel::FollowTail.
98  
99 =cut
100  
101 0 ▶sub run {
Subroutine run: not called CC=17 SCAR=57.2
Pod documented
102 0 my ( $blank, %opts ) = @_;
103  
104 0 ▶ if ( !defined( $opts{files} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"files"}) 0 0 0
105 0 die('"files" is not defined');
106 }
107  
108 0 ▶ if ( ref( $opts{files} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"files"} ne "HASH") 0 0 0
109 0 die("$opts{files} is not a hash");
110 }
111  
112 0 ▶ if ( !defined( $opts{program} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"program"}) 0 0 0
113 0 $opts{program} = 'fileSyslogger';
114 }
115  
116 0 ▶ if ( !defined( $opts{socket} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"socket"}) 0 0 0
117 0 $opts{socket} = "/var/run/log";
118 }
119  
120 #mapping for severity for constant handling
121 0 my %sev_mapping = (
122 'emerg' => LOG_EMERG,
123 'emergency' => LOG_EMERG,
124 'alert' => LOG_ALERT,
125 'crit' => LOG_CRIT,
126 'critical' => LOG_CRIT,
127 'err' => LOG_ERR,
128 'error' => LOG_ERR,
129 'warning' => LOG_WARNING,
130 'notice' => LOG_NOTICE,
131 'info' => LOG_INFO,
132 );
133  
134 # default to info if none is specified
135 0 ▶ if ( !defined( $opts{priority} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'priority'}) { } 0 0 0
136 0 $opts{priority} = "notice";
137 } else {
138 # one was specified, convert to lower case and make sure it valid
139 0 $opts{priority} = lc( $opts{priority} );
140 0 ▶ if ( !defined( $sev_mapping{ $opts{priority} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sev_mapping{$opts{"priority"}}) 0 0 0
141 0 die( '"' . $opts{priority} . '" is not a known priority' );
142 }
143 }
144  
145 #mapping for facility for constant handling
146 0 my %fac_mapping = (
147 'kern' => LOG_KERN,
148 'user' => LOG_USER,
149 'mail' => LOG_MAIL,
150 'daemon' => LOG_DAEMON,
151 'auth' => LOG_AUTH,
152 'syslog' => LOG_SYSLOG,
153 'lpr' => LOG_LPR,
154 'news' => LOG_NEWS,
155 'uucp' => LOG_UUCP,
156 'cron' => LOG_CRON,
157 'authpriv' => LOG_AUTHPRIV,
158 'ftp' => LOG_FTP,
159 'local0' => LOG_LOCAL0,
160 'local1' => LOG_LOCAL1,
161 'local2' => LOG_LOCAL2,
162 'local3' => LOG_LOCAL3,
163 'local4' => LOG_LOCAL4,
164 'local5' => LOG_LOCAL5,
165 'local6' => LOG_LOCAL6,
166 'local7' => LOG_LOCAL7,
167 );
168  
169 # default to system if none is specified
170 0 ▶ if ( !defined( $opts{facility} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'facility'}) { } 0 0 0
171 0 $opts{facility} = 'daemon';
172 } else {
173 # one was specified, convert to lower case and make sure it valid
174 0 $opts{facility} = lc( $opts{facility} );
175 0 ▶ if ( !defined( $fac_mapping{ $opts{facility} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fac_mapping{$opts{"facility"}}) 0 0 0
176 0 die( '"' . $opts{facility} . '" is not a known facility' );
177 }
178 }
179  
180 # process each file and setup the syslogger
181 0 my $file_count = 0;
182 0 foreach my $item ( keys( %{ $opts{files} } ) ) {
183  
184 # make sure we have a file specified
185 0 ▶ if ( !defined( $opts{files}{$item}{file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"files"}{$item}{"file"}) 0 0 0
186 0 die( 'No file specified for "' . $item . '"' );
187 }
188  
189 # figure out what facility to use for this item
190 0 my $item_fac;
191 0 ▶ if ( defined( $opts{files}{$item}{facility} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'files'}{$item}{'facility'}) { } 0 0 0
192  
193 # make sure it is valid
194 0 $item_fac = lc( $opts{files}{$item}{facility} );
195 0 ▶ if ( !defined( $fac_mapping{ $opts{facility} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fac_mapping{$opts{"facility"}}) 0 0 0
196 0 die( '"' . $item_fac . '" in "' . $item . '" is not a known facility' );
197 }
198 } else {
199 # none specified, so using default
200 0 $item_fac = $opts{facility};
201 }
202 0 $item_fac = $fac_mapping{$item_fac};
203  
204 # figure out what facility to use for this item
205 0 my $item_pri;
206 0 ▶ if ( defined( $opts{files}{$item}{priority} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'files'}{$item}{'priority'}) { } 0 0 0
207  
208 # make sure it is valid
209 0 $item_pri = lc( $opts{files}{$item}{priority} );
210 0 ▶ if ( !defined( $fac_mapping{$item_pri} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fac_mapping{$item_pri}) 0 0 0
211 0 die( '"' . $item_pri . '" in "' . $item . '" is not a known facility' );
212 }
213 } else {
214 # none specified, so using default
215 0 $item_pri = $opts{priority};
216 }
217 0 $item_pri = $sev_mapping{$item_pri};
218  
219 # figure out what program name to use
220 0 my $item_program;
221 0 ▶ if ( defined( $opts{files}{$item}{program} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'files'}{$item}{'program'}) { } 0 0 0
222 0 $item_program = $opts{files}{$item}{program};
223 } else {
224 # none specified, so using default
225 0 $item_program = $opts{program};
226 }
227  
228 # create the logger that will be used by the POE session
229 my $logger
230 0 = Log::Syslog::Fast->new( LOG_UNIX, $opts{socket}, 1, $item_fac, $item_pri, hostname, $item_program );
231  
232 # create the POE session
233 POE::Session->create(
234 inline_states => {
235 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
237 Filename => $_[HEAP]{file},
238 0 InputEvent => "got_log_line",
239 );
240 },
241 0 ▶ got_log_line => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 $_[HEAP]{logger}->send( $_[ARG0] );
243 },
244 },
245 0 heap => { file => $opts{files}{$item}{file}, logger => $logger },
246 );
247  
248 0 $file_count++;
249 } ## end foreach my $item ( keys( %{ $opts{files} } ) )
250  
251 0 ▶ if ( $file_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_count == 0) 0 0 0
252 0 die("No files specified");
253 }
254  
255 0 POE::Kernel->run;
256 } ## end sub run
257  
258 =head1 AUTHOR
259  
260 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
261  
262 =head1 BUGS
263  
264 Please report any bugs or feature requests to C<bug-file-syslogger at rt.cpan.org>, or through
265 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Syslogger>. I will be notified, and then you'll
266 automatically be notified of progress on your bug as I make changes.
267  
268 Or via Github at L<https://github.com/VVelox/File-Syslogger/issues>.
269  
270 =head1 SUPPORT
271  
272 You can find documentation for this module with the perldoc command.
273  
274 perldoc File::Syslogger
275  
276  
277 You can also look for information at:
278  
279 =over 4
280  
281 =item * GitHub
282  
283 L<https://github.com/VVelox/File-Syslogger>
284  
285 =item * RT: CPAN's request tracker (report bugs here)
286  
287 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Syslogger>
288  
289 =item * Search CPAN
290  
291 L<https://metacpan.org/release/File-Syslogger>
292  
293 =back
294  
295  
296 =head1 ACKNOWLEDGEMENTS
297  
298  
299 =head1 LICENSE AND COPYRIGHT
300  
301 This software is Copyright (c) 2025 by Zane C. Bowers-Hadley.
302  
303 This is free software, licensed under:
304  
305 The Artistic License 2.0 (GPL Compatible)
306  
307  
308 =cut
309  
310 1 1; # End of File::Syslogger
↑ Summary