| 1 | package Monitoring::Sneck::Checks::mtime; | ||||||||||||||||||||||||||||
| 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::mtime - checks file mtimes against a time | ||||||||||||||||||||||||||||
| 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 | old_nginx|perl:mtime -e le -m 2023-06-22T00:00 -f /usr/local/sbin/nginx | ||||||||||||||||||||||||||||
| 23 | old_app|perl:mtime -m 2024-01-01 -f /usr/local/bin/app -f /usr/local/lib/libapp.so | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | A file whose mtime compares true against --mtime is CRITICAL, so with | ||||||||||||||||||||||||||||
| 28 | le, the default, a file from --mtime or before is, for finding | ||||||||||||||||||||||||||||
| 29 | binaries older than a known good build. A file that is not there is | ||||||||||||||||||||||||||||
| 30 | UNKNOWN. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | - -m, --mtime :: The time, in any format L<Time::Piece::Guess> knows, | ||||||||||||||||||||||||||||
| 35 | UTC if it has no zone. Required. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | - -e, --equality :: lt, le, ge, or gt. | ||||||||||||||||||||||||||||
| 38 | Default :: le | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | - -f, --file :: A file. May be given more than once. Required. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # How each equality reads in messages, and how it compares a file's mtime to the wanted time, both epochs. | ||||||||||||||||||||||||||||
| 45 | my %EQUALITIES = ( | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | lt => { words => 'before', compare => sub { $_[0] < $_[1] } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | le => { words => 'at or before', compare => sub { $_[0] <= $_[1] } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | ge => { words => 'at or after', compare => sub { $_[0] >= $_[1] } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | gt => { words => 'after', compare => sub { $_[0] > $_[1] } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 3 | ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # The Getopt::Long specs of the options, see OPTIONS. | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | # Returns the list of specs. | ||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||
| 56 | # Example... | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | # my @specs = $check->option_spec; | ||||||||||||||||||||||||||||
| 59 | # # ( 'mtime|m=s', 'equality|e=s', 'file|f=s@' ) | ||||||||||||||||||||||||||||
| 60 | 8 | ▶ | sub option_spec { return ( 'mtime|m=s', 'equality|e=s', 'file|f=s@' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # The option defaults, see OPTIONS. | ||||||||||||||||||||||||||||
| 63 | # | ||||||||||||||||||||||||||||
| 64 | # Returns a hash ref of option names to defaults. | ||||||||||||||||||||||||||||
| 65 | # | ||||||||||||||||||||||||||||
| 66 | # Example... | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # my $defaults = $check->defaults; | ||||||||||||||||||||||||||||
| 69 | # # { equality => 'le' } | ||||||||||||||||||||||||||||
| 70 | 8 | ▶ | sub defaults { return { equality => 'le' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # The options that must be given. | ||||||||||||||||||||||||||||
| 73 | # | ||||||||||||||||||||||||||||
| 74 | # Returns the list of option names. | ||||||||||||||||||||||||||||
| 75 | # | ||||||||||||||||||||||||||||
| 76 | # Example... | ||||||||||||||||||||||||||||
| 77 | # | ||||||||||||||||||||||||||||
| 78 | # my @required = $check->required; | ||||||||||||||||||||||||||||
| 79 | # # ( 'mtime', 'file' ) | ||||||||||||||||||||||||||||
| 80 | 8 | ▶ | sub required { return ( 'mtime', 'file' ) } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Checks what Getopt::Long can not, the equality being known and the time parsing. | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # Args... | ||||||||||||||||||||||||||||
| 85 | # | ||||||||||||||||||||||||||||
| 86 | # - options :: Hash ref of option names to values, defaults included. | ||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||
| 88 | # Returns a list of what is wrong, each a string, empty if nothing is. | ||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | # Example... | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | # my @problems = $check->validate( { mtime => 'nope', equality => 'eq', file => ['/usr/local/sbin/nginx'] } ); | ||||||||||||||||||||||||||||
| 93 | # # ( '--equality must be lt, le, ge, or gt', '--mtime "nope" is not a time Time::Piece::Guess knows' ) | ||||||||||||||||||||||||||||
| 94 | 8 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 8 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 8 | my @problems; | |||||||||||||||||||||||||||
| 98 | 8 | ▶ | push( @problems, '--equality must be lt, le, ge, or gt' ) if !$EQUALITIES{ $options->{equality} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 8 | ▶ | if ( defined( $options->{mtime} ) && !defined( _epoch( $options->{mtime} ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options->{"mtime"} and not defined _epoch($options->{"mtime"})
MC/DC50%1/2
defined $options->{"mtime"} and not defined _epoch($options->{"mtime"})
defined $options->{"mtime"}
not defined _epoch($options->{"mtime"})
Truth table66%2/3
A and not(B)
A= defined $options->{"mtime"}B= not defined _epoch($options->{"mtime"})
| |||||||||||||||||||||||||||||
| 100 | 1 | push( @problems, '--mtime "' . $options->{mtime} . '" is not a time Time::Piece::Guess knows' ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 8 | return @problems; | |||||||||||||||||||||||||||
| 104 | } ## end sub validate | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Turns a time into a epoch through Time::Piece::Guess, a time without a zone being UTC. | ||||||||||||||||||||||||||||
| 107 | # | ||||||||||||||||||||||||||||
| 108 | # Args... | ||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||
| 110 | # - time :: The time, in any format Time::Piece::Guess knows. | ||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||
| 112 | # Returns the epoch as a integer, or undef if Time::Piece::Guess does not know the format. | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # Example... | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | # my $epoch = _epoch('2023-06-22T00:00'); | ||||||||||||||||||||||||||||
| 117 | # # 1687392000 | ||||||||||||||||||||||||||||
| 118 | 13 | ▶ | sub _epoch { | ||||||||||||||||||||||||||
|
Subroutine
_epoch: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | 13 | my ($time) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 13 | require Time::Piece::Guess; | |||||||||||||||||||||||||||
| 122 | 13 | my $parsed = Time::Piece::Guess->guess_to_object($time); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 13 | ▶ | return defined($parsed) ? $parsed->epoch : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 METHODS | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 check | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Does the check. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 5 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | my $wanted_epoch = _epoch( $self->opt('mtime') ); | |||||||||||||||||||||||||||
| 139 | 5 | my $equality = $self->opt('equality'); | |||||||||||||||||||||||||||
| 140 | 5 | my @missing; | |||||||||||||||||||||||||||
| 141 | my @matched; | ||||||||||||||||||||||||||||
| 142 | 5 | foreach my $file ( @{ $self->opt('file') } ) { | |||||||||||||||||||||||||||
| 143 | 5 | my $mtime = ( stat($file) )[9]; | |||||||||||||||||||||||||||
| 144 | 5 | ▶ | if ( !defined($mtime) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | push( @missing, $file ); | |||||||||||||||||||||||||||
| 146 | 1 | next; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 4 | ▶ | push( @matched, $file ) if $EQUALITIES{$equality}{compare}->( $mtime, $wanted_epoch ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 5 | ▶ | return $self->finish( $self->UNKNOWN, 'not there: ' . join( ', ', @missing ) ) if @missing; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 4 | my $wanted_text = 'mtime ' . $EQUALITIES{$equality}{words} . ' ' . $self->opt('mtime'); | |||||||||||||||||||||||||||
| 153 | 4 | ▶ | return $self->finish( $self->CRITICAL, $wanted_text . ': ' . join( ', ', @matched ) ) if @matched; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | return $self->finish( $self->OK, 'none with a ' . $wanted_text ); | |||||||||||||||||||||||||||
| 156 | } ## end sub check | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 3 | 1; | |||||||||||||||||||||||||||