lib/Parse/Syslog/Line.pm

filter: Statementstmt 83.3% 230 / 276 Branchbran 66.2% 98 / 148 Conditioncond 52.2% 23 / 44 MC/DCmcdc 28.5% 8 / 28 Subroutinesub 77.2% 17 / 22 Podpod 100% 8 / 8 totaltotal 73.0% 384 / 526 CC 95 SCAR 49.3
CC
95
Cov
75%
parse_syslog_line
178.2
parse_syslog_lines
30.0
set_syslog_timezone
3.2
CRAP 138.3
↑ Summary script/parse-syslog-line.pl »
1 # ABSTRACT: Simple syslog line parser
2  
3 package Parse::Syslog::Line;
4  
5 5 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Hash::Merge::Simple qw( dclone_merge );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Module::Load qw( load );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Module::Loaded qw( is_loaded );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use POSIX qw( strftime tzset );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Ref::Util qw( is_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # RECOMMEND PREREQ: Cpanel::JSON::XS
20  
21 5 our $VERSION = '6.4';
22  
23 # Default for Handling Parsing
24 5 our $DateParsing = 1;
25 5 our $EpochCreate = 1;
26  
27 5 our $ExtractProgram = 1;
28 5 our $AutoDetectJSON = 0;
29 5 our $AutoDetectKeyValues = 0;
30 5 our $PruneRaw = 0;
31 5 our $PruneEmpty = 0;
32 5 our @PruneFields = ();
33 5 our $FmtDate;
34 5 our $TimeMomentFormatString = "%FT%T%f%z";
35  
36 # RFC-5424 Parsing
37 5 our $RFC5424StructuredData = 1;
38 5 our $RFC5424StructuredDataStrict = 0;
39  
40 # DEPRECATED Settings
41 5 our $DateTimeCreate;
42 5 our $HiResFmt;
43 5 our $OutputTimeZone;
44 5 our $NormalizeToUTC;
45  
46  
47 5 my %INT_PRIORITY = (
48 'emerg' => 0,
49 'alert' => 1,
50 'crit' => 2,
51 'err' => 3,
52 'warn' => 4,
53 'notice' => 5,
54 'info' => 6,
55 'debug' => 7,
56 );
57  
58 5 my %INT_FACILITY = (
59 #
60 # POSIX Facilities
61 'kern' => 0 << 3,
62 'user' => 1 << 3,
63 'mail' => 2 << 3,
64 'daemon' => 3 << 3,
65 'auth' => 4 << 3,
66 'syslog' => 5 << 3,
67 'lpr' => 6 << 3,
68 'news' => 7 << 3,
69 'uucp' => 8 << 3,
70 'cron' => 9 << 3,
71 'authpriv' => 10 << 3,
72 'ftp' => 11 << 3,
73 #
74 # Local Reserved
75 'local0' => 16 << 3,
76 'local1' => 17 << 3,
77 'local2' => 18 << 3,
78 'local3' => 19 << 3,
79 'local4' => 20 << 3,
80 'local5' => 21 << 3,
81 'local6' => 22 << 3,
82 'local7' => 23 << 3,
83 #
84 # Apple Additions
85 'netinfo' => 12 << 3,
86 'remoteauth' => 13 << 3,
87 'install' => 14 << 3,
88 'ras' => 15 << 3,
89 'launchd' => 24 << 3,
90 );
91  
92 5 const our %LOG_PRIORITY => (
93 %INT_PRIORITY,
94 reverse(%INT_PRIORITY),
95 );
96  
97 5 const our %LOG_FACILITY => (
98 %INT_FACILITY,
99 reverse(%INT_FACILITY),
100 );
101  
102 5 const our %CONV_MASK => (
103 priority => 0x07,
104 facility => 0x03f8,
105 );
106  
107  
108 5 our @ISA = qw(Exporter);
109 5 our @EXPORT = qw(parse_syslog_line psl_enable_sdata);
110 5 our @EXPORT_OK = qw(
111 parse_syslog_line
112 parse_syslog_lines
113 psl_enable_sdata
114 preamble_priority preamble_facility
115 %LOG_FACILITY %LOG_PRIORITY
116 get_syslog_timezone set_syslog_timezone
117 use_utc_syslog
118 );
119 5 our %EXPORT_TAGS = (
120 constants => [ qw( %LOG_FACILITY %LOG_PRIORITY ) ],
121 preamble => [ qw(preamble_priority preamble_facility) ],
122 with_timezones => [ qw(parse_syslog_line set_syslog_timezone get_syslog_timezone use_utc_syslog) ],
123 );
124  
125 # Regex to Extract Data
126 5 const my %RE => (
127 IPv4 => qr/(?>(?:[0-9]{1,3}\.){3}[0-9]{1,3})/,
128 preamble => qr/^\<(\d+)\>(\d{0,2}(?=\s))?\s*/,
129 date_named_capture => qr/
130 (?:(?<year>\d{4})\s)? # Option Year: YYYY
131 (?<date> # Whole String
132 (?<month>[A-Za-z]{3}) # Month: Mmm
133 \s+
134 (?<day>[0-9]+) # Day: DD
135 \s+
136 (?<hour>[0-9]{1,2})
137 :(?<minute>[0-9]{2})
138 :(?<second>[0-9]{2})
139 (?:\. # Time: .DDD millisecond or .DDDDDD microsecond resolution
140 (?<highprecision>(?:[0-9]{3}){1,2})
141 )?
142 )
143 /x,
144 date => qr/
145 (?:(\d{4})\s)? # Option Year: YYYY --> 1
146 ( # Whole String -------> 2
147 ([A-Za-z]{3}) # Month: Mmm -------> 3
148 \s+
149 ([0-9]+) # Day: DD ------------> 4
150 \s+
151 ([0-9]{1,2}) # Hour: HH -----------> 5
152 :([0-9]{2}) # Minute: MM ---------> 6
153 :([0-9]{2}) # Second: SS ---------> 7
154 (?:\. # Time: .DDD millisecond or .DDDDDD microsecond resolution
155 ((?:[0-9]{3}){1,2}) # Highprecision --> 8
156 )?
157 )
158 /x,
159 date_long => qr/
160 (?:[0-9]{4}\s+)? # Year: Because, Cisco
161 ([.*])? # Cisco adds a * for no ntp, and a . for configured but out of sync
162 [a-zA-Z]{3}\s+[0-9]+ # Date: Jan 1
163 (?:\s+[0-9]{4})? # Year: Because, Cisco
164 \s+ # Date Separator: spaces
165 [0-9]{1,2}(?:\:[0-9]{2}){1,2} # Time: HH:MM or HH:MM:SS
166 (?:\.[0-9]{3,6})? # Time: .DDD(DDD) ms resolution
167 (?:\s+[A-Z]{3,4})? # Timezone, ZZZ or ZZZZ
168 (?:\:?) # Cisco adds a : after the second timestamp
169 /x,
170 date_iso8601 => qr/(
171 [0-9]{4}-[0-9]{2}-[0-9]{2} # Date: YYYY-MM-DD
172 (?:\s|T) # Separato: T or ' '
173 [0-9]{2}:[0-9]{2}:[0-9]{2} # Time: HH:MM:SS
174 \S* # Grab the rest since this looks like a date
175 )/x,
176 date_iso8601_capture => qr/(?<date>
177 (?<year>[0-9]{4}) # Date: YYYY-MM-DD
178 -(?<month>[0-9]{2})
179 -(?<day>[0-9]{2})
180 (?:\s|T) # Separato: T or ' '
181 (?<hour>[0-9]{2}) # Time: HH:MM:SS
182 :(?<minute>[0-9]{2})
183 :(?<second>[0-9]{2})
184 (?:\. # Time: .DDD millisecond or .DDDDDD microsecond resolution
185 (?<highprecision>(?:[0-9]{3}){1,2})
186 )?
187 (?<offset>[Zz]| # UTC Offset +DD:MM or 'Z' indicating UTC-0
188 (?<offset_sign>[+\-])
189 (?<offset_hours>[0-9]{2})
190 \:
191 (?<offset_minutes>[0-9]{2})
192 )
193 )/x,
194 host => qr/\s*([^:\s]+)\s+/,
195 cisco_detection => qr/\s*[0-9]*:\s+/,
196 program_raw => qr/\s*([^\[][^:]+)(:|\s-)\s+/,
197 program_name => qr/(.[^\[\(\ ]*)(.*)/,
198 program_sub => qr/(?>\(([^\)]+)\))/,
199 program_pid => qr/(?>\[([^\]]+)\])/,
200 program_netapp => qr/(?>\[([^\]]+)\]:\s*)/,
201 kvdata => qr/
202 (?:^|\b) # Start from beginning or a word boundary
203 \K # Keep everything to the left, don't include in $&
204 (?> # Start atomic match, ie, disable back tracking
205 ([a-zA-Z\.0-9\-_@]+) # An "SDID", ie "\w" plus '.' and '-'
206 ) # RETURN: Key
207 = # Literal '='
208 (
209 \S+ # Any non-space string
210 (?:\s+\S+) # Clustering, non-grouping, space followed by one or more strings
211 *? # Not greedy
212 ) # RETURN: Value, could be one word, or several
213 (?= # Zero width positive look-ahead
214 (?: # Clustering, non-grouping group one of:
215 \s*[,;(\[] # Space, comma, semicolon, open bracket or paren
216 |$ # End of string
217 |\s+[a-zA-Z\.0-9\-_]+= # A word, followed by an equal sign
218 )
219 )
220 /x,
221 rfc_sdata_extract => qr/
222 (?> # Disable backtracking since it won't help us
223 (?:^|\s)
224 \[
225 (?! # Zero width negative look ahead
226 [^=]+ # A string that doesn't contain '='
227 \] # followed by a ]
228 )
229 ([^\]]+) # RETURN: all non] characters
230 \]
231 )
232 /x,
233 rfc_sdata_strict => qr/
234 ^
235 (?>
236 \[
237 (
238 (?:
239 (?:timeQuality|origin|meta)
240 |(?:[a-zA-Z0-9\.\-]+@[0-9]+)
241 )
242 (?:
243 \s
244 [a-zA-Z0-9\.\-]+="(?:[^"\\]++|\\.)*+"
245 )+
246 )
247 \]
248 )
249 /x,
250 sysword => qr/[a-zA-Z0-9\.\-]+/,
251 quotedstring => qr/"(?:[^"\\]++|\\.)*+"/,
252 );
253  
254 # For Date Translations
255 5 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
256 5 my %MoY;
257 5 @MoY{@MoY} = 0..11;
258  
259  
260  
261 5 my $SYSLOG_TIMEZONE = '';
262  
263 111 ▶sub parse_syslog_line {
Subroutine parse_syslog_line: called CC=89 SCAR=51.8
Pod documented
264 111 my ($raw_string) = @_;
265  
266 # State Variables
267 111 state $CpanelJSONXSWarning = 0;
268 111 ▶ state $DisableWarnings = $ENV{PARSE_SYSLOG_LINE_QUIET} || $ENV{TEST_ACTIVE} || $ENV{TEST2_ACTIVE};
Condition33%2/6
$ENV{'PARSE_SYSLOG_LINE_QUIET'} || $ENV{'TEST_ACTIVE'} || $ENV{'TEST2_ACTIVE'}
l!l&&r!l&&!r
300
$ENV{"PARSE_SYSLOG_LINE_QUIET"} or $ENV{"TEST_ACTIVE"}
l!l&&r!l&&!r
030
MC/DC0%0/3
$ENV{'PARSE_SYSLOG_LINE_QUIET'} || $ENV{'TEST_ACTIVE'} || $ENV{'TEST2_ACTIVE'}
$ENV{"PARSE_SYSLOG_LINE_QUIET"} $ENV{"TEST_ACTIVE"} $ENV{'TEST2_ACTIVE'}
Truth table25%1/4
A or B || C
A= $ENV{"PARSE_SYSLOG_LINE_QUIET"}B= $ENV{"TEST_ACTIVE"}C= $ENV{'TEST2_ACTIVE'}
ABCresult
1XX1
01X1
0011
0000
269  
270 # Initialize everything to undef
271 111 my %msg = ();
272 111 ▶ $msg{message_raw} = $raw_string unless $PruneRaw;
Branch: 1 branch
BranchTrueFalseTotal
unless $Parse::Syslog::Line::PruneRaw 102 9 111
273  
274 # Lines that begin with a space aren't syslog messages, skip
275 111 ▶ return \%msg if $raw_string =~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $raw_string =~ /^\s/u 0 111 111
276  
277 # grab the preamble:
278 111 ▶ if( $raw_string =~ s/^$RE{preamble}//o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_string =~ s/^$RE{"preamble"}//ou) 72 39 111
279 # Cast to integer
280 72 $msg{preamble} = int $1;
281 72 ▶ $msg{version} = int $2 if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 6 66 72
282  
283 # Extract Integers
284 72 $msg{priority_int} = $msg{preamble} & $CONV_MASK{priority};
285 72 $msg{facility_int} = $msg{preamble} & $CONV_MASK{facility};
286  
287 # Lookups
288 72 $msg{priority} = $LOG_PRIORITY{ $msg{priority_int} };
289 72 $msg{facility} = $LOG_FACILITY{ $msg{facility_int} };
290 }
291  
292 #
293 # Handle Date/Time
294 111 my %date = ();
295 111 ▶ if( $raw_string =~ s/^$RE{date}//o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($raw_string =~ s/^$RE{'date'}//ou) { } 91 20 111
elsif ($raw_string =~ s/^$RE{'date_iso8601'}//ou) { } 20 0 20
296 # Positional
297 91 $msg{datetime_raw} = $2;
298 91 ▶ $msg{datetime_raw} .= " $1" if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 6 85 91
299 # Copy into the date hash
300 91 %date = (
301 year => $1,
302 date => $2,
303 month => $3,
304 day => $4,
305 hour => $5,
306 minute => $6,
307 second => $7,
308 highprecision => $8,
309 );
310 91 $date{month_abbr} = 1;
311 }
312 elsif( $raw_string =~ s/^$RE{date_iso8601}//o) {
313 20 $msg{datetime_raw} = $1;
314 }
315  
316 # Handle Date Parsing
317 111 ▶ if( exists $msg{datetime_raw} && length $msg{datetime_raw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $msg{"datetime_raw"} and length $msg{"datetime_raw"}) 111 0 111
Condition33%1/3
exists $msg{"datetime_raw"} and length $msg{"datetime_raw"}
!ll&&!rl&&r
00111
MC/DC0%0/2
exists $msg{"datetime_raw"} and length $msg{"datetime_raw"}
exists $msg{"datetime_raw"} length $msg{"datetime_raw"}
Truth table33%1/3
A and B
A= exists $msg{"datetime_raw"}B= length $msg{"datetime_raw"}
ABresult
0X0
100
111
318 111 ▶ if ( $DateParsing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Syslog::Line::DateParsing) 111 0 111
319 # if User wants to fight with dates themselves, let them :)
320 111 ▶ if( $FmtDate && ref $FmtDate eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Parse::Syslog::Line::FmtDate and ref $Parse::Syslog::Line::FmtDate eq 'CODE') { } 27 84 111
elsif ($msg{'datetime_raw'}) { } 84 0 84
Condition66%2/3
$Parse::Syslog::Line::FmtDate and ref $Parse::Syslog::Line::FmtDate eq "CODE"
!ll&&!rl&&r
84027
MC/DC50%1/2
$Parse::Syslog::Line::FmtDate and ref $Parse::Syslog::Line::FmtDate eq "CODE"
$Parse::Syslog::Line::FmtDate ref $Parse::Syslog::Line::FmtDate eq "CODE"
Truth table66%2/3
A and B
A= $Parse::Syslog::Line::FmtDateB= ref $Parse::Syslog::Line::FmtDate eq "CODE"
ABresult
0X0
100
111
321 27 @msg{qw(date time epoch datetime_str)} = $FmtDate->($msg{datetime_raw});
322 }
323 elsif ( $msg{datetime_raw} ) {
324 84 my $tm;
325 84 ▶ if ( %date ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%date) { } 68 16 84
326 68 delete $date{date};
327 68 ▶ if ( delete $date{month_abbr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $date{"month_abbr"}) 68 0 68
328 68 $date{month} = $MoY{$date{month}} + 1;
329 }
330 68 ▶ if ( my $offset = delete $date{offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $offset = delete $date{'offset'}) { } 0 68 68
331 0 ▶ if ( $offset eq 'Z' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset eq 'Z') { } 0 0 0
332 0 $date{offset} = 0;
333 }
334 else {
335 0 my $sign = delete $date{offset_sign};
336 0 my $hours = delete $date{offset_hours};
337 0 my $minutes = delete $date{offset_minutes};
338 0 ▶ $date{offset} = ($sign eq '-' ? -1 : 1)
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 0 0
339 * (( $hours * 60 ) + $minutes);
340 }
341 }
342 else {
343 68 $date{offset} = Time::Moment->now()->offset;
344 }
345 68 ▶ if( my $hp = delete $date{highprecision} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hp = delete $date{"highprecision"}) 0 68 68
346 0 my $len = length($hp);
347 0 ▶ my $v = $len <= 3 ? $hp * 1_000_000
Branch: 3 branches
BranchTrueFalseTotal
$len <= 3 ? : 0 0 0
$len <= 6 ? : 0 0 0
$len <= 9 ? : 0 0 0
348 : $len <= 6 ? $hp * 1_000
349 : $len <= 9 ? $hp
350 : 0;
351 0 ▶ $date{nanosecond} = $v if $v
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
352 }
353 68 my $has_year = length $date{year};
354 68 ▶ if ( !$has_year ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_year) 64 4 68
355 64 $date{year} = 1900 + (localtime)[5];
356 }
357 68 $tm = eval { Time::Moment->new(%date) };
358 68 ▶ if ( defined $tm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tm) 68 0 68
359 # Check that the timestamp isn't more than 1 day in the future
360 68 ▶ $tm = Time::Moment->new(%date, year => $date{year} - 1)
Branch: 1 branch
BranchTrueFalseTotal
if not $has_year and $tm->epoch > time + 86400 3 65 68
Condition100%3/3
not $has_year and $tm->epoch > time + 86400
!ll&&!rl&&r
4613
MC/DC100%2/2
not $has_year and $tm->epoch > time + 86400
not $has_year $tm->epoch > time + 86400
Truth table100%3/3
not(A) and B
A= not $has_yearB= $tm->epoch > time + 86400
ABresult
0X0
100
111
361 if !$has_year && $tm->epoch > (time + 86400);
362 }
363 }
364 else {
365 16 $tm = eval { Time::Moment->from_string($msg{datetime_raw}, lenient => 1) };
366 }
367 84 my $ltm = Time::Moment->now();
368 84 ▶ $tm ||= $ltm;
Condition33%1/3
$tm ||= $ltm
l!l&&r!l&&!r
8400
MC/DC0%0/2
$tm ||= $ltm
$tm $ltm
Truth table33%1/3
A ||= B
A= $tmB= $ltm
ABresult
1X1
011
000
369  
370 # HiRes Epoch
371 84 $msg{epoch} = $tm->strftime("%s%f");
372 # ISO8601 Timestamps
373 84 $msg{datetime_utc} = $tm->with_offset_same_instant(0)->strftime($TimeMomentFormatString) =~ s/\+00:?00$/Z/r;
374 84 $msg{datetime_local} = $tm->with_offset_same_instant($ltm->offset)->strftime($TimeMomentFormatString) =~ s/\+00:?00$/Z/r;
375 84 $msg{datetime_str} = $tm->strftime($TimeMomentFormatString) =~ s/\+00:?00$/Z/r;
376 84 $msg{tz} = $tm->strftime('%z') =~ s/\+00:?00/Z/r;
377 }
378  
379 111 ▶ if ( $msg{datetime_str} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{"datetime_str"}) 84 27 111
380 # Split this up into parts
381 84 my @parts = split /[ T]/, $msg{datetime_str};
382 84 $msg{date} = $parts[0];
383 84 $msg{time} = (split /[+\-Z]/, $parts[1])[0];
384  
385 # Debugging for my sanity
386 printf("TZ=%s Parsed: %s to [%s] %s D:%s T:%s O:%s\n",
387 $SYSLOG_TIMEZONE,
388 @msg{qw(datetime_raw epoch datetime_str date time tz)},
389 84 ▶ ) if $ENV{PARSE_SYSLOG_LINE_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PARSE_SYSLOG_LINE_DEBUG"} 0 84 84
390 }
391 }
392 }
393  
394 #
395 # Host Information:
396 111 ▶ if( $raw_string =~ s/^$RE{host}//o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_string =~ s/^$RE{"host"}//ou) 108 3 111
397 108 my $hostStr = $1;
398 108 my($ip) = ($hostStr =~ /($RE{IPv4})/o);
399 108 ▶ if( defined $ip && length $ip ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ip and length $ip) { } 20 88 108
elsif (length $hostStr) { } 88 0 88
Condition66%2/3
defined $ip and length $ip
!ll&&!rl&&r
88020
MC/DC50%1/2
defined $ip and length $ip
defined $ip length $ip
Truth table66%2/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
400 20 $msg{host_raw} = $hostStr;
401 20 $msg{host} = $ip;
402 }
403 elsif( length $hostStr ) {
404 88 my ($host,$domain) = split /\./, $hostStr, 2;
405 88 $msg{host_raw} = $hostStr;
406 88 $msg{host} = $host;
407 88 $msg{domain} = $domain;
408 }
409 }
410 # Check for relayed logs, grab the origin
411 111 ▶ while( $raw_string =~ /^(?:\s*[0-9]+\s+)?$RE{date_iso8601}\s+$RE{host}/go ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_string =~ /^(?:\s*[0-9]+\s+)?$RE{"date_iso8601"}\s+$RE{"host"}/gou) 6 111 117
412 6 $msg{origin} = $2;
413 6 $msg{origin_date} = $1;
414 6 $raw_string = substr($raw_string,pos($raw_string));
415 }
416  
417 # Find weird cisco dates
418 111 ▶ if( $raw_string =~ s/^$RE{cisco_detection}//o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_string =~ s/^$RE{"cisco_detection"}//ou) 22 89 111
419 # Yes, Cisco adds a second timestamp to it's messages, because ...
420 22 ▶ if( $raw_string =~ s/^$RE{date_long}//o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_string =~ s/^$RE{"date_long"}//ou) 17 5 22
421 # Cisco encodes the status of NTP in the second datestamp, so let's pass it back
422 17 ▶ if ( my $ntp = $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ntp = $1) { } 6 11 17
423 6 ▶ $msg{ntp} = $ntp eq '.' ? 'out of sync'
Branch: 2 branches
BranchTrueFalseTotal
$ntp eq '.' ? : 3 3 6
$ntp eq '*' ? : 3 0 3
424 : $ntp eq '*' ? 'not configured'
425 : 'unknown';
426 }
427 else {
428 11 $msg{ntp} = 'ok';
429 }
430 }
431 }
432  
433 #
434 # Parse the Program portion
435 111 my $progsep = ':';
436 111 ▶ if( $ExtractProgram ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Syslog::Line::ExtractProgram) { } 84 27 111
437 84 ▶ if( $raw_string =~ s/^$RE{program_raw}//o ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($raw_string =~ s/^$RE{'program_raw'}//ou) { } 77 7 84
elsif ($raw_string =~ s/$RE{'program_netapp'}//ou) { } 6 1 7
438 77 $msg{program_raw} = $1;
439 77 ▶ $progsep = $2 || '';
Condition50%1/2
$2 || ''
l!l
770
MC/DC0%0/1
$2 || ''
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
440 77 my $progStr = join ' ', grep {!exists $INT_PRIORITY{$_}} split /\s+/, $msg{program_raw};
441 77 ▶ if( $progStr =~ /^$RE{program_name}/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($progStr =~ /^$RE{"program_name"}/ou) 77 0 77
442 77 $msg{program_name} = $1;
443 77 my $remainder = $2;
444 77 ▶ if ( $remainder ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder) 28 49 77
445 28 ($msg{program_pid}) = ($remainder =~ /$RE{program_pid}/o);
446 28 ($msg{program_sub}) = ($remainder =~ /$RE{program_sub}/o);
447 28 ▶ if( !$msg{program_sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg{"program_sub"}) 28 0 28
448 28 ($msg{program_sub}) = ($remainder =~ /^(?:[\/\s])?([^\[(]+)/o);
449 }
450 }
451 77 ▶ if( $msg{program_name} !~ m{^/} && $msg{program_name} =~ tr{/}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $msg{"program_name"} =~ m[^/]u and $msg{"program_name"} =~ tr[/][]) 2 75 77
Condition100%3/3
not $msg{"program_name"} =~ m[^/]u and $msg{"program_name"} =~ tr[/][]
!ll&&!rl&&r
2732
MC/DC100%2/2
not $msg{"program_name"} =~ m[^/]u and $msg{"program_name"} =~ tr[/][]
not $msg{"program_name"} =~ m[^/]u $msg{"program_name"} =~ tr[/][]
Truth table100%3/3
not(A) and B
A= not $msg{"program_name"} =~ m[^/]uB= $msg{"program_name"} =~ tr[/][]
ABresult
0X0
100
111
452 2 @msg{qw(program_name program_sub)} = split m{/}, $msg{program_name}, 2;
453 }
454 }
455 }
456 elsif( $raw_string =~ s/$RE{program_netapp}//o ) {
457 # Check for a [host thing.subthing:level]: tag
458 # or [host:thing.subthing:level]: tag, Thanks NetApp.
459 6 my $subStr = $1;
460 6 $msg{program_raw} = qq{[$subStr]};
461 6 my ($host,$program,$level) = split /[: ]+/, $subStr;
462 6 $msg{program_name} = $program;
463 6 ▶ if(!exists $msg{priority} && exists $LOG_PRIORITY{$level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $msg{"priority"} and exists $Parse::Syslog::Line::LOG_PRIORITY{$level}) 0 6 6
Condition33%1/3
not exists $msg{"priority"} and exists $Parse::Syslog::Line::LOG_PRIORITY{$level}
!ll&&!rl&&r
600
MC/DC0%0/2
not exists $msg{"priority"} and exists $Parse::Syslog::Line::LOG_PRIORITY{$level}
not exists $msg{"priority"} exists $Parse::Syslog::Line::LOG_PRIORITY{$level}
Truth table33%1/3
not(A) and B
A= not exists $msg{"priority"}B= exists $Parse::Syslog::Line::LOG_PRIORITY{$level}
ABresult
0X0
100
111
464 0 $msg{priority} = $level;
465 0 $msg{priority_int} = $LOG_PRIORITY{$level};
466 }
467 6 $raw_string =~ s/^[ :]+//;
468 }
469 }
470 else {
471 27 $raw_string =~ s/^\s+//;
472 }
473  
474 # The left overs should be the message
475 111 $msg{content} = $raw_string;
476 111 chomp $msg{content};
477 111 ▶ $msg{message} = defined $msg{program_raw} ? "$msg{program_raw}$progsep $msg{content}" : $msg{content};
Branch: 1 branch
BranchTrueFalseTotal
defined $msg{'program_raw'} ? : 83 28 111
478  
479 # Extract RFC Structured Data
480 111 ▶ if( $RFC5424StructuredDataStrict ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Parse::Syslog::Line::RFC5424StructuredDataStrict) { } 1 110 111
elsif ($Parse::Syslog::Line::RFC5424StructuredData) { } 110 0 110
481 1 ▶ while ( $msg{content} =~ s/$RE{rfc_sdata_strict}//o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{"content"} =~ s/$RE{"rfc_sdata_strict"}//ou) 2 1 3
482 2 my $rfc_sdata = $1;
483 2 my ($sdid,$sdata) = split /\s+/, $rfc_sdata, 2;
484 2 foreach my $token ( $sdata =~ /($RE{sysword}=$RE{quotedstring})/og ) {
485 2 my ($k,$v) = split /=/, $token, 2;
486 2 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 0 2 2
487 # Trim off the quotes
488 2 $v = substr($v, 1, length($v) - 2);
489 2 $msg{SDATA}{$sdid}{$k} = $v;
490 }
491 }
492 1 $msg{content} =~ s/^\s+//;
493 }
494 elsif ( $RFC5424StructuredData ) {
495 110 ▶ while ( $msg{content} =~ s/$RE{rfc_sdata_extract}//o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{"content"} =~ s/$RE{"rfc_sdata_extract"}//ou) 39 110 149
496 39 my $rfc_sdata = $1;
497 39 ▶ my ($group) = $rfc_sdata =~ s/^([^\s=]+)\s// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$rfc_sdata =~ s/^([^\s=]+)\s//u ? : 34 5 39
498 39 foreach my $token ( $rfc_sdata =~ /($RE{sysword}=(?:$RE{quotedstring}|\S+))/og ) {
499 326 my ($k,$v) = split /=/, $token, 2;
500 326 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 0 326 326
501 # Trim off the quotes
502 326 $v =~ s/(?:^")|(?:"$)//g;
503 326 ▶ if( $group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) { } 321 5 326
504 321 $msg{SDATA}{$group}{$k} = $v;
505 } else {
506 5 $msg{SDATA}{$k} = $v;
507 }
508 }
509 # When we parse without ExtractProgram, shit gets weird.
510 # We need to restore the first space between the first semi-colon
511 # and the rest of the string
512 39 ▶ $msg{content} =~ s/:\s*/: / if $msg{SDATA};
Branch: 1 branch
BranchTrueFalseTotal
if $msg{"SDATA"} 39 0 39
513 }
514 110 ▶ $msg{content} =~ s/^\s+// if $msg{SDATA};
Branch: 1 branch
BranchTrueFalseTotal
if $msg{"SDATA"} 17 93 110
515 }
516  
517 111 ▶ if( $AutoDetectJSON && (my $pos = index($msg{content},'{')) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Syslog::Line::AutoDetectJSON and (my $pos = index($msg{"content"}, "{")) >= 0) 4 107 111
Condition66%2/3
$Parse::Syslog::Line::AutoDetectJSON and (my $pos = index($msg{"content"}, "{")) >= 0
!ll&&!rl&&r
10704
MC/DC50%1/2
$Parse::Syslog::Line::AutoDetectJSON and (my $pos = index($msg{"content"}, "{")) >= 0
$Parse::Syslog::Line::AutoDetectJSON (my $pos = index($msg{"content"}, "{")) >= 0
Truth table66%2/3
A and B
A= $Parse::Syslog::Line::AutoDetectJSONB= (my $pos = index($msg{"content"}, "{")) >= 0
ABresult
0X0
100
111
518 4 ▶ if( !$CpanelJSONXSWarning && !is_loaded('Cpanel::JSON::XS') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CpanelJSONXSWarning or &is_loaded("Cpanel::JSON::XS")) 0 4 4
Condition33%1/3
$CpanelJSONXSWarning or &is_loaded("Cpanel::JSON::XS")
l!l&&r!l&&!r
040
MC/DC0%0/2
$CpanelJSONXSWarning or &is_loaded("Cpanel::JSON::XS")
$CpanelJSONXSWarning &is_loaded("Cpanel::JSON::XS")
Truth table33%1/3
A or B
A= $CpanelJSONXSWarningB= &is_loaded("Cpanel::JSON::XS")
ABresult
1X1
011
000
519 0 ▶ warn "When using AutoDetectJSON, we recommend Cpanel::JSON::XS for performance and compatibility"
Branch: 1 branch
BranchTrueFalseTotal
unless $DisableWarnings 0 0 0
520 unless $DisableWarnings;
521 0 $CpanelJSONXSWarning = 1;
522 }
523 eval {
524 4 my $json = decode_json(substr($msg{content},$pos));
525 3 ▶ $msg{SDATA} = $msg{SDATA} ? dclone_merge($json,$msg{SDATA}) : $json;
Branch: 1 branch
BranchTrueFalseTotal
$msg{'SDATA'} ? : 2 1 3
526 3 1;
527 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $json = &decode_json(substr($msg{"content"}, $pos)); $msg{"SDATA"} = $msg{"SDATA"} ? dclone_merge($json, $msg{"SDATA"}) : $json; 1 } } or do { my $err = $@; $msg{"_json_error"} = sprintf('Failed to decode json: %s', $err) } 3 1 4
528 1 my $err = $@;
529 1 $msg{_json_error} = sprintf "Failed to decode json: %s", $err;
530 };
531 }
532 111 ▶ if( $AutoDetectKeyValues && $msg{content} =~ /(?:^|\s)[a-zA-Z\.0-9\-_]+=\S+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Syslog::Line::AutoDetectKeyValues and $msg{"content"} =~ /(?:^|\s)[a-zA-Z\.0-9\-_]+=\S+/u) 7 104 111
Condition66%2/3
$Parse::Syslog::Line::AutoDetectKeyValues and $msg{"content"} =~ /(?:^|\s)[a-zA-Z\.0-9\-_]+=\S+/u
!ll&&!rl&&r
10407
MC/DC50%1/2
$Parse::Syslog::Line::AutoDetectKeyValues and $msg{"content"} =~ /(?:^|\s)[a-zA-Z\.0-9\-_]+=\S+/u
$Parse::Syslog::Line::AutoDetectKeyValues $msg{"content"} =~ /(?:^|\s)[a-zA-Z\.0-9\-_]+=\S+/u
Truth table66%2/3
A and B
A= $Parse::Syslog::Line::AutoDetectKeyValuesB= $msg{"content"} =~ /(?:^|\s)[a-zA-Z\.0-9\-_]+=\S+/u
ABresult
0X0
100
111
533 7 my %sdata = ();
534 7 ▶ while( $msg{content} =~ /$RE{kvdata}/og ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg{"content"} =~ /$RE{"kvdata"}/gou) 75 7 82
535 75 my ($k,$v) = ($1,$2);
536 # Remove Trailing Characters
537 75 $v =~ s/[)\]>,;'"]+$//;
538 # Remove Leading Characters
539 75 $v =~ s/^[(\[<'"]+//;
540 75 ▶ if( exists $sdata{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sdata{$k}) { } 0 75 75
541 0 ▶ if( is_arrayref($sdata{$k}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($sdata{$k})) { } 0 0 0
542 0 push @{ $sdata{$k} }, $v;
543 }
544 else {
545 # Auto Promote to an Array Ref
546 0 $sdata{$k} = [ $sdata{$k}, $v ];
547 }
548 }
549 else {
550 75 $sdata{$k} = $v;
551 }
552 }
553 7 ▶ if ( %sdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%sdata) 7 0 7
554 7 ▶ $msg{SDATA} = $msg{SDATA} ? dclone_merge(\%sdata,$msg{SDATA}) : \%sdata;
Branch: 1 branch
BranchTrueFalseTotal
$msg{'SDATA'} ? : 0 7 7
555 }
556 }
557  
558 111 ▶ if( $PruneRaw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Syslog::Line::PruneRaw) 9 102 111
559 9 delete $msg{$_} for grep { $_ =~ /_raw$/ } keys %msg;
560 }
561 111 ▶ if( $PruneEmpty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::Syslog::Line::PruneEmpty) 9 102 111
562 9 delete $msg{$_} for grep { !length $msg{$_} } keys %msg;
563 }
564 111 ▶ if( @PruneFields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Parse::Syslog::Line::PruneFields) 0 111 111
565 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
566 0 delete $msg{$_} for @PruneFields;
567 }
568 111 ▶ delete $msg{epoch} if exists $msg{epoch} and !$EpochCreate;
Branch: 1 branch
BranchTrueFalseTotal
if exists $msg{"epoch"} and not $Parse::Syslog::Line::EpochCreate 0 111 111
Condition33%1/3
exists $msg{"epoch"} and not $Parse::Syslog::Line::EpochCreate
!ll&&!rl&&r
01110
MC/DC0%0/2
exists $msg{"epoch"} and not $Parse::Syslog::Line::EpochCreate
exists $msg{"epoch"} not $Parse::Syslog::Line::EpochCreate
Truth table33%1/3
A and not(B)
A= exists $msg{"epoch"}B= not $Parse::Syslog::Line::EpochCreate
ABresult
0X0
100
111
569  
570 #
571 # Return our hash reference!
572 111 return \%msg;
573 }
574  
575  
576 {
577 5 my $buffer = '';
578 0 ▶ sub parse_syslog_lines {
Subroutine parse_syslog_lines: not called CC=5 SCAR=34.0
Pod documented
579 0 my @lines = map { split /\r?\n/, $_ } grep { defined } @_;
580 0 my @structured = ();
581 0 ▶ if( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) { } 0 0 0
582 0 ▶ while( my $line = shift @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 0 0 0
583 0 ▶ if( $line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s/u) { } 0 0 0
584 0 $buffer .= "\n" . $line;
585 0 next;
586 }
587 else {
588 0 push @structured, parse_syslog_line($buffer);
589 0 $buffer = $line;
590 }
591 }
592 }
593 else {
594 # grab the remaining buffer
595 0 ▶ push @structured, parse_syslog_line($buffer)
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer 0 0 0
596 if length $buffer;
597 0 $buffer = '';
598 }
599 0 return @structured;
600 }
601  
602 }
603  
604  
605 5 ▶sub psl_enable_sdata {
Subroutine psl_enable_sdata: not called CC=1 SCAR=3.5
Pod documented
606 0 $AutoDetectJSON = 1;
607 0 $AutoDetectKeyValues = 1;
608 0 $RFC5424StructuredData = 1;
609 }
610  
611  
612 0 ▶sub preamble_priority {
Subroutine preamble_priority: not called CC=1 SCAR=6.9
Pod documented
613 0 my $preamble = int shift;
614  
615 0 my %hash = (
616 preamble => $preamble,
617 );
618  
619 0 $hash{as_int} = $preamble & $CONV_MASK{priority};
620 0 $hash{as_text} = $LOG_PRIORITY{ $hash{as_int} };
621  
622 0 return \%hash;
623 }
624  
625  
626 0 ▶sub preamble_facility {
Subroutine preamble_facility: not called CC=1 SCAR=6.9
Pod documented
627 0 my $preamble = int shift;
628  
629 0 my %hash = (
630 preamble => $preamble,
631 );
632  
633 0 $hash{as_int} = $preamble & $CONV_MASK{facility};
634 0 $hash{as_text} = $LOG_FACILITY{ $hash{as_int} };
635  
636 0 return \%hash;
637  
638 }
639  
640 10 ▶sub set_syslog_timezone {
Subroutine set_syslog_timezone: called CC=3 SCAR=11.8
Pod documented
641 10 my ( $tz_name ) = @_;
642  
643 10 ▶ if( defined $tz_name && length $tz_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tz_name and length $tz_name) 10 0 10
Condition33%1/3
defined $tz_name and length $tz_name
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $tz_name and length $tz_name
defined $tz_name length $tz_name
Truth table33%1/3
A and B
A= defined $tz_nameB= length $tz_name
ABresult
0X0
100
111
644 10 $ENV{TZ} = $SYSLOG_TIMEZONE = $tz_name;
645 10 tzset();
646 }
647  
648 10 return $SYSLOG_TIMEZONE;
649 }
650  
651 0 ▶sub get_syslog_timezone {
Subroutine get_syslog_timezone: not called CC=1 SCAR=6.9
Pod documented
652 0 return $SYSLOG_TIMEZONE;
653 }
654  
655 # If you have a syslog which logs dates in UTC, then processing will be much, much faster
656 1 ▶sub use_utc_syslog {
Subroutine use_utc_syslog: called CC=1 SCAR=0.0
Pod documented
657 1 set_syslog_timezone('UTC');
658 1 return;
659 }
660  
661 5 1; # End of Parse::Syslog::Line
662  
663 __END__
↑ Summary script/parse-syslog-line.pl »