blib/lib/Monitoring/Sneck/Checks/log.pm

filter: Statementstmt 100% 98 / 98 Branchbran 88.1% 37 / 42 Conditioncond 93.5% 29 / 31 MC/DCmcdc 88.2% 15 / 17 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 95.6% 200 / 209 CC 37 SCAR 27.9
CC
37
Cov
96%
check
27.0
read_lines
5.1
validate
5.0
CRAP 16.3
« blib/lib/Monitoring/Sneck/Checks/accounts.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/thermal_throttle.pm »
1 package Monitoring::Sneck::Checks::log;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Slurp qw(read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::log - checks new lines in a log for a regex
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 =head1 SYNOPSIS
22  
23 auth_fails|perl:log -f /var/log/auth.log -r 'Failed password' -c 10
24 kernel_errors|perl:log -f /var/log/messages -r 'I/O error|Medium Error' -x 'cd0' -H 3600
25  
26 =head1 DESCRIPTION
27  
28 Reads the lines added to a log since it was last run and counts those
29 matching any --regex and none of --exclude, the same as check_log. The
30 count is checked against the ranges, so by default any is WARNING.
31  
32 Where it got to is kept in a state file, one per log and set of
33 regexes and excludes unless --state is given. A log that was rotated,
34 a new inode,
35 or truncated, smaller than before, is read from the start. The first
36 time, it starts at the end, so what was already there is not counted,
37 unless --from-start is given. Only whole lines are read, so a line
38 still being written is left for next time.
39  
40 A match is only seen by the one run that reads it, so the next run is
41 OK again, which a poll can easily miss. --hold keeps reporting the
42 last matches for that many seconds after.
43  
44 Running it by hand, such as through sneck_check, moves the state on as
45 well, so give it a --state of its own for that.
46  
47 Not being able to read the log or the state, or write the state, is
48 UNKNOWN.
49  
50 =head1 OPTIONS
51  
52 - -f, --file :: The log. Required.
53  
54 - -r, --regex :: A regex for lines to count. May be given more than
55 once, any matching is enough. Required.
56  
57 - -x, --exclude :: A regex for lines not to count. May be given more
58 than once.
59  
60 - -w, --warning :: Range for the number of lines.
61 Default :: 0
62  
63 - -c, --critical :: Range for the number of lines.
64  
65 - -H, --hold :: Seconds to keep reporting matches for.
66 Default :: 0
67  
68 - -n, --lines :: How many matching lines to list after the status
69 line, once it is not OK.
70 Default :: 5
71  
72 - -s, --state :: The state file.
73 Default :: under /var/cache/sneck.checks, named by --file, --regex, and --exclude
74  
75 - --from-start :: The first time, read the whole log.
76  
77 =cut
78  
79 17 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
80 return (
81 17 'file|f=s', 'regex|r=s@', 'exclude|x=s@', 'warning|w=s', 'critical|c=s',
82 'hold|H=i', 'lines|n=i', 'state|s=s', 'from-start'
83 );
84 }
85  
86 17 ▶sub defaults { return { warning => '0', hold => 0, lines => 5 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
87  
88 17 ▶sub required { return ( 'file', 'regex' ) }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
89  
90 17 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
91  
92 17 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
93 17 my ( $self, $options ) = @_;
94  
95 17 my @problems;
96 17 foreach my $name ( 'regex', 'exclude' ) {
97 34 ▶ foreach my $regex ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
2113
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
98 21 ▶ push( @problems, '--' . $name . ' "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$regex/u; 1 } } 1 20 21
99 }
100 }
101  
102 17 return @problems;
103 } ## end sub validate
104  
105 =head1 METHODS
106  
107 =head2 check
108  
109 Does the check.
110  
111 =cut
112  
113 15 ▶sub check {
Subroutine check: called CC=27 SCAR=33.0
Pod documented
114 15 my ($self) = @_;
115  
116 15 my $file = $self->opt('file');
117 15 my $state_file = $self->state_file;
118 15 my $state = $self->read_state($state_file);
119 15 ▶ return $self->finish( $self->UNKNOWN, 'can not read the state in ' . $state_file ) if !defined($state);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 1 14 15
120  
121 14 my @stat = stat($file);
122 14 ▶ return $self->finish( $self->UNKNOWN, 'can not read ' . $file . ', ' . $! ) if !defined( $stat[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stat[0] 1 13 14
123 13 my $first_time = !defined( $state->{inode} );
124 13 ▶ my $offset = $state->{offset} // 0;
Condition100%2/2
$state->{"offset"} // 0
l!l
76
MC/DC100%1/1
$state->{"offset"} // 0
$state->{"offset"}
Truth table100%2/2
A
A= $state->{"offset"}
Aresult
11
00
125 13 ▶ if ( $first_time && !$self->opt('from-start') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($first_time and not $self->opt('from-start')) { } 5 8 13
elsif (not $first_time and $state->{'inode'} != $stat[1] || $state->{'device'} != $stat[0] || $stat[7] < $offset) { } 2 7 9
Condition100%3/3
$first_time and not $self->opt("from-start")
!ll&&!rl&&r
715
MC/DC100%2/2
$first_time and not $self->opt("from-start")
$first_time not $self->opt("from-start")
Truth table100%3/3
A and not(B)
A= $first_timeB= not $self->opt("from-start")
ABresult
0X0
100
111
126 5 $offset = $stat[7];
127 ▶ } elsif ( !$first_time && ( $state->{inode} != $stat[1] || $state->{device} != $stat[0] || $stat[7] < $offset ) ) {
Condition88%8/9
not $first_time and $state->{"inode"} != $stat[1] || $state->{"device"} != $stat[0] || $stat[7] < $offset
!ll&&!rl&&r
152
$state->{"inode"} != $stat[1] or $state->{"device"} != $stat[0] or $stat[7] < $offset
l!l&&r!l&&!r
115
$state->{"inode"} != $stat[1] or $state->{"device"} != $stat[0]
l!l&&r!l&&!r
106
MC/DC75%3/4
not $first_time and $state->{"inode"} != $stat[1] || $state->{"device"} != $stat[0] || $stat[7] < $offset
not $first_time $state->{"inode"} != $stat[1] $state->{"device"} != $stat[0] $stat[7] < $offset
Truth table80%4/5
not(A) and B or C or D
A= not $first_timeB= $state->{"inode"} != $stat[1]C= $state->{"device"} != $stat[0]D= $stat[7] < $offset
ABCDresult
0XXX0
10000
11XX1
101X1
10011
128 2 $offset = 0;
129 }
130  
131 13 my ( $lines, $read_to ) = $self->read_lines( $file, $offset );
132 13 ▶ return $self->finish( $self->UNKNOWN, 'can not read ' . $file . ', ' . $read_to ) if !defined($lines);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lines 0 13 13
133  
134 13 my @regexes = @{ $self->opt('regex') };
135 13 ▶ my @excludes = @{ $self->opt('exclude') // [] };
Condition100%2/2
$self->opt("exclude") // []
l!l
58
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
Aresult
11
00
136 13 my @matched;
137 13 foreach my $line ( @{$lines} ) {
138 15 ▶ next if !grep { $line =~ /$_/ } @regexes;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$line =~ /$_/u;} @regexes 1 14 15
139 14 ▶ next if grep { $line =~ /$_/ } @excludes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$line =~ /$_/u;} @excludes 1 13 14
140 13 push( @matched, $line );
141 }
142  
143 13 my $now = time;
144 13 my %new_state = ( inode => $stat[1], device => $stat[0], offset => $read_to );
145 13 ▶ if ( defined( $matched[0] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $matched[0]) { } 6 7 13
elsif (defined $state->{'matched_at'} and $now - $state->{'matched_at'} < $self->opt('hold')) { } 1 11 12
146 6 ▶ %new_state = (
Branch: 1 branch
BranchTrueFalseTotal
$#matched < 49 ? : 6 0 6
147 %new_state,
148 matched_at => $now,
149 matched => scalar(@matched),
150 matched_lines => [ @matched[ 0 .. ( $#matched < 49 ? $#matched : 49 ) ] ]
151 );
152 ▶ } elsif ( defined( $state->{matched_at} ) && $now - $state->{matched_at} < $self->opt('hold') ) {
Condition100%3/3
defined $state->{"matched_at"} and $now - $state->{"matched_at"} < $self->opt("hold")
!ll&&!rl&&r
511
MC/DC100%2/2
defined $state->{"matched_at"} and $now - $state->{"matched_at"} < $self->opt("hold")
defined $state->{"matched_at"} $now - $state->{"matched_at"} < $self->opt("hold")
Truth table100%3/3
A and B
A= defined $state->{"matched_at"}B= $now - $state->{"matched_at"} < $self->opt("hold")
ABresult
0X0
100
111
153 1 %new_state = ( %new_state, map { $_ => $state->{$_} } ( 'matched_at', 'matched', 'matched_lines' ) );
154 }
155 13 my $problem = $self->write_state( $state_file, \%new_state );
156 13 ▶ return $self->finish( $self->UNKNOWN, $problem ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 13 13
157  
158 # held matches are reported as they were, saying when
159 13 my $count = scalar(@matched);
160 13 my @shown = @matched;
161 13 my $held = '';
162 13 ▶ if ( !$count && defined( $new_state{matched_at} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $count and defined $new_state{"matched_at"}) 1 12 13
Condition100%3/3
not $count and defined $new_state{"matched_at"}
!ll&&!rl&&r
661
MC/DC100%2/2
not $count and defined $new_state{"matched_at"}
not $count defined $new_state{"matched_at"}
Truth table100%3/3
not(A) and B
A= not $countB= defined $new_state{"matched_at"}
ABresult
0X0
100
111
163 1 $count = $new_state{matched};
164 1 @shown = @{ $new_state{matched_lines} };
165 1 $held = ', held from ' . ( $now - $new_state{matched_at} ) . ' seconds ago';
166 }
167  
168 $self->perfdata(
169 13 label => 'lines',
170 value => scalar(@matched),
171 warning => $self->opt('warning'),
172 critical => $self->opt('critical'),
173 min => 0
174 );
175 13 my $code = $self->threshold_status( $count, $self->opt('warning'), $self->opt('critical') );
176 13 ▶ my $message = $count . ' matching line' . ( $count == 1 ? '' : 's' ) . ' in ' . $file . $held;
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 6 7 13
177 13 ▶ $message = $message . ', starting at the end of it' if $first_time && !$self->opt('from-start');
Branch: 1 branch
BranchTrueFalseTotal
if $first_time and not $self->opt("from-start") 5 8 13
Condition100%3/3
$first_time and not $self->opt("from-start")
!ll&&!rl&&r
715
MC/DC100%2/2
$first_time and not $self->opt("from-start")
$first_time not $self->opt("from-start")
Truth table100%3/3
A and not(B)
A= $first_timeB= not $self->opt("from-start")
ABresult
0X0
100
111
178 13 my @details = ();
179 13 ▶ if ( $code != $self->OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != $self->OK) 7 6 13
180 7 @details = splice( @shown, 0, $self->opt('lines') );
181 7 ▶ push( @details, 'and ' . ( $count - scalar(@details) ) . ' more' ) if $count > scalar(@details);
Branch: 1 branch
BranchTrueFalseTotal
if $count > scalar @details 1 6 7
182 }
183  
184 13 return $self->finish( $code, $message, @details );
185 } ## end sub check
186  
187 =head2 state_file
188  
189 Returns the state file, --state or one under the state directory
190 named by --file, --regex, and --exclude.
191  
192 =cut
193  
194 15 ▶sub state_file {
Subroutine state_file: called CC=3 SCAR=11.0
Pod documented
195 15 my ($self) = @_;
196  
197 15 ▶ return $self->opt('state') if defined( $self->opt('state') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("state") 6 9 15
198 9 require Digest::SHA;
199 9 my $name = $self->opt('file') =~ s{[^A-Za-z0-9_.-]+}{_}gr;
200  
201 return
202 $Monitoring::Sneck::Checks::base::STATE_DIR . '/log'
203 . $name . '-'
204 . substr(
205 Digest::SHA::sha256_hex(
206 9 ▶ join( "\0", $self->opt('file'), @{ $self->opt('regex') }, "\0", @{ $self->opt('exclude') // [] } )
Condition100%2/2
$self->opt("exclude") // []
l!l
54
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
Aresult
11
00
207 ),
208 0, 12
209 ) . '.json';
210 } ## end sub state_file
211  
212 =head2 read_lines
213  
214 Reads the whole lines of a file from a offset.
215  
216 - file :: The file.
217  
218 - offset :: Where to start.
219  
220 Returns a array ref of the lines, without line endings, and the offset
221 after the last whole line. Or undef and why if it can not be read.
222  
223 =cut
224  
225 13 ▶sub read_lines {
Subroutine read_lines: called CC=5 SCAR=16.3
Pod documented
226 13 my ( $self, $file, $offset ) = @_;
227  
228 13 ▶ open( my $fh, '<:raw', $file ) or return ( undef, $! );
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return undef, $! 13 0 13
229 13 ▶ seek( $fh, $offset, 0 ) or return ( undef, $! );
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $offset, 0 or return undef, $! 13 0 13
230 13 ▶ my $content = do { local $/; <$fh> }
Condition50%1/2
do { local $/; readline $fh } // ""
l!l
130
MC/DC0%0/1
do { local $/; readline $fh } // ""
do { local $/; readline $fh }
Truth table50%1/2
A
A= do { local $/; readline $fh }
Aresult
11
00
231 // '';
232 13 close($fh);
233  
234 13 my $last_newline = rindex( $content, "\n" );
235 13 ▶ return ( [], $offset ) if $last_newline < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $last_newline < 0 7 6 13
236 6 my $whole = substr( $content, 0, $last_newline + 1 );
237  
238 6 return ( [ split( /\r?\n/, $whole ) ], $offset + length($whole) );
239 } ## end sub read_lines
240  
241 3 1;
« blib/lib/Monitoring/Sneck/Checks/accounts.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/thermal_throttle.pm »