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

filter: Statementstmt 100% 41 / 41 Branchbran 80.0% 16 / 20 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 95.6% 88 / 92 CC 15 SCAR 23.6
CC
15
Cov
95%
check
15.1
BEGIN
1.0
BEGIN
1.0
CRAP 10.6
« blib/lib/Monitoring/Sneck/Checks/swraid.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/debsecan.pm »
1 package Monitoring::Sneck::Checks::oom;
2  
3 24 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::oom - checks for processes killed for running out of memory
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 24 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 oom|perl:oom
23 oom_held|perl:oom -H 3600 -c 5
24  
25 =head1 DESCRIPTION
26  
27 Counts the processes the Linux OOM killer killed since the last run,
28 from oom_kill in /proc/vmstat, which kernels from 4.13 have. By
29 default any is WARNING.
30  
31 The count is kept in a state file, see
32 L<Monitoring::Sneck::Checks::base/read_state>, so the first run, and
33 the first after a reboot, have nothing to compare to and are OK.
34  
35 A kill is only seen by the run after it, so the next run is OK again,
36 which a poll can easily miss. --hold keeps reporting it for that many
37 seconds after.
38  
39 Only Linux has this. Other OSes are UNKNOWN, as is a kernel without
40 oom_kill.
41  
42 =head1 OPTIONS
43  
44 - -w, --warning :: Range for the kills since the last run.
45 Default :: 0
46  
47 - -c, --critical :: Range for the kills since the last run.
48  
49 - -H, --hold :: Seconds to keep reporting kills for.
50 Default :: 0
51  
52 =cut
53  
54 154 ▶sub option_spec { return ( 'warning|w=s', 'critical|c=s', 'hold|H=i' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
55  
56 154 ▶sub defaults { return { warning => '0', hold => 0 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
57  
58 154 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
59  
60 =head1 METHODS
61  
62 =head2 check
63  
64 Does the check.
65  
66 =cut
67  
68 154 ▶sub check {
Subroutine check: called CC=15 SCAR=27.1
Pod documented
69 154 my ($self) = @_;
70  
71 154 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "linux" 0 154 154
72 154 my ($count) = $self->proc_file('vmstat') =~ /^oom_kill ([0-9]+)$/m;
73 154 ▶ die("this kernel does not count OOM kills\n") if !defined($count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 22 132 154
74  
75 132 my $file = $Monitoring::Sneck::Checks::base::STATE_DIR . '/oom.json';
76 132 my $state = $self->read_state($file);
77 132 ▶ return $self->finish( $self->UNKNOWN, 'can not read the state in ' . $file ) if !defined($state);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 132 132
78  
79 # a lower count than last time is a reboot, so starts over
80 132 my $now = time;
81 132 ▶ my $first = !defined( $state->{count} ) || $count < $state->{count};
Condition100%3/3
!defined($state->{'count'}) || $count < $state->{'count'}
l!l&&r!l&&!r
222288
MC/DC100%2/2
!defined($state->{'count'}) || $count < $state->{'count'}
!defined($state->{'count'}) $count < $state->{'count'}
Truth table100%3/3
not(A) || B
A= !defined($state->{'count'})B= $count < $state->{'count'}
ABresult
1X1
011
000
82 132 ▶ my $kills = $first ? 0 : $count - $state->{count};
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 44 88 132
83 132 my %new = ( count => $count );
84 132 ▶ if ($kills) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kills) { } 22 110 132
elsif (not $first and defined $state->{'killed_at'} and $now - $state->{'killed_at'} < $self->opt('hold')) { } 22 154 176
85 22 %new = ( %new, killed_at => $now, killed => $kills );
86 ▶ } elsif ( !$first && defined( $state->{killed_at} ) && $now - $state->{killed_at} < $self->opt('hold') ) {
Condition100%6/6
not $first and defined $state->{"killed_at"} and $now - $state->{"killed_at"} < $self->opt("hold")
!ll&&!rl&&r
662222
not $first and defined $state->{"killed_at"}
!ll&&!rl&&r
442244
MC/DC100%3/3
not $first and defined $state->{"killed_at"} and $now - $state->{"killed_at"} < $self->opt("hold")
not $first defined $state->{"killed_at"} $now - $state->{"killed_at"} < $self->opt("hold")
Truth table100%4/4
not(A) and B and C
A= not $firstB= defined $state->{"killed_at"}C= $now - $state->{"killed_at"} < $self->opt("hold")
ABCresult
0XX0
10X0
1100
1111
87 22 %new = ( %new, killed_at => $state->{killed_at}, killed => $state->{killed} );
88 }
89 132 my $problem = $self->write_state( $file, \%new );
90 132 ▶ return $self->finish( $self->UNKNOWN, $problem ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 132 132
91  
92 132 my $reported = $kills;
93 132 my $held = '';
94 132 ▶ if ( !$kills && defined( $new{killed_at} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $kills and defined $new{"killed_at"}) 22 110 132
Condition100%3/3
not $kills and defined $new{"killed_at"}
!ll&&!rl&&r
228822
MC/DC100%2/2
not $kills and defined $new{"killed_at"}
not $kills defined $new{"killed_at"}
Truth table100%3/3
not(A) and B
A= not $killsB= defined $new{"killed_at"}
ABresult
0X0
100
111
95 22 $reported = $new{killed};
96 22 $held = ', held from ' . ( $now - $new{killed_at} ) . ' seconds ago';
97 }
98 $self->perfdata(
99 132 label => 'kills',
100 value => $kills,
101 warning => $self->opt('warning'),
102 critical => $self->opt('critical'),
103 min => 0
104 );
105 132 ▶ my $message
Branch: 1 branch
BranchTrueFalseTotal
$reported == 1 ? : 0 132 132
106 = $reported
107 . ' OOM kill'
108 . ( $reported == 1 ? '' : 's' )
109 . ' since the last check'
110 . $held . ', '
111 . $count
112 . ' since boot';
113 132 ▶ $message = 'first check, ' . $count . ' OOM kills since boot' if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 44 88 132
114  
115 132 return $self->finish( $self->threshold_status( $reported, $self->opt('warning'), $self->opt('critical') ),
116 $message );
117 } ## end sub check
118  
119 24 1;
« blib/lib/Monitoring/Sneck/Checks/swraid.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/debsecan.pm »