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

filter: Statementstmt 91.1% 41 / 45 Branchbran 60.0% 6 / 10 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 77.2% 68 / 88 CC 11 SCAR 16.9
CC
11
Cov
79%
check
10.6
_size
4.0
BEGIN
1.0
CRAP 5.4
« blib/lib/Monitoring/Sneck/Checks/ntp.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/systemd_failed.pm »
1 package Monitoring::Sneck::Checks::disk;
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::disk - checks how full file systems are
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 disk_root|perl:disk -p /
23 disks|perl:disk -p / -p /var -p /usr/home -w 85 -c 95 -W 80 -K 90
24  
25 =head1 DESCRIPTION
26  
27 Checks the percent used of each file system, and optionally of its
28 inodes, through L<Filesys::Df>. Used is as df shows it, out of what
29 users may use, so space kept for root is left out of the total. A path that can
30 not be looked at is CRITICAL.
31  
32 =head1 OPTIONS
33  
34 - -p, --path :: A path on the file system. May be given more than once.
35 Required.
36  
37 - -w, --warning :: Range of percent used.
38 Default :: 80
39  
40 - -c, --critical :: Range of percent used.
41 Default :: 90
42  
43 - -W, --inode-warning :: Range of percent of inodes used.
44  
45 - -K, --inode-critical :: Range of percent of inodes used.
46  
47 =cut
48  
49 5 ▶sub option_spec { return ( 'path|p=s@', 'warning|w=s', 'critical|c=s', 'inode-warning|W=s', 'inode-critical|K=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
50  
51 5 ▶sub defaults { return { warning => '80', critical => '90' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
52  
53 5 ▶sub required { return ('path') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
54  
55 5 ▶sub ranges { return ( 'warning', 'critical', 'inode-warning', 'inode-critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
56  
57 =head1 METHODS
58  
59 =head2 check
60  
61 Does the check.
62  
63 =cut
64  
65 5 ▶sub check {
Subroutine check: called CC=8 SCAR=23.6
Pod documented
66 5 my ($self) = @_;
67  
68 5 require Filesys::Df;
69 5 my @codes;
70 my @parts;
71 5 foreach my $path ( @{ $self->opt('path') } ) {
72 5 my $df = Filesys::Df::df( $path, 1 );
73 5 ▶ if ( !defined($df) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $df) 1 4 5
74 1 push( @codes, $self->CRITICAL );
75 1 ▶ push( @parts, $path . ' can not be looked at, ' . ( $! || 'unknown error' ) );
Condition50%1/2
$! || 'unknown error'
l!l
10
MC/DC0%0/1
$! || 'unknown error'
$!
Truth table50%1/2
A
A= $!
Aresult
11
00
76 1 next;
77 }
78  
79 4 my $code = $self->threshold_status( $df->{per}, $self->opt('warning'), $self->opt('critical') );
80 4 ▶ my $part = $path . ' ' . $df->{per} . '% used of ' . _size( $df->{user_blocks} // $df->{blocks} );
Condition33%1/3
$df->{"user_blocks"} // $df->{"blocks"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$df->{"user_blocks"} // $df->{"blocks"}
$df->{"user_blocks"} $df->{"blocks"}
Truth table33%1/3
A // B
A= $df->{"user_blocks"}B= $df->{"blocks"}
ABresult
1X1
011
000
81 $self->perfdata(
82 label => $path,
83 value => $df->{per},
84 4 uom => '%',
85 warning => $self->opt('warning'),
86 critical => $self->opt('critical'),
87 min => 0,
88 max => 100
89 );
90 4 ▶ if ( defined( $df->{fper} ) && $df->{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $df->{"fper"} and $df->{"files"}) 0 4 4
Condition33%1/3
defined $df->{"fper"} and $df->{"files"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $df->{"fper"} and $df->{"files"}
defined $df->{"fper"} $df->{"files"}
Truth table33%1/3
A and B
A= defined $df->{"fper"}B= $df->{"files"}
ABresult
0X0
100
111
91 my $inode_code
92 0 = $self->threshold_status( $df->{fper}, $self->opt('inode-warning'), $self->opt('inode-critical') );
93 0 $code = $self->worst( $code, $inode_code );
94 0 ▶ $part = $part . ', ' . $df->{fper} . '% inodes' if $inode_code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $inode_code != $self->OK 0 0 0
95 $self->perfdata(
96 label => $path . ' inodes',
97 value => $df->{fper},
98 0 uom => '%',
99 warning => $self->opt('inode-warning'),
100 critical => $self->opt('inode-critical'),
101 min => 0,
102 max => 100
103 );
104 } ## end if ( defined( $df->{fper} ) && $df->{files...})
105 4 push( @codes, $code );
106 4 push( @parts, $part );
107 } ## end foreach my $path ( @{ $self->opt('path') } )
108  
109 5 return $self->finish( $self->worst(@codes), join( ', ', @parts ) );
110 } ## end sub check
111  
112 # Turns bytes into a size for people, such as 931.5 GiB.
113 #
114 # Args...
115 #
116 # - bytes :: The number of bytes.
117 #
118 # Returns the size, in the largest unit it is at least one of.
119 #
120 # Example...
121 #
122 # _size(1073741824); # '1.0 GiB'
123 4 ▶sub _size {
Subroutine _size: called CC=4 SCAR=14.0
124 4 my ($bytes) = @_;
125  
126 4 my @units = ( 'B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' );
127 4 my $unit = 0;
128 4 ▶ while ( $bytes >= 1024 && $unit < $#units ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes >= 1024 and $unit < $#units) 12 4 16
Condition66%2/3
$bytes >= 1024 and $unit < $#units
!ll&&!rl&&r
4012
MC/DC50%1/2
$bytes >= 1024 and $unit < $#units
$bytes >= 1024 $unit < $#units
Truth table66%2/3
A and B
A= $bytes >= 1024B= $unit < $#units
ABresult
0X0
100
111
129 12 $bytes = $bytes / 1024;
130 12 $unit++;
131 }
132  
133 4 ▶ return $unit == 0 ? $bytes . ' B' : sprintf( '%.1f %s', $bytes, $units[$unit] );
Branch: 1 branch
BranchTrueFalseTotal
$unit == 0 ? : 0 4 4
134 } ## end sub _size
135  
136 3 1;
« blib/lib/Monitoring/Sneck/Checks/ntp.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/systemd_failed.pm »