Saftpresse/Plugin/Syslog.pm

filter: Statementstmt 25.0% 11 / 44 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 33.3% 2 / 6 Podpod 50.0% 1 / 2 totaltotal 16.0% 14 / 87 SCAR 49.0
CC
14
Cov
15%
parse_rfc3164_line
156.0
process
12.0
__ANON__
2.0
CRAP 134.9
« Saftpresse/Input/RELP/Frame.pm ↑ Summary Saftpresse/Config.pm »
1 package Log::Saftpresse::Plugin::Syslog;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: syslog server input plugin for saftpresse
6 1 our $VERSION = '1.5'; # VERSION
7  
8 1 extends 'Log::Saftpresse::Plugin';
9  
10 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub process {
13 0 ▶ my ( $self, $stash ) = @_;
Subroutine process: not called CC=3 SCAR=24.8
Pod documented
14 0 my $line = $stash->{'message'};
15 0 ▶ if( ! defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
16 0 return;
17 }
18 0 $line =~ s/[\r\n]*$//;
19 0 my $event = $self->parse_rfc3164_line( $line );
20 0 ▶ if( defined $event ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event) 0 0 0
21 0 $self->incr_one('events', 'by_host', $event->{'host'} );
22 0 $self->incr_one('events', 'by_program', $event->{'program'} );
23 0 @$stash{ keys %$event } = values %$event;
24 }
25  
26 0 return;
27 }
28  
29 has priorities => (
30 is => 'ro', isa => 'ArrayRef', lazy => 1,
31 default => sub { [
32 0 ▶ 'emerg',
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 'alert',
34 'crit',
35 'error',
36 'warn',
37 'notice',
38 'info',
39 'debug',
40 ] },
41 1 );
42  
43 has facilities => (
44 is => 'ro', isa => 'ArrayRef', lazy => 1,
45 default => sub { [
46 0 ▶ 'kernel',
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 'user',
48 'mail',
49 'daemon',
50 'auth',
51 'syslog',
52 'printer',
53 'news',
54 'uucp',
55 'cron',
56 'authpriv',
57 'ftp',
58 'ntp',
59 'audit',
60 'alert',
61 'clock',
62 'local0',
63 'local1',
64 'local2',
65 'local3',
66 'local4',
67 'local5',
68 'local6',
69 'local7',
70 ] },
71 1 );
72  
73 sub parse_rfc3164_line {
74 0 ▶ my ( $self, $line ) = @_;
Subroutine parse_rfc3164_line: not called CC=12 SCAR=50.5
Pod undocumented
75 0 my ( $d, $time_str, $host, $proc, $pid, $message ) =
76 $line =~ m/^<(\d+)>([A-Z][a-z]{2} [\d ]\d \d\d:\d\d:\d\d|\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d+\+\d\d:\d\d) ([^ ]+) ([^\[]+)(?:\[(\d+)\])?: (.*)$/;
77 0 ▶ if( ! defined $d || ! defined $time_str || ! defined $host || ! defined $proc || ! defined $message ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $d and defined $time_str and defined $host and defined $proc and defined $message) 0 0 0
Condition0%0/12
defined $d and defined $time_str and defined $host and defined $proc and defined $message
!ll&&!rl&&r
000
defined $d and defined $time_str and defined $host and defined $proc
!ll&&!rl&&r
000
defined $d and defined $time_str and defined $host
!ll&&!rl&&r
000
defined $d and defined $time_str
!ll&&!rl&&r
000
MC/DC0%0/5
defined $d and defined $time_str and defined $host and defined $proc and defined $message
defined $d defined $time_str defined $host defined $proc defined $message
Truth table0%0/6
A and B and C and D and E
A= defined $dB= defined $time_strC= defined $hostD= defined $procE= defined $message
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
78 0 return;
79 }
80 0 my $priority = $self->priorities->[ $d & 7 ];
81 0 my $facility = $self->facilities->[ $d >> 3 ];
82  
83 0 my $time;
84 0 ▶ if( $time_str =~ /^\d{4}-\d\d-\d\dT/ ) { # like 2015-05-29T15:15:55.716831+02:00
Branch: 2 branches
BranchTrueFalseTotal
if ($time_str =~ /^\d{4}-\d\d-\d\dT/) { } 0 0 0
elsif ($time_str =~ /^[A-Z][a-z]{2} /) { } 0 0 0
85 0 $time_str =~ s/\.\d{6}//; # remove microseconds
86 0 $time_str =~ s/:(\d\d)$/$1/; # remove : from zone
87 0 eval { $time = Time::Piece->strptime($time_str, "%Y-%m-%dT%H:%M:%S%z"); };
88 } elsif( $time_str =~ /^[A-Z][a-z]{2} / ) { # like May 29 15:27:32
89 0 eval { $time = Time::Piece->strptime($time_str, "%b %e %H:%M:%S"); };
90 0 my $now = Time::Piece->new;
91 # guess year
92 0 ▶ if( $time->mon > $now->mon ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time->mon > $now->mon) { } 0 0 0
93 # Time::Piece->year is ro :-/
94 0 $time->[5] = $now->[5] - 1;
95 } else {
96 0 $time->[5] = $now->[5];
97 }
98 } else {
99 0 return; # unknown date format :-/
100 }
101  
102 return {
103 0 ▶ defined $priority ? (priority => $priority) : (),
Branch: 3 branches
BranchTrueFalseTotal
defined $priority ? : 0 0 0
defined $facility ? : 0 0 0
defined $pid ? : 0 0 0
104 defined $facility ? (facility => $facility) : (),
105 time => $time,
106 host => $host,
107 program => $proc,
108 defined $pid ? ( pid => $pid ) : (),
109 message => $message,
110 };
111 }
112  
113 1 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 Log::Saftpresse::Plugin::Syslog - syslog server input plugin for saftpresse
124  
125 =head1 VERSION
126  
127 version 1.5
128  
129 =head1 AUTHOR
130  
131 Markus Benning <ich@markusbenning.de>
132  
133 =head1 COPYRIGHT AND LICENSE
134  
135 This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
136  
137 This is free software, licensed under:
138  
139 The GNU General Public License, Version 2, June 1991
140  
141 =cut
« Saftpresse/Input/RELP/Frame.pm ↑ Summary Saftpresse/Config.pm »