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

filter: Statementstmt 98.0% 50 / 51 Branchbran 72.2% 13 / 18 Conditioncond 50.0% 8 / 16 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 81.6% 89 / 109 CC 17 SCAR 17.2
CC
17
Cov
84%
memory
8.9
arc_spare
7.9
check
4.0
CRAP 5.6
« blib/lib/Monitoring/Sneck/Checks/selinux.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/ntp.pm »
1 package Monitoring::Sneck::Checks::memory;
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::memory - checks how much memory is used
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 memory|perl:memory
23 memory_zfs|perl:memory -a -w 85 -c 95
24  
25 =head1 DESCRIPTION
26  
27 Checks the percent of memory used, which is what is not available to
28 start new programs with. On Linux that is MemAvailable from
29 /proc/meminfo, or MemFree, Buffers, and Cached added up on kernels
30 without it. On FreeBSD it is the free and inactive pages, through
31 L<BSD::Sysctl>. Other OSes are UNKNOWN.
32  
33 The ZFS ARC counts as used on both, as the kernel does not count it as
34 available, though most of it can be given back. --arc counts what it
35 holds over its minimum as available.
36  
37 =head1 OPTIONS
38  
39 - -w, --warning :: Range of percent used.
40 Default :: 90
41  
42 - -c, --critical :: Range of percent used.
43 Default :: 95
44  
45 - -a, --arc :: Count the ZFS ARC over its minimum as available.
46  
47 =cut
48  
49 30 ▶sub option_spec { return ( 'warning|w=s', 'critical|c=s', 'arc|a' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
50  
51 30 ▶sub defaults { return { warning => '90', critical => '95' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
52  
53 30 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
54  
55 =head1 METHODS
56  
57 =head2 check
58  
59 Does the check.
60  
61 =cut
62  
63 30 ▶sub check {
Subroutine check: called CC=4 SCAR=13.9
Pod documented
64 30 my ($self) = @_;
65  
66 30 my ( $total, $available ) = $self->memory;
67 30 ▶ $available = $available + $self->arc_spare if $self->opt('arc');
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("arc") 10 20 30
68 30 ▶ $available = $total if $available > $total;
Branch: 1 branch
BranchTrueFalseTotal
if $available > $total 0 30 30
69 30 my $used = $total - $available;
70 30 my $percent = sprintf( '%.1f', $used / $total * 100 );
71  
72 30 $self->perfdata(
73 label => 'used',
74 value => $percent,
75 uom => '%',
76 warning => $self->opt('warning'),
77 critical => $self->opt('critical'),
78 min => 0,
79 max => 100
80 );
81 30 $self->perfdata( label => 'used_bytes', value => $used, uom => 'B', min => 0, max => $total );
82 30 my $code = $self->threshold_status( $percent, $self->opt('warning'), $self->opt('critical') );
83  
84 30 ▶ return $self->finish( $code,
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('arc') ? : 10 20 30
85 $percent
86 . '% used, '
87 . _mib($used) . ' of '
88 . _mib($total) . ' MiB'
89 . ( $self->opt('arc') ? ', ARC over its minimum as available' : '' ) );
90 } ## end sub check
91  
92 =head2 memory
93  
94 Returns the bytes of memory in total and available. Dies if they can
95 not be found.
96  
97 =cut
98  
99 30 ▶sub memory {
Subroutine memory: called CC=8 SCAR=21.8
Pod documented
100 30 my ($self) = @_;
101  
102 30 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 20 10 30
103 20 my %meminfo = $self->proc_file('meminfo') =~ /^(\w+):\s+([0-9]+)/mg;
104 20 ▶ die("no MemTotal in meminfo\n") if !$meminfo{MemTotal};
Branch: 1 branch
BranchTrueFalseTotal
unless $meminfo{"MemTotal"} 0 20 20
105 my $available = $meminfo{MemAvailable}
106 20 ▶ // ( ( $meminfo{MemFree} // 0 ) + ( $meminfo{Buffers} // 0 ) + ( $meminfo{Cached} // 0 ) );
Condition62%5/8
$meminfo{"MemAvailable"} // ($meminfo{"MemFree"} // 0) + ($meminfo{"Buffers"} // 0) + ($meminfo{"Cached"} // 0)
l!l
155
$meminfo{"MemFree"} // 0
l!l
50
$meminfo{"Buffers"} // 0
l!l
50
$meminfo{"Cached"} // 0
l!l
50
MC/DC100%1/1
$meminfo{"MemAvailable"} // ($meminfo{"MemFree"} // 0) + ($meminfo{"Buffers"} // 0) + ($meminfo{"Cached"} // 0)
$meminfo{"MemAvailable"}
MC/DC0%0/1
$meminfo{"Buffers"} // 0
$meminfo{"Buffers"}
MC/DC0%0/1
$meminfo{"Cached"} // 0
$meminfo{"Cached"}
Truth table100%2/2
A
A= $meminfo{"MemAvailable"}
Aresult
11
00
Truth table50%1/2
A
A= $meminfo{"Buffers"}
Aresult
11
00
Truth table50%1/2
A
A= $meminfo{"Cached"}
Aresult
11
00
107 20 return ( $meminfo{MemTotal} * 1024, $available * 1024 );
108 }
109 10 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 10 0 10
110 10 my $page_size = $self->sysctl('hw.pagesize');
111 10 my $pages = $self->sysctl('vm.stats.vm.v_free_count') + $self->sysctl('vm.stats.vm.v_inactive_count');
112 10 return ( $self->sysctl('hw.physmem'), $pages * $page_size );
113 }
114  
115 0 die( 'not supported on ' . $self->os . "\n" );
116 } ## end sub memory
117  
118 =head2 arc_spare
119  
120 Returns the bytes the ZFS ARC holds over its minimum, 0 if there is no
121 ARC.
122  
123 =cut
124  
125 10 ▶sub arc_spare {
Subroutine arc_spare: called CC=7 SCAR=20.6
Pod documented
126 10 my ($self) = @_;
127  
128 10 my ( $size, $minimum );
129 10 ▶ if ( $self->os eq 'linux' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->os eq 'linux') { } 5 5 10
elsif ($self->os eq 'freebsd') { } 5 0 5
130 5 ▶ my $arcstats = eval { $self->proc_file('spl/kstat/zfs/arcstats') } // '';
Condition50%1/2
eval { do { $self->proc_file("spl/kstat/zfs/arcstats") } } // ""
l!l
50
MC/DC0%0/1
eval { do { $self->proc_file("spl/kstat/zfs/arcstats") } } // ""
eval { do { $self->proc_file("spl/kstat/zfs/arcstats") } }
Truth table50%1/2
A
A= eval { do { $self->proc_file("spl/kstat/zfs/arcstats") } }
Aresult
11
00
131 5 ($size) = $arcstats =~ /^size\s+\d+\s+([0-9]+)/m;
132 5 ($minimum) = $arcstats =~ /^c_min\s+\d+\s+([0-9]+)/m;
133 } elsif ( $self->os eq 'freebsd' ) {
134 5 $size = eval { $self->sysctl('kstat.zfs.misc.arcstats.size') };
135 5 $minimum = eval { $self->sysctl('kstat.zfs.misc.arcstats.c_min') };
136 }
137 10 ▶ return 0 if !defined($size) || !defined($minimum) || $size <= $minimum;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $size && defined $minimum or $size <= $minimum 0 10 10
Condition33%2/6
not defined $size && defined $minimum or $size <= $minimum
l!l&&r!l&&!r
0010
defined $size && defined $minimum
!ll&&!rl&&r
0010
MC/DC0%0/3
not defined $size && defined $minimum or $size <= $minimum
defined $size defined $minimum $size <= $minimum
Truth table25%1/4
not(A && B) or C
A= defined $sizeB= defined $minimumC= $size <= $minimum
ABCresult
0XX1
10X1
1111
1100
138  
139 10 return $size - $minimum;
140 } ## end sub arc_spare
141  
142 # Turns bytes into whole MiB.
143 #
144 # Args...
145 #
146 # - bytes :: The number of bytes.
147 #
148 # Returns the MiB, rounded.
149 #
150 # Example...
151 #
152 # _mib(1073741824); # 1024
153 60 ▶sub _mib {
Subroutine _mib: called CC=1 SCAR=0.0
154 60 return sprintf( '%.0f', $_[0] / 1048576 );
155 }
156  
157 7 1;
« blib/lib/Monitoring/Sneck/Checks/selinux.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/ntp.pm »