-
Notifications
You must be signed in to change notification settings - Fork 2
/
check-lockfile.pl
executable file
·98 lines (83 loc) · 2.75 KB
/
check-lockfile.pl
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
92
93
94
95
96
97
98
#!/usr/bin/perl
############################## check_lockfile.pl ##############
# Version : 0.1
# Author : Jason Rojas
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
###########################################################
use strict;
use Getopt::Long;
use File::stat;
use Time::localtime;
my $Version='0.1';
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $o_help=undef; # wan't some help ?
my $o_version=undef; # print version
my $v_debug=undef; # Check if a file doesn't exist
my $o_age=undef; # Check if file is older than X seconds
my $o_file=undef; # Check if file is older than X seconds
my $o_paramok=undef;
sub print_version {
print "check_file version : $Version\n";
}
sub print_usage {
print "Usage: check_file.pl [-v] [-h] -a <age> -f <file>\n";
}
sub print_help {
print "\nNagios Plugin to check if a lock file exist/doesn't exist, and how old it is..\n";
print "This will also compare the age of the file, this is best used for lock files..\n\n";
print_usage();
print <<EOT;
-v, --version
Print version of this plugin
-h, --help
Print this help message
-a, --age
Go critical if file age is greater than X seconds.
-f, --file
File to perform the check on.
-d, --debug
what do you think?
EOT
}
sub check_options {
Getopt::Long::Configure("bundling");
GetOptions(
'h' => \$o_help, 'help' => \$o_help,
'v' => \$o_version, 'version' => \$o_version,
'd' => \$v_debug, 'debug' => \$v_debug,
"a=s" => \$o_age, "age=s" => \$o_age,
"f=s" => \$o_file, "file=s" => \$o_file,
);
if (defined ($o_help)) { print_help(); exit $ERRORS{"UNKNOWN"}};
if (defined ($o_version)) { print_version(); exit $ERRORS{"UNKNOWN"}};
if (!defined ($o_file) && !defined ($o_age)) { print_usage(); exit $ERRORS{"UNKNOWN"}};;
}
###### MAIN ######
check_options();
print "Checking $o_file \n" if $v_debug;
my $datetime = (stat($o_file))[9];
print "File mtime: $datetime \n" if $v_debug;
my $curtime = time;
print "Current time: $curtime \n" if $v_debug;
my $age = $curtime - $datetime;
print "Current file age: $age \n" if $v_debug;
my $exit_status = $ERRORS{"OK"};
if (! -e $o_file) {
print "File " . $o_file . " does not exist.\n";
exit $ERRORS{"OK"};
}
else {
print "File: " . $o_file . " exists!!!\n";
$exit_status = $ERRORS{"CRITICAL"};
}
if (defined ($o_age)) {
print "Checking: $age vs $o_age\n" if $v_debug;
if ($age < $o_age) {
$exit_status = $ERRORS{"OK"};
print "File is only $age seconds old..\n";
} else {
$exit_status = $ERRORS{"CRITICAL"};
print "File is $age seconds old!\nPlease check the buildmap process!\n";
}
}
exit $exit_status;