| 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
Condition66%2/3
defined $options->{"regex"} and not eval {
do {
qr/$options->{"regex"}/u;
1
}
}
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
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 37 | ▶ | if ( defined( $options->{user} ) && $options->{user} !~ /\A[0-9]+\z/ && !defined( getpwnam( $options->{user} ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $options->{"user"} and not $options->{"user"} =~ /\A[0-9]+\z/u and not defined getpwnam $options->{"user"}
defined $options->{"user"} and not $options->{"user"} =~ /\A[0-9]+\z/u
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $options->{"state"} and not $Monitoring::Sneck::Checks::procs::STATES{uc $options->{"state"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $uid and not $uid =~ /\A[0-9]+\z/u
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 35 | ▶ | my $state = defined( $self->opt('state') ) ? $STATES{ uc( $self->opt('state') ) } : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 35 | ▶ | die("--fd-warning and --fd-critical are only for linux\n") if $fds && $self->os ne 'linux'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fds and $self->os ne "linux"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 26 | my $unreadable = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 26 | foreach my $process ( @{ Proc::ProcessTable->new->table } ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 260 | ▶ | next if $process->pid == $$; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 234 | ▶ | next if defined( $self->opt('command') ) && ( $process->fname // '' ) ne $self->opt('command'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
defined $self->opt("command") and ($process->fname // "") ne $self->opt("command")
$process->fname // ""
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 234 | ▶ | next if defined($regex) && ( $process->cmndline // '' ) !~ /$regex/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
defined $regex and not +($process->cmndline // "") =~ /$regex/u
$process->cmndline // ""
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 25 | ▶ | next if defined($uid) && $process->uid != $uid; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $uid and $process->uid != $uid
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 25 | ▶ | next if defined($state) && !grep { $_ eq lc( $process->state // '' ) } @{$state}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
defined $state and not grep({$_ eq lc($process->state // "");} @{$state;})
$process->state // ""
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;})
Truth table50%1/2
A
A= $process->state
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 23 | $count++; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | 23 | ▶ | my %used = ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$process->rss // 0
$process->pctcpu // 0
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
Truth table50%1/2
A
A= $process->pctcpu
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | rss => sprintf( '%.1f', ( $process->rss // 0 ) / 1048576 ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | cpu => sprintf( '%.1f', $process->pctcpu // 0 ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 23 | ▶ | if ($fds) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 18 | my $fd = $self->fd_use( $process->pid ); | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | 18 | ▶ | if ( !defined($fd) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $most{"fd"} or $fd->{"percent"} > $most{"fd"}{"value"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $most{$kind} or $used{$kind} > $most{$kind}{"value"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 26 | ▶ | push( @filters, 'matching /' . $regex . '/' ) if defined($regex); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 26 | ▶ | push( @filters, 'as ' . $self->opt('user') ) if defined( $self->opt('user') ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 26 | ▶ | push( @filters, 'in state ' . uc( $self->opt('state') ) ) if defined($state); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 26 | ▶ | $message = $message . ' ' . join( ', ', @filters ) if defined( $filters[0] ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $warning || defined $critical and defined $most{$name}
defined $warning or defined $critical
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}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 13 | ▶ | $message = $message . ', ' . $most{$name}{open} . ' of ' . $most{$name}{limit} if $name eq 'fd'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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)
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')
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 9 | my ($limit) = $limits =~ /^Max open files\s+([0-9]+)/m; | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | 9 | ▶ | return undef if !$limit; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 9 | return { open => $open, limit => $limit, percent => sprintf( '%.1f', $open / $limit * 100 ) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | } ## end sub fd_use | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||||||