-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_files_age
executable file
·91 lines (74 loc) · 2.23 KB
/
check_files_age
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/perl
# Copyright 2012 TripAdvisor, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Finds all files that match a pattern and checks the age of the youngest.
#
# Option:
# -w age in hours for warning
# -c age in hours for critical
###############################################################################
use Getopt::Long;
use warnings;
use strict;
# nagios variables
my $CRITICAL = 2;
my $WARNING = 1;
my $OK = 0;
my $test = 'FILES_AGE';
###############################################################################
# Handle options
# default options
my $warning = 25;
my $critical = 30;
my $directory;
my $pattern;
GetOptions(
"w=i" => \$warning,
"c=i" => \$critical,
"d=s" => \$directory,
"p=s" => \$pattern,
) or die "Cannot parse options, $!\n";
$directory or die "Must specify a directory with -d";
$pattern or die "Must specify a pattern with -p";
###############################################################################
sub mtime() { return (stat($_[0]))[9] }
opendir(DIR,$directory) or die "Cannot open directory $directory, $!\n";
my @files = grep(/$pattern/, readdir(DIR));
closedir(DIR);
my $file = shift @files;
my $youngestAge = &mtime("$directory/$file");
my $youngestFile = $file;
for my $file (@files) {
my $mtime = &mtime("$directory/$file");
if ($mtime > $youngestAge) {
$youngestAge = $mtime;
$youngestFile = $file;
}
}
my $age = int((time - $youngestAge)/(60*60));
my $msg = "Youngest file $file is $age hours old";
my $status;
if ($age > $critical) {
$msg = "$test CRITICAL: $msg";
$status = $CRITICAL;
} elsif ($age > $warning) {
$msg = "$test WARNING: $msg";
$status = $WARNING;
} else {
$msg = "$test OK: $msg";
$status = $OK;
}
print $msg,"\n";
exit $status;