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

filter: Statementstmt 100% 45 / 45 Branchbran 100% 12 / 12 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 92.5% 75 / 81 CC 9 SCAR 14.9
CC
9
Cov
95%
check
8.0
validate
2.0
BEGIN
1.0
CRAP 4.4
« blib/lib/Monitoring/Sneck/Checks/systemd_failed.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/load.pm »
1 package Monitoring::Sneck::Checks::file_age;
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::file_age - checks how old and how big a file is
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 backup_age|perl:file_age -f /var/backups/db.sql.gz -w 90000 -c 180000 -W 1024:
23 heartbeat|perl:file_age -f /var/run/app.heartbeat -w 60 -c 300
24 nightly_dump|perl:file_age -n '/var/backups/db-*.sql.gz' -w 90000 -c 180000 -W 1024:
25  
26 =head1 DESCRIPTION
27  
28 Checks the age of a file, from when it was last changed, and its size.
29 A file that is not there is CRITICAL, unless --ignore-missing is given.
30  
31 With --newest, the file is the most recently changed one matching a
32 glob, for files named by date, such as backups. Nothing matching is
33 the same as a file not being there.
34  
35 As ranges, a plain number such as 240 is over 240 seconds, and 1024: is
36 under 1024 bytes.
37  
38 =head1 OPTIONS
39  
40 - -f, --file :: The file.
41  
42 - -n, --newest :: A glob, the newest file matching it being checked, in
43 place of --file.
44  
45 - -w, --warning :: Range of seconds for the age.
46 Default :: 240
47  
48 - -c, --critical :: Range of seconds for the age.
49 Default :: 600
50  
51 - -W, --size-warning :: Range of bytes for the size.
52  
53 - -C, --size-critical :: Range of bytes for the size.
54  
55 - -i, --ignore-missing :: A missing file is OK.
56  
57 One of --file or --newest must be given.
58  
59 =cut
60  
61 13 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
62 return (
63 13 'file|f=s', 'newest|n=s', 'warning|w=s', 'critical|c=s',
64 'size-warning|W=s', 'size-critical|C=s', 'ignore-missing|i'
65 );
66 }
67  
68 13 ▶sub defaults { return { warning => '240', critical => '600' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
69  
70 13 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
71 13 my ( $self, $options ) = @_;
72  
73 13 ▶ return ('give one of --file or --newest') if defined( $options->{file} ) == defined( $options->{newest} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"file"} == defined $options->{"newest"} 2 11 13
74  
75 11 return ();
76 }
77  
78 13 ▶sub ranges { return ( 'warning', 'critical', 'size-warning', 'size-critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
79  
80 =head1 METHODS
81  
82 =head2 check
83  
84 Does the check.
85  
86 =cut
87  
88 10 ▶sub check {
Subroutine check: called CC=8 SCAR=20.8
Pod documented
89 10 my ($self) = @_;
90  
91 10 my $file = $self->opt('file');
92 10 my $which = '';
93 10 ▶ if ( defined( $self->opt('newest') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("newest")) 3 7 10
94 3 require File::Glob;
95 3 my %changed = map { $_ => ( stat($_) )[9] } grep { -f $_ } File::Glob::bsd_glob( $self->opt('newest') );
96 3 ▶ ($file) = sort { $changed{$b} <=> $changed{$a} || $a cmp $b } keys(%changed);
Condition33%1/3
$changed{$b} <=> $changed{$a} || $a cmp $b
l!l&&r!l&&!r
100
MC/DC0%0/2
$changed{$b} <=> $changed{$a} || $a cmp $b
$changed{$b} <=> $changed{$a} $a cmp $b
Truth table33%1/3
A || B
A= $changed{$b} <=> $changed{$a}B= $a cmp $b
ABresult
1X1
011
000
97 3 ▶ if ( !defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 2 1 3
98 2 my $nothing = 'nothing matches ' . $self->opt('newest');
99 2 ▶ return $self->finish( $self->opt('ignore-missing') ? $self->OK : $self->CRITICAL, $nothing );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('ignore-missing') ? : 1 1 2
100 }
101 1 $which = 'the newest of ' . $self->opt('newest') . ', ';
102 } ## end if ( defined( $self->opt('newest') ) )
103 8 my @stat = stat($file);
104 8 ▶ if ( !defined( $stat[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stat[0]) 2 6 8
105 2 my $why = $!;
106 2 ▶ if ( $self->opt('ignore-missing') && !-e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("ignore-missing") and not -e $file) 1 1 2
Condition66%2/3
$self->opt("ignore-missing") and not -e $file
!ll&&!rl&&r
101
MC/DC50%1/2
$self->opt("ignore-missing") and not -e $file
$self->opt("ignore-missing") not -e $file
Truth table66%2/3
A and not(B)
A= $self->opt("ignore-missing")B= not -e $file
ABresult
0X0
100
111
107 1 return $self->finish( $self->OK, $file . ' is not there' );
108 }
109 1 return $self->finish( $self->CRITICAL, $file . ', ' . $why );
110 }
111  
112 6 my $age = time - $stat[9];
113 6 my $size = $stat[7];
114 6 $self->perfdata(
115 label => 'age',
116 value => $age,
117 uom => 's',
118 warning => $self->opt('warning'),
119 critical => $self->opt('critical')
120 );
121 6 $self->perfdata(
122 label => 'size',
123 value => $size,
124 uom => 'B',
125 warning => $self->opt('size-warning'),
126 critical => $self->opt('size-critical'),
127 min => 0
128 );
129 6 my $code = $self->worst(
130 $self->threshold_status( $age, $self->opt('warning'), $self->opt('critical') ),
131 $self->threshold_status( $size, $self->opt('size-warning'), $self->opt('size-critical') ),
132 );
133  
134 6 return $self->finish( $code, $which . $file . ' is ' . $age . ' seconds old and ' . $size . ' bytes' );
135 } ## end sub check
136  
137 3 1;
« blib/lib/Monitoring/Sneck/Checks/systemd_failed.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/load.pm »