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

filter: Statementstmt 98.8% 88 / 89 Branchbran 78.5% 44 / 56 Conditioncond 63.1% 36 / 57 MC/DCmcdc 31.2% 10 / 32 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 77.6% 194 / 250 CC 53 SCAR 36.5
CC
53
Cov
83%
check
53.3
validate
9.1
fd_use
4.0
CRAP 38.5
« blib/lib/Monitoring/Sneck/Checks/ping.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/smart.pm »
1 package Monitoring::Sneck::Checks::procs;
2  
3 12 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::procs - checks how many processes are running
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 12 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 sshd_running|perl:procs -C sshd
23 php_fpm|perl:procs -C php-fpm -w 2:50 -c 1:100
24 worker|perl:procs -r 'bin/worker --queue mail' -u www -c 1:1
25 zombies|perl:procs -s Z -w 0 -c 5
26 java_rss|perl:procs -C java --rss-warning 4096 --rss-critical 6144
27 runaway|perl:procs -u www --cpu-warning 80 --cpu-critical 95 -c 0:
28  
29 =head1 DESCRIPTION
30  
31 Counts the processes matching all of the filters given, through
32 L<Proc::ProcessTable>, and checks the count against the ranges. By
33 default, none matching is CRITICAL. sneck itself is never counted.
34  
35 --command is the process name. Linux cuts it to 15 characters, so
36 match longer names with --regex.
37  
38 --rss-warning and the like check each matching process, the one using
39 the most being said. CPU is the percent over its whole life, as ps
40 shows it, not right now, so a process that only just started spinning
41 takes a while to show it.
42  
43 =head1 OPTIONS
44  
45 - -C, --command :: The process name, exactly.
46  
47 - -r, --regex :: A regex matched against the whole command line, so
48 matching anywhere in it is enough.
49  
50 - -u, --user :: The user it runs as, by name or UID.
51  
52 - -s, --state :: Its state, one of R, running, S, sleeping, D, waiting
53 on IO, Z, zombie, T, stopped, or I, idle.
54  
55 - -w, --warning :: Range for the count.
56  
57 - -c, --critical :: Range for the count.
58 Default :: 1:
59  
60 - --rss-warning :: Range of MiB of memory for each process.
61  
62 - --rss-critical :: Range of MiB of memory for each process.
63  
64 - --cpu-warning :: Range of percent CPU for each process.
65  
66 - --cpu-critical :: Range of percent CPU for each process.
67  
68 - --fd-warning :: Range of percent of its open file limit each process
69 is using. Linux only, as it reads /proc. UNKNOWN elsewhere.
70  
71 - --fd-critical :: Range of percent of its open file limit each process
72 is using. Linux only, the same as --fd-warning.
73  
74 =cut
75  
76 37 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
77 return (
78 37 'command|C=s', 'regex|r=s', 'user|u=s', 'state|s=s', 'warning|w=s', 'critical|c=s',
79 'rss-warning=s', 'rss-critical=s', 'cpu-warning=s', 'cpu-critical=s', 'fd-warning=s', 'fd-critical=s'
80 );
81 }
82  
83 37 ▶sub defaults { return { critical => '1:' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
84  
85 37 ▶sub ranges {
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
86 return (
87 37 'warning', 'critical', 'rss-warning', 'rss-critical',
88 'cpu-warning', 'cpu-critical', 'fd-warning', 'fd-critical'
89 );
90 }
91  
92 # How Proc::ProcessTable names states on Linux and FreeBSD, by the letter ps uses.
93 12 our %STATES = (
94 R => [ 'run', 'onprocessor' ],
95 S => ['sleep'],
96 D => [ 'uwait', 'wait', 'lock' ],
97 Z => [ 'defunct', 'zombie' ],
98 T => ['stop'],
99 I => ['idle'],
100 );
101  
102 37 ▶sub validate {
Subroutine validate: called CC=8 SCAR=22.1
Pod documented
103 37 my ( $self, $options ) = @_;
104  
105 37 my @problems;
106 push( @problems, '--regex is not a valid regex' )
107 37 ▶ if defined( $options->{regex} ) && !eval { qr/$options->{regex}/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"regex"} and not eval { do { qr/$options->{"regex"}/u; 1 } } 0 37 37
Condition66%2/3
defined $options->{"regex"} and not eval { do { qr/$options->{"regex"}/u; 1 } }
!ll&&!rl&&r
11260
MC/DC0%0/2
defined $options->{"regex"} and not eval { do { qr/$options->{"regex"}/u; 1 } }
defined $options->{"regex"} not eval { do { qr/$options->{"regex"}/u; 1 } }
Truth table66%2/3
A and not(B)
A= defined $options->{"regex"}B= not eval { do { qr/$options->{"regex"}/u; 1 } }
ABresult
0X0
100
111
108 37 ▶ if ( defined( $options->{user} ) && $options->{user} !~ /\A[0-9]+\z/ && !defined( getpwnam( $options->{user} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"user"} and not $options->{"user"} =~ /\A[0-9]+\z/u and not defined getpwnam $options->{"user"}) 0 37 37
Condition50%3/6
defined $options->{"user"} and not $options->{"user"} =~ /\A[0-9]+\z/u and not defined getpwnam $options->{"user"}
!ll&&!rl&&r
3700
defined $options->{"user"} and not $options->{"user"} =~ /\A[0-9]+\z/u
!ll&&!rl&&r
3610
MC/DC0%0/3
defined $options->{"user"} and not $options->{"user"} =~ /\A[0-9]+\z/u and not defined getpwnam $options->{"user"}
defined $options->{"user"} not $options->{"user"} =~ /\A[0-9]+\z/u not defined getpwnam $options->{"user"}
Truth table50%2/4
A and not(B) and not(C)
A= defined $options->{"user"}B= not $options->{"user"} =~ /\A[0-9]+\z/uC= not defined getpwnam $options->{"user"}
ABCresult
0XX0
10X0
1100
1111
109 0 push( @problems, '--user "' . $options->{user} . '" is not a user' );
110 }
111 push( @problems, '--state must be one of ' . join( ', ', sort( keys(%STATES) ) ) )
112 37 ▶ if defined( $options->{state} ) && !$STATES{ uc( $options->{state} ) };
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"state"} and not $Monitoring::Sneck::Checks::procs::STATES{uc $options->{"state"}} 1 36 37
Condition100%3/3
defined $options->{"state"} and not $Monitoring::Sneck::Checks::procs::STATES{uc $options->{"state"}}
!ll&&!rl&&r
3421
MC/DC100%2/2
defined $options->{"state"} and not $Monitoring::Sneck::Checks::procs::STATES{uc $options->{"state"}}
defined $options->{"state"} not $Monitoring::Sneck::Checks::procs::STATES{uc $options->{"state"}}
Truth table100%3/3
A and not(B)
A= defined $options->{"state"}B= not $Monitoring::Sneck::Checks::procs::STATES{uc $options->{"state"}}
ABresult
0X0
100
111
113  
114 37 return @problems;
115 } ## end sub validate
116  
117 =head1 METHODS
118  
119 =head2 check
120  
121 Does the check.
122  
123 =cut
124  
125 35 ▶sub check {
Subroutine check: called CC=43 SCAR=39.8
Pod documented
126 35 my ($self) = @_;
127  
128 35 require Proc::ProcessTable;
129 35 my $regex = $self->opt('regex');
130 35 my $uid = $self->opt('user');
131 35 ▶ $uid = getpwnam($uid) if defined($uid) && $uid !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uid and not $uid =~ /\A[0-9]+\z/u 0 35 35
Condition66%2/3
defined $uid and not $uid =~ /\A[0-9]+\z/u
!ll&&!rl&&r
3410
MC/DC0%0/2
defined $uid and not $uid =~ /\A[0-9]+\z/u
defined $uid not $uid =~ /\A[0-9]+\z/u
Truth table66%2/3
A and not(B)
A= defined $uidB= not $uid =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
132 35 ▶ my $state = defined( $self->opt('state') ) ? $STATES{ uc( $self->opt('state') ) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('state') ? : 2 33 35
133  
134 35 my $count = 0;
135 35 my %most = ( rss => undef, cpu => undef, fd => undef );
136 35 ▶ my $fds = defined( $self->opt('fd-warning') ) || defined( $self->opt('fd-critical') );
Condition66%2/3
defined $self->opt('fd-warning') || defined $self->opt('fd-critical')
l!l&&r!l&&!r
2708
MC/DC50%1/2
defined $self->opt('fd-warning') || defined $self->opt('fd-critical')
defined $self->opt('fd-warning') defined $self->opt('fd-critical')
Truth table66%2/3
A || B
A= defined $self->opt('fd-warning')B= defined $self->opt('fd-critical')
ABresult
1X1
011
000
137 35 ▶ die("--fd-warning and --fd-critical are only for linux\n") if $fds && $self->os ne 'linux';
Branch: 1 branch
BranchTrueFalseTotal
if $fds and $self->os ne "linux" 9 26 35
Condition100%3/3
$fds and $self->os ne "linux"
!ll&&!rl&&r
8189
MC/DC100%2/2
$fds and $self->os ne "linux"
$fds $self->os ne "linux"
Truth table100%3/3
A and B
A= $fdsB= $self->os ne "linux"
ABresult
0X0
100
111
138 26 my $unreadable = 0;
139 26 foreach my $process ( @{ Proc::ProcessTable->new->table } ) {
140 260 ▶ next if $process->pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $process->pid == $$ 26 234 260
141 234 ▶ next if defined( $self->opt('command') ) && ( $process->fname // '' ) ne $self->opt('command');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("command") and ($process->fname // "") ne $self->opt("command") 0 234 234
Condition20%1/5
defined $self->opt("command") and ($process->fname // "") ne $self->opt("command")
!ll&&!rl&&r
23400
$process->fname // ""
l!l
00
MC/DC0%0/2
defined $self->opt("command") and ($process->fname // "") ne $self->opt("command")
defined $self->opt("command") $process->fname
Truth table33%1/3
A and B
A= defined $self->opt("command")B= $process->fname
ABresult
0X0
100
111
142 234 ▶ next if defined($regex) && ( $process->cmndline // '' ) !~ /$regex/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $regex and not +($process->cmndline // "") =~ /$regex/u 209 25 234
Condition60%3/5
defined $regex and not +($process->cmndline // "") =~ /$regex/u
!ll&&!rl&&r
025209
$process->cmndline // ""
l!l
2340
MC/DC0%0/2
defined $regex and not +($process->cmndline // "") =~ /$regex/u
defined $regex $process->cmndline
Truth table33%1/3
A and not(B)
A= defined $regexB= $process->cmndline
ABresult
0X0
110
101
143 25 ▶ next if defined($uid) && $process->uid != $uid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uid and $process->uid != $uid 0 25 25
Condition66%2/3
defined $uid and $process->uid != $uid
!ll&&!rl&&r
2410
MC/DC0%0/2
defined $uid and $process->uid != $uid
defined $uid $process->uid != $uid
Truth table66%2/3
A and B
A= defined $uidB= $process->uid != $uid
ABresult
0X0
100
111
144 25 ▶ next if defined($state) && !grep { $_ eq lc( $process->state // '' ) } @{$state};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state and not grep({$_ eq lc($process->state // "");} @{$state;}) 2 23 25
Condition60%3/5
defined $state and not grep({$_ eq lc($process->state // "");} @{$state;})
!ll&&!rl&&r
2302
$process->state // ""
l!l
30
MC/DC50%1/2
defined $state and not grep({$_ eq lc($process->state // "");} @{$state;})
defined $state not grep({$_ eq lc($process->state // "");} @{$state;})
MC/DC0%0/1
$process->state // ""
$process->state
Truth table66%2/3
A and not(B)
A= defined $stateB= not grep({$_ eq lc($process->state // "");} @{$state;})
ABresult
0X0
100
111
Truth table50%1/2
A
A= $process->state
Aresult
11
00
145 23 $count++;
146 23 ▶ my %used = (
Condition50%2/4
$process->rss // 0
l!l
230
$process->pctcpu // 0
l!l
230
MC/DC0%0/1
$process->rss // 0
$process->rss
MC/DC0%0/1
$process->pctcpu // 0
$process->pctcpu
Truth table50%1/2
A
A= $process->rss
Aresult
11
00
Truth table50%1/2
A
A= $process->pctcpu
Aresult
11
00
147 rss => sprintf( '%.1f', ( $process->rss // 0 ) / 1048576 ),
148 cpu => sprintf( '%.1f', $process->pctcpu // 0 )
149 );
150 23 ▶ if ($fds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fds) 18 5 23
151 18 my $fd = $self->fd_use( $process->pid );
152 18 ▶ if ( !defined($fd) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fd) { } 9 9 18
153 9 $unreadable++;
154 } else {
155 9 $used{fd} = $fd->{percent};
156 9 $most{fd} = { %{$fd}, value => $fd->{percent}, pid => $process->pid }
157 9 ▶ if !defined( $most{fd} ) || $fd->{percent} > $most{fd}{value};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $most{"fd"} or $fd->{"percent"} > $most{"fd"}{"value"} 9 0 9
Condition33%1/3
not defined $most{"fd"} or $fd->{"percent"} > $most{"fd"}{"value"}
l!l&&r!l&&!r
900
MC/DC0%0/2
not defined $most{"fd"} or $fd->{"percent"} > $most{"fd"}{"value"}
not defined $most{"fd"} $fd->{"percent"} > $most{"fd"}{"value"}
Truth table33%1/3
not(A) or B
A= not defined $most{"fd"}B= $fd->{"percent"} > $most{"fd"}{"value"}
ABresult
1X1
011
000
158 }
159 } ## end if ($fds)
160 23 foreach my $kind ( 'rss', 'cpu' ) {
161 $most{$kind} = { value => $used{$kind}, pid => $process->pid }
162 46 ▶ if !defined( $most{$kind} ) || $used{$kind} > $most{$kind}{value};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $most{$kind} or $used{$kind} > $most{$kind}{"value"} 46 0 46
Condition33%1/3
not defined $most{$kind} or $used{$kind} > $most{$kind}{"value"}
l!l&&r!l&&!r
4600
MC/DC0%0/2
not defined $most{$kind} or $used{$kind} > $most{$kind}{"value"}
not defined $most{$kind} $used{$kind} > $most{$kind}{"value"}
Truth table33%1/3
not(A) or B
A= not defined $most{$kind}B= $used{$kind} > $most{$kind}{"value"}
ABresult
1X1
011
000
163 }
164 } ## end foreach my $process ( @{ Proc::ProcessTable->new...})
165  
166 26 my @filters;
167 26 ▶ push( @filters, 'named ' . $self->opt('command') ) if defined( $self->opt('command') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("command") 0 26 26
168 26 ▶ push( @filters, 'matching /' . $regex . '/' ) if defined($regex);
Branch: 1 branch
BranchTrueFalseTotal
if defined $regex 26 0 26
169 26 ▶ push( @filters, 'as ' . $self->opt('user') ) if defined( $self->opt('user') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("user") 1 25 26
170 26 ▶ push( @filters, 'in state ' . uc( $self->opt('state') ) ) if defined($state);
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 2 24 26
171 26 $self->perfdata(
172 label => 'procs',
173 value => $count,
174 warning => $self->opt('warning'),
175 critical => $self->opt('critical'),
176 min => 0
177 );
178 26 ▶ my $message = $count . ' process' . ( $count == 1 ? '' : 'es' );
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 23 3 26
179 26 ▶ $message = $message . ' ' . join( ', ', @filters ) if defined( $filters[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $filters[0] 26 0 26
180  
181 # the process using the most is what is checked, as each must be within the ranges
182 26 my @codes = ( $self->threshold_status( $count, $self->opt('warning'), $self->opt('critical') ) );
183 26 foreach my $kind (
184 [ 'rss', 'MiB', 'most memory' ],
185 [ 'cpu', '%', 'most CPU' ],
186 [ 'fd', '%', 'most of its open file limit' ]
187 )
188 {
189 78 my ( $name, $unit, $said ) = @{$kind};
190 78 my ( $warning, $critical ) = ( $self->opt( $name . '-warning' ), $self->opt( $name . '-critical' ) );
191 78 ▶ next if ( !defined($warning) && !defined($critical) ) || !defined( $most{$name} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $warning || defined $critical and defined $most{$name} 65 13 78
Condition100%6/6
defined $warning || defined $critical and defined $most{$name}
!ll&&!rl&&r
56913
defined $warning or defined $critical
l!l&&r!l&&!r
20256
MC/DC100%3/3
defined $warning || defined $critical and defined $most{$name}
defined $warning defined $critical defined $most{$name}
Truth table80%4/5
A or B and C
A= defined $warningB= defined $criticalC= defined $most{$name}
ABCresult
00X0
1X00
0100
1X11
0111
192 13 my $code = $self->threshold_status( $most{$name}{value}, $warning, $critical );
193 13 push( @codes, $code );
194 $self->perfdata(
195 label => $name . '_max',
196 value => $most{$name}{value},
197 13 ▶ uom => $unit eq '%' ? '%' : '',
Branch: 1 branch
BranchTrueFalseTotal
$unit eq '%' ? : 11 2 13
198 warning => $warning,
199 critical => $critical,
200 min => 0
201 );
202 $message
203 = $message . ', '
204 . $said . ' '
205 . $most{$name}{value}
206 . ( $unit eq '%' ? '%' : ' ' . $unit ) . ' by '
207 13 ▶ . $most{$name}{pid};
Branch: 1 branch
BranchTrueFalseTotal
$unit eq '%' ? : 11 2 13
208 13 ▶ $message = $message . ', ' . $most{$name}{open} . ' of ' . $most{$name}{limit} if $name eq 'fd';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "fd" 9 4 13
209 } ## end foreach my $kind ( [ 'rss', 'MiB', 'most memory'...])
210 26 ▶ $message = $message . ', ' . $unreadable . ' could not be read for open files' if $unreadable;
Branch: 1 branch
BranchTrueFalseTotal
if $unreadable 9 17 26
211  
212 26 return $self->finish( $self->worst(@codes), $message );
213 } ## end sub check
214  
215 =head2 fd_use
216  
217 Works out how much of its open file limit a process is using, from
218 /proc/PID/fd and the soft limit in /proc/PID/limits. Seeing other
219 users' processes needs root.
220  
221 Returns a hash ref of open, limit, and percent, or undef if it can not
222 be read or has no limit.
223  
224 =cut
225  
226 18 ▶sub fd_use {
Subroutine fd_use: called CC=4 SCAR=13.9
Pod documented
227 18 my ( $self, $pid ) = @_;
228  
229 18 my $dir = $Monitoring::Sneck::Checks::base::PROC . '/' . $pid;
230 18 ▶ opendir( my $fd_dir, $dir . '/fd' ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $fd_dir, $dir . '/fd' or return undef 18 0 18
231 18 my $open = grep { !/\A\.\.?\z/ } readdir($fd_dir);
232 18 closedir($fd_dir);
233 18 ▶ my $limits = eval { $self->proc_file( $pid . '/limits' ) } // return undef;
Condition100%2/2
eval { do { $self->proc_file($pid . '/limits') } } // (return undef)
l!l
99
MC/DC100%1/1
eval { do { $self->proc_file($pid . '/limits') } } // (return undef)
eval { do { $self->proc_file($pid . '/limits') } }
Truth table100%2/2
A
A= eval { do { $self->proc_file($pid . '/limits') } }
Aresult
11
00
234 9 my ($limit) = $limits =~ /^Max open files\s+([0-9]+)/m;
235 9 ▶ return undef if !$limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 0 9 9
236  
237 9 return { open => $open, limit => $limit, percent => sprintf( '%.1f', $open / $limit * 100 ) };
238 } ## end sub fd_use
239  
240 12 1;
« blib/lib/Monitoring/Sneck/Checks/ping.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/smart.pm »