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

filter: Statementstmt 97.4% 77 / 79 Branchbran 80.7% 21 / 26 Conditioncond 68.7% 22 / 32 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 100% 14 / 14 Podpod 100% 9 / 9 totaltotal 84.6% 149 / 176 CC 27 SCAR 18.1
CC
27
Cov
88%
rate_status
11.4
swapinfo
7.0
swap_counts
5.6
CRAP 6.1
« blib/lib/Monitoring/Sneck/Checks/sysctl.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/selinux.pm »
1 package Monitoring::Sneck::Checks::swap;
2  
3 17 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::swap - checks how much swap is used
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 17 our $VERSION = '0.0.1';
20  
21 # Where swapinfo is on FreeBSD. Set by tests.
22 17 our $SWAPINFO = '/usr/sbin/swapinfo';
23  
24 # The codes --no-swap takes.
25 17 our %NO_SWAP_CODES = ( ok => 0, warning => 1, critical => 2, unknown => 3 );
26  
27 =head1 SYNOPSIS
28  
29 swap|perl:swap
30 swap_tight|perl:swap -w 20 -c 50 -n critical
31 swapping|perl:swap -W 100 -C 1000
32  
33 =head1 DESCRIPTION
34  
35 Checks the percent of swap used, across every swap device. On Linux it
36 is read from /proc/meminfo. On FreeBSD it is from running swapinfo, as
37 the sysctl it keeps it in is not one L<BSD::Sysctl> reads. Other OSes
38 are UNKNOWN.
39  
40 Having no swap is OK, unless --no-swap says otherwise, as plenty of
41 hosts run without it on purpose.
42  
43 Swap being used but sitting there is harmless, while swapping all the
44 time is not. --rate-warning and --rate-critical check the pages
45 swapped in and out per second since the last run, from pswpin and
46 pswpout in /proc/vmstat on Linux, and v_swappgsin and v_swappgsout
47 through L<BSD::Sysctl> on FreeBSD. The counts are kept in a state file,
48 see L<Monitoring::Sneck::Checks::base/read_state>, so the first run, or
49 one after a reboot, has no rate. They are only kept and checked if one
50 of those is given.
51  
52 =head1 OPTIONS
53  
54 - -w, --warning :: Range of percent used.
55 Default :: 80
56  
57 - -c, --critical :: Range of percent used.
58 Default :: 90
59  
60 - -n, --no-swap :: What having no swap is, ok, warning, critical, or
61 unknown.
62 Default :: ok
63  
64 - -W, --rate-warning :: Range of pages swapped in and out per second.
65  
66 - -C, --rate-critical :: Range of pages swapped in and out per second.
67  
68 =cut
69  
70 79 ▶sub option_spec { return ( 'warning|w=s', 'critical|c=s', 'no-swap|n=s', 'rate-warning|W=s', 'rate-critical|C=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
71  
72 79 ▶sub defaults { return { warning => '80', critical => '90', 'no-swap' => 'ok' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
73  
74 79 ▶sub ranges { return ( 'warning', 'critical', 'rate-warning', 'rate-critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
75  
76 79 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
77 79 my ( $self, $options ) = @_;
78  
79 return ( '--no-swap must be ' . join( ', ', sort( keys(%NO_SWAP_CODES) ) ) )
80 79 ▶ if !defined( $NO_SWAP_CODES{ lc( $options->{'no-swap'} ) } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Monitoring::Sneck::Checks::swap::NO_SWAP_CODES{lc $options->{"no-swap"}} 5 74 79
81  
82 74 return ();
83 }
84  
85 =head1 METHODS
86  
87 =head2 check
88  
89 Does the check.
90  
91 =cut
92  
93 74 ▶sub check {
Subroutine check: called CC=2 SCAR=6.9
Pod documented
94 74 my ($self) = @_;
95  
96 74 my ( $total, $used ) = $self->swap;
97 66 my ( $rate_code, $rate_text ) = $self->rate_status;
98 66 ▶ if ( !$total ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($total) 13 53 66
99 13 return $self->finish( $self->worst( $NO_SWAP_CODES{ lc( $self->opt('no-swap') ) }, $rate_code ),
100 'no swap' . $rate_text );
101 }
102  
103 53 my $percent = sprintf( '%.1f', $used / $total * 100 );
104 53 $self->perfdata(
105 label => 'used',
106 value => $percent,
107 uom => '%',
108 warning => $self->opt('warning'),
109 critical => $self->opt('critical'),
110 min => 0,
111 max => 100
112 );
113 53 $self->perfdata( label => 'used_bytes', value => $used, uom => 'B', min => 0, max => $total );
114 53 my $code = $self->worst( $self->threshold_status( $percent, $self->opt('warning'), $self->opt('critical') ),
115 $rate_code );
116  
117 53 return $self->finish( $code,
118 $percent
119 . '% used, '
120 . sprintf( '%.0f', $used / 1048576 ) . ' of '
121 . sprintf( '%.0f', $total / 1048576 ) . ' MiB'
122 . $rate_text );
123 } ## end sub check
124  
125 =head2 rate_status
126  
127 Works out the pages swapped per second since the last run, if
128 --rate-warning or --rate-critical is given, saving the counts for the
129 next, and adds its perfdata.
130  
131 Returns the code and text to add to the message, OK and '' if not
132 checked or there is no rate yet. Dies if the state can not be kept.
133  
134 =cut
135  
136 66 ▶sub rate_status {
Subroutine rate_status: called CC=11 SCAR=24.3
Pod documented
137 66 my ($self) = @_;
138  
139 66 my ( $warning, $critical ) = ( $self->opt('rate-warning'), $self->opt('rate-critical') );
140 66 ▶ return ( $self->OK, '' ) if !defined($warning) && !defined($critical);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $warning or defined $critical 37 29 66
Condition100%3/3
defined $warning or defined $critical
l!l&&r!l&&!r
20937
MC/DC100%2/2
defined $warning or defined $critical
defined $warning defined $critical
Truth table100%3/3
A or B
A= defined $warningB= defined $critical
ABresult
1X1
011
000
141  
142 29 my ( $in, $out ) = $self->swap_counts;
143 29 my $file = $Monitoring::Sneck::Checks::base::STATE_DIR . '/swap.json';
144 29 ▶ my $state = $self->read_state($file) // {};
Condition50%1/2
$self->read_state($file) // {}
l!l
290
MC/DC0%0/1
$self->read_state($file) // {}
$self->read_state($file)
Truth table50%1/2
A
A= $self->read_state($file)
Aresult
11
00
145 29 my $now = Time::HiRes::time;
146 29 my $problem = $self->write_state( $file, { time => $now, in => $in, out => $out } );
147 29 ▶ die( $problem . "\n" ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 29 29
148  
149 # a lower count than last time is a reboot, so starts over
150 return ( $self->OK, ', no swap rate yet' )
151 if !defined( $state->{time} )
152 || $now <= $state->{time}
153 || $in < ( $state->{in} // 0 )
154 29 ▶ || $out < ( $state->{out} // 0 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $state->{"time"} or $now <= $state->{"time"} or $in < ($state->{"in"} // 0) or $out < ($state->{"out"} // 0) 19 10 29
Condition69%9/13
not defined $state->{"time"} or $now <= $state->{"time"} or $in < ($state->{"in"} // 0) or $out < ($state->{"out"} // 0)
l!l&&r!l&&!r
19010
not defined $state->{"time"} or $now <= $state->{"time"} or $in < ($state->{"in"} // 0)
l!l&&r!l&&!r
10910
not defined $state->{"time"} or $now <= $state->{"time"}
l!l&&r!l&&!r
10019
$state->{"in"} // 0
l!l
190
$state->{"out"} // 0
l!l
100
MC/DC50%2/4
not defined $state->{"time"} or $now <= $state->{"time"} or $in < ($state->{"in"} // 0) or $out < ($state->{"out"} // 0)
not defined $state->{"time"} $now <= $state->{"time"} $in < ($state->{"in"} // 0) $out < ($state->{"out"} // 0)
MC/DC0%0/1
$state->{"in"} // 0
$state->{"in"}
MC/DC0%0/1
$state->{"out"} // 0
$state->{"out"}
Truth table60%3/5
not(A) or B or C or D
A= not defined $state->{"time"}B= $now <= $state->{"time"}C= $in < ($state->{"in"} // 0)D= $out < ($state->{"out"} // 0)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table50%1/2
A
A= $state->{"in"}
Aresult
11
00
Truth table50%1/2
A
A= $state->{"out"}
Aresult
11
00
155 10 my $seconds = $now - $state->{time};
156 10 my $in_rate = sprintf( '%.1f', ( $in - $state->{in} ) / $seconds );
157 10 my $out_rate = sprintf( '%.1f', ( $out - $state->{out} ) / $seconds );
158 10 my $total = sprintf( '%.1f', $in_rate + $out_rate );
159 10 $self->perfdata( label => 'swap_in', value => $in_rate, min => 0 );
160 10 $self->perfdata( label => 'swap_out', value => $out_rate, min => 0 );
161 10 $self->perfdata( label => 'swapping', value => $total, warning => $warning, critical => $critical, min => 0 );
162  
163 return (
164 10 $self->threshold_status( $total, $warning, $critical ),
165 ', swapping ' . $in_rate . ' pages/s in and ' . $out_rate . ' out'
166 );
167 } ## end sub rate_status
168  
169 =head2 swap_counts
170  
171 Returns the pages swapped in and out since boot. Dies if they can not
172 be found.
173  
174 =cut
175  
176 29 ▶sub swap_counts {
Subroutine swap_counts: called CC=5 SCAR=17.3
Pod documented
177 29 my ($self) = @_;
178  
179 29 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 20 9 29
180 20 my %vmstat = $self->proc_file('vmstat') =~ /^(\w+) ([0-9]+)$/mg;
181 20 ▶ die("no pswpin and pswpout in /proc/vmstat\n") if !defined( $vmstat{pswpin} ) || !defined( $vmstat{pswpout} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vmstat{"pswpin"} and defined $vmstat{"pswpout"} 0 20 20
Condition33%1/3
defined $vmstat{"pswpin"} and defined $vmstat{"pswpout"}
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $vmstat{"pswpin"} and defined $vmstat{"pswpout"}
defined $vmstat{"pswpin"} defined $vmstat{"pswpout"}
Truth table33%1/3
A and B
A= defined $vmstat{"pswpin"}B= defined $vmstat{"pswpout"}
ABresult
0X0
100
111
182 20 return ( $vmstat{pswpin}, $vmstat{pswpout} );
183 }
184 9 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 9 0 9
185 9 return ( $self->sysctl('vm.stats.vm.v_swappgsin'), $self->sysctl('vm.stats.vm.v_swappgsout') );
186 }
187  
188 0 die( 'not supported on ' . $self->os . "\n" );
189 } ## end sub swap_counts
190  
191 =head2 swap
192  
193 Returns the bytes of swap in total and used. Dies if they can not be
194 found.
195  
196 =cut
197  
198 74 ▶sub swap {
Subroutine swap: called CC=5 SCAR=16.8
Pod documented
199 74 my ($self) = @_;
200  
201 74 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 50 24 74
202 50 my %meminfo = $self->proc_file('meminfo') =~ /^(\w+):\s+([0-9]+)/mg;
203 50 ▶ die("no SwapTotal in meminfo\n") if !defined( $meminfo{SwapTotal} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meminfo{"SwapTotal"} 0 50 50
204 50 ▶ return ( $meminfo{SwapTotal} * 1024, ( $meminfo{SwapTotal} - ( $meminfo{SwapFree} // 0 ) ) * 1024 );
Condition50%1/2
$meminfo{"SwapFree"} // 0
l!l
500
MC/DC0%0/1
$meminfo{"SwapFree"} // 0
$meminfo{"SwapFree"}
Truth table50%1/2
A
A= $meminfo{"SwapFree"}
Aresult
11
00
205 }
206 24 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 24 0 24
207 24 return $self->swapinfo;
208 }
209  
210 0 die( 'not supported on ' . $self->os . "\n" );
211 } ## end sub swap
212  
213 =head2 swapinfo
214  
215 Runs swapinfo -k and adds up its devices. Dies if it fails.
216  
217 Returns the bytes of swap in total and used.
218  
219 =cut
220  
221 24 ▶sub swapinfo {
Subroutine swapinfo: called CC=7 SCAR=19.5
Pod documented
222 24 my ($self) = @_;
223  
224 24 my ( $exit, $output ) = $self->run_command( $SWAPINFO, '-k' );
225 18 ▶ die( $SWAPINFO . ' exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 2 16 18
226 16 my @lines = split( /\n/, $output );
227  
228 # Device 1K-blocks Used Avail Capacity, with a Total line after more than one device
229 16 my ( $total, $used ) = ( 0, 0 );
230 16 foreach my $line (@lines) {
231 37 my ( $device, $blocks, $device_used ) = split( /\s+/, $line );
232 37 ▶ next if !defined($device_used) || $device eq 'Device' || $device eq 'Total' || $blocks !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $device_used or $device eq "Device" or $device eq "Total" or not $blocks =~ /\A[0-9]+\z/u 20 17 37
Condition77%7/9
not defined $device_used or $device eq "Device" or $device eq "Total" or not $blocks =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
20017
not defined $device_used or $device eq "Device" or $device eq "Total"
l!l&&r!l&&!r
16417
not defined $device_used or $device eq "Device"
l!l&&r!l&&!r
01621
MC/DC50%2/4
not defined $device_used or $device eq "Device" or $device eq "Total" or not $blocks =~ /\A[0-9]+\z/u
not defined $device_used $device eq "Device" $device eq "Total" not $blocks =~ /\A[0-9]+\z/u
Truth table60%3/5
not(A) or B or C or not(D)
A= not defined $device_usedB= $device eq "Device"C= $device eq "Total"D= not $blocks =~ /\A[0-9]+\z/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
233 17 $total += $blocks;
234 17 $used += $device_used;
235 }
236  
237 16 return ( $total * 1024, $used * 1024 );
238 } ## end sub swapinfo
239  
240 17 1;
« blib/lib/Monitoring/Sneck/Checks/sysctl.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/selinux.pm »