« blib/lib/Monitoring/Sneck/Checks/pressure.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/service.pm »
| 1 | package Monitoring::Sneck::Checks::pid_file; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use File::Slurp qw(read_file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Monitoring::Sneck::Checks::pid_file - checks the process in a PID file is running | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | nginx_pid|perl:pid_file -f /var/run/nginx.pid -n nginx | ||||||||||||||||||||||||||||
| 24 | unbound_pid|perl:pid_file -f /var/run/local_unbound.pid | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Reads the PID file and checks the process is running, through kill 0, | ||||||||||||||||||||||||||||
| 29 | which works without being able to signal it. A PID file that is not | ||||||||||||||||||||||||||||
| 30 | there, or does not start with a PID, is CRITICAL, as is the process not | ||||||||||||||||||||||||||||
| 31 | running, or with --name, being something else, as for a PID used again | ||||||||||||||||||||||||||||
| 32 | after the one it was for died. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | --name is checked through L<Proc::ProcessTable>, against the process | ||||||||||||||||||||||||||||
| 35 | name, which Linux cuts to 15 characters. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | - -f, --file :: The PID file. Required. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | - -n, --name :: The name the process must have. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 9 | ▶ | sub option_spec { return ( 'file|f=s', 'name|n=s' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 9 | ▶ | sub required { return ('file') } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 check | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Does the check. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | my $file = $self->opt('file'); | |||||||||||||||||||||||||||
| 61 | 9 | my $content = read_file( $file, err_mode => 'quiet' ); | |||||||||||||||||||||||||||
| 62 | 9 | ▶ | return $self->finish( $self->CRITICAL, 'can not read ' . $file . ', ' . $! ) if !defined($content); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 8 | my ($pid) = $content =~ /\A\s*([0-9]+)/; | |||||||||||||||||||||||||||
| 64 | 8 | ▶ | return $self->finish( $self->CRITICAL, $file . ' does not have a PID' ) if !defined($pid) || $pid < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $pid or $pid < 1
MC/DC50%1/2
not defined $pid or $pid < 1
not defined $pid
$pid < 1
Truth table66%2/3
not(A) or B
A= not defined $pidB= $pid < 1
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # EPERM means it is there, just not ours to signal | ||||||||||||||||||||||||||||
| 67 | 7 | ▶ | if ( !kill( 0, $pid ) && !$!{EPERM} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
kill 0, $pid or $!{"EPERM"}
MC/DC50%1/2
kill 0, $pid or $!{"EPERM"}
kill 0, $pid
$!{"EPERM"}
Truth table66%2/3
A or B
A= kill 0, $pidB= $!{"EPERM"}
| |||||||||||||||||||||||||||||
| 68 | 1 | return $self->finish( $self->CRITICAL, $pid . ' from ' . $file . ' is not running' ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | my $is = ''; | |||||||||||||||||||||||||||
| 72 | 6 | ▶ | if ( defined( $self->opt('name') ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 4 | require Proc::ProcessTable; | |||||||||||||||||||||||||||
| 74 | 4 | my ($process) = grep { $_->pid == $pid } @{ Proc::ProcessTable->new->table }; | |||||||||||||||||||||||||||
| 75 | 4 | ▶ | my $name = defined($process) ? $process->fname // '' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$process->fname // ""
MC/DC0%0/1
$process->fname // ""
$process->fname
Truth table50%1/2
A
A= $process->fname
| |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | if ( $name ne $self->opt('name') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | return $self->finish( $self->CRITICAL, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | $pid | ||||||||||||||||||||||||||||
| 79 | . ' from ' | ||||||||||||||||||||||||||||
| 80 | . $file . ' is ' | ||||||||||||||||||||||||||||
| 81 | . ( $name eq '' ? 'not known' : $name ) | ||||||||||||||||||||||||||||
| 82 | . ', not ' | ||||||||||||||||||||||||||||
| 83 | . $self->opt('name') ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 2 | $is = ' ' . $name; | |||||||||||||||||||||||||||
| 86 | } ## end if ( defined( $self->opt('name') ) ) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | return $self->finish( $self->OK, $pid . $is . ' from ' . $file . ' is running' ); | |||||||||||||||||||||||||||
| 89 | } ## end sub check | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | 1; | |||||||||||||||||||||||||||