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

filter: Statementstmt 97.2% 35 / 36 Branchbran 83.3% 15 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 84.8% 67 / 79 CC 12 SCAR 18.5
CC
12
Cov
87%
seconds_up
11.7
_human
3.0
BEGIN
1.0
CRAP 6.4
« blib/lib/Monitoring/Sneck/Checks/conntrack.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/sysctl.pm »
1 package Monitoring::Sneck::Checks::uptime;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::uptime - checks how long the host has been up
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 3 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 rebooted|perl:uptime
23 rebooted_or_stale|perl:uptime -w 900:7776000
24  
25 =head1 DESCRIPTION
26  
27 Checks the seconds the host has been up against the ranges, so by
28 default being up under 15 minutes, as after a reboot no one planned,
29 is WARNING. A upper end, such as 7776000 for 90 days, catches hosts
30 that have gone too long without one.
31  
32 On Linux it is read from /proc/uptime, and on FreeBSD from kern.boottime
33 through L<BSD::Sysctl>. Other OSes are UNKNOWN.
34  
35 =head1 OPTIONS
36  
37 - -w, --warning :: Range of seconds up.
38 Default :: 900:
39  
40 - -c, --critical :: Range of seconds up.
41  
42 =cut
43  
44 7 ▶sub option_spec { return ( 'warning|w=s', 'critical|c=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
45  
46 7 ▶sub defaults { return { warning => '900:' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
47  
48 7 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
49  
50 =head1 METHODS
51  
52 =head2 check
53  
54 Does the check.
55  
56 =cut
57  
58 7 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
59 7 my ($self) = @_;
60  
61 7 my $seconds = int( $self->seconds_up );
62 7 $self->perfdata(
63 label => 'uptime',
64 value => $seconds,
65 uom => 's',
66 warning => $self->opt('warning'),
67 critical => $self->opt('critical'),
68 min => 0
69 );
70  
71 7 return $self->finish( $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') ),
72 'up ' . _human($seconds) );
73 } ## end sub check
74  
75 =head2 seconds_up
76  
77 Returns the seconds the host has been up. Dies if it can not be found.
78  
79 =cut
80  
81 7 ▶sub seconds_up {
Subroutine seconds_up: called CC=10 SCAR=24.6
Pod documented
82 7 my ($self) = @_;
83  
84 7 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 3 4 7
85 3 my ($seconds) = $self->proc_file('uptime') =~ /\A([0-9.]+)/;
86 3 ▶ die("can not make sense of /proc/uptime\n") if !defined($seconds);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seconds 0 3 3
87 3 return $seconds;
88 }
89 4 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 4 0 4
90  
91 # a timeval, which may come as a hash or array ref, or as text such as "{ sec = 1700000000, usec = 0 } ..."
92 4 my $boot = $self->sysctl('kern.boottime');
93 my $sec
94 = ref($boot) eq 'HASH' ? $boot->{sec} // $boot->{tv_sec}
95 4 ▶ : ref($boot) eq 'ARRAY' ? $boot->[0]
Branch: 3 branches
BranchTrueFalseTotal
ref $boot eq 'HASH' ? : 1 3 4
ref $boot eq 'ARRAY' ? : 1 2 3
$boot =~ /sec\s*=\s*([0-9]+)/u ? : 1 1 2
Condition33%1/3
$boot->{"sec"} // $boot->{"tv_sec"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$boot->{"sec"} // $boot->{"tv_sec"}
$boot->{"sec"} $boot->{"tv_sec"}
Truth table33%1/3
A // B
A= $boot->{"sec"}B= $boot->{"tv_sec"}
ABresult
1X1
011
000
96 : $boot =~ /sec\s*=\s*([0-9]+)/ ? $1
97 : $boot;
98 4 ▶ die("can not make sense of kern.boottime\n") if !defined($sec) || $sec !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sec and $sec =~ /\A[0-9]+\z/u 0 4 4
Condition33%1/3
defined $sec and $sec =~ /\A[0-9]+\z/u
!ll&&!rl&&r
004
MC/DC0%0/2
defined $sec and $sec =~ /\A[0-9]+\z/u
defined $sec $sec =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $secB= $sec =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
99 4 return time - $sec;
100 } ## end if ( $self->os eq 'freebsd' )
101  
102 0 die( 'not supported on ' . $self->os . "\n" );
103 } ## end sub seconds_up
104  
105 # Turns seconds into days, hours, and minutes for people.
106 #
107 # Args...
108 #
109 # - seconds :: The seconds.
110 #
111 # Returns the text, such as '12 days, 3:04'.
112 #
113 # Example...
114 #
115 # _human(1049040); # '12 days, 3:24'
116 7 ▶sub _human {
Subroutine _human: called CC=3 SCAR=11.0
117 7 my ($seconds) = @_;
118  
119 7 my $days = int( $seconds / 86400 );
120 7 my $hours = int( ( $seconds % 86400 ) / 3600 );
121 7 my $minutes = int( ( $seconds % 3600 ) / 60 );
122  
123 7 ▶ return ( $days ? $days . ' day' . ( $days == 1 ? '' : 's' ) . ', ' : '' ) . sprintf( '%d:%02d', $hours, $minutes );
Branch: 2 branches
BranchTrueFalseTotal
$days ? : 6 1 7
$days == 1 ? : 4 2 6
124 }
125  
126 3 1;
« blib/lib/Monitoring/Sneck/Checks/conntrack.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/sysctl.pm »