« blib/lib/Monitoring/Sneck/Checks/file_age.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/mtime.pm »
| 1 | package Monitoring::Sneck::Checks::load; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 7 | ▶ | use 5.014; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 7 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 NAME | ||||||||||||||
| 9 | |||||||||||||||
| 10 | Monitoring::Sneck::Checks::load - checks the load averages | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||
| 13 | |||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =cut | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 7 | our $VERSION = '0.0.1'; | |||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||
| 21 | |||||||||||||||
| 22 | load|perl:load | ||||||||||||||
| 23 | load_per_cpu|perl:load -r -w 1.5,1.2,1 -c 3,2.5,2 | ||||||||||||||
| 24 | |||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||
| 26 | |||||||||||||||
| 27 | Checks the 1, 5, and 15 minute load averages, the same as check_load. | ||||||||||||||
| 28 | Each is WARNING or CRITICAL if it is over its limit. On Linux they are | ||||||||||||||
| 29 | read from /proc, and on FreeBSD through L<BSD::Sysctl>. Other OSes are | ||||||||||||||
| 30 | UNKNOWN. | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 OPTIONS | ||||||||||||||
| 33 | |||||||||||||||
| 34 | - -w, --warning :: "1,5,15", the limits for each average. | ||||||||||||||
| 35 | Default :: 15,10,5 | ||||||||||||||
| 36 | |||||||||||||||
| 37 | - -c, --critical :: "1,5,15", the limits for each average. | ||||||||||||||
| 38 | Default :: 30,25,20 | ||||||||||||||
| 39 | |||||||||||||||
| 40 | - -r, --per-cpu :: Divide the averages by the number of CPUs first. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =cut | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 40 | ▶ | sub option_spec { return ( 'warning|w=s', 'critical|c=s', 'per-cpu|r' ) } | ||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 40 | ▶ | sub defaults { return { warning => '15,10,5', critical => '30,25,20' } } | ||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | 40 | ▶ | sub validate { | ||||||||||||
|
Subroutine
validate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 49 | 40 | my ( $self, $options ) = @_; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 40 | my @problems; | |||||||||||||
| 52 | 40 | foreach my $name ( 'warning', 'critical' ) { | |||||||||||||
| 53 | push( @problems, '--' . $name . ' must be three numbers, "1,5,15", such as 15,10,5' ) | ||||||||||||||
| 54 | 80 | ▶ | if $options->{$name} !~ /\A[0-9.]+,[0-9.]+,[0-9.]+\z/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 40 | return @problems; | |||||||||||||
| 58 | } ## end sub validate | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head1 METHODS | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 check | ||||||||||||||
| 63 | |||||||||||||||
| 64 | Does the check. | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =cut | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 35 | ▶ | sub check { | ||||||||||||
|
Subroutine
check: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 69 | 35 | my ($self) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 35 | my @averages = $self->averages; | |||||||||||||
| 72 | 30 | ▶ | my $cpus = $self->opt('per-cpu') ? $self->cpus : 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 25 | @averages = map { sprintf( '%.2f', $_ / $cpus ) } @averages; | |||||||||||||
| 74 | 25 | my @warning = split( /,/, $self->opt('warning') ); | |||||||||||||
| 75 | 25 | my @critical = split( /,/, $self->opt('critical') ); | |||||||||||||
| 76 | |||||||||||||||
| 77 | 25 | my $code = $self->OK; | |||||||||||||
| 78 | 25 | foreach my $index ( 0 .. 2 ) { | |||||||||||||
| 79 | 75 | my $average = $averages[$index]; | |||||||||||||
| 80 | 75 | ▶ | if ( $average > $critical[$index] ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 81 | 5 | $code = $self->CRITICAL; | |||||||||||||
| 82 | } elsif ( $average > $warning[$index] ) { | ||||||||||||||
| 83 | 20 | $code = $self->worst( $code, $self->WARNING ); | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | $self->perfdata( | ||||||||||||||
| 86 | 75 | label => 'load' . ( 1, 5, 15 )[$index], | |||||||||||||
| 87 | value => $average, | ||||||||||||||
| 88 | warning => $warning[$index], | ||||||||||||||
| 89 | critical => $critical[$index], | ||||||||||||||
| 90 | min => 0 | ||||||||||||||
| 91 | ); | ||||||||||||||
| 92 | } ## end foreach my $index ( 0 .. 2 ) | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 25 | my $message = 'load average ' . join( ', ', @averages ); | |||||||||||||
| 95 | 25 | ▶ | $message = $message . ' per CPU, of ' . $cpus if $self->opt('per-cpu'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | |||||||||||||||
| 97 | 25 | return $self->finish( $code, $message ); | |||||||||||||
| 98 | } ## end sub check | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head2 averages | ||||||||||||||
| 101 | |||||||||||||||
| 102 | Returns the 1, 5, and 15 minute load averages. Dies if they can not be | ||||||||||||||
| 103 | found. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =cut | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 35 | ▶ | sub averages { | ||||||||||||
|
Subroutine
averages: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 108 | 35 | my ($self) = @_; | |||||||||||||
| 109 | |||||||||||||||
| 110 | 35 | ▶ | if ( $self->os eq 'linux' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 20 | my @averages = split( /\s+/, $self->proc_file('loadavg') ); | |||||||||||||
| 112 | 20 | return @averages[ 0 .. 2 ]; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | 15 | ▶ | if ( $self->os eq 'freebsd' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | 10 | my $averages = $self->sysctl('vm.loadavg'); | |||||||||||||
| 116 | 10 | return @{$averages}[ 0 .. 2 ]; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 5 | die( 'not supported on ' . $self->os . "\n" ); | |||||||||||||
| 120 | } ## end sub averages | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =head2 cpus | ||||||||||||||
| 123 | |||||||||||||||
| 124 | Returns the number of CPUs. Dies if it can not be found. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =cut | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 15 | ▶ | sub cpus { | ||||||||||||
|
Subroutine
cpus: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||
| 129 | 15 | my ($self) = @_; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 15 | ▶ | if ( $self->os eq 'linux' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 5 | my $cpus = () = $self->proc_file('cpuinfo') =~ /^processor\s*:/mg; | |||||||||||||
| 133 | 5 | ▶ | return $cpus || 1; | ||||||||||||
|
Condition50%1/2
$cpus || 1
MC/DC0%0/1
$cpus || 1
$cpus
Truth table50%1/2
A
A= $cpus
| |||||||||||||||
| 134 | } | ||||||||||||||
| 135 | 10 | ▶ | if ( $self->os eq 'freebsd' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 10 | return $self->sysctl('hw.ncpu'); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | die( 'not supported on ' . $self->os . "\n" ); | |||||||||||||
| 140 | } ## end sub cpus | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 7 | 1; | |||||||||||||