-
-
Save zz/3303054 to your computer and use it in GitHub Desktop.
Nagios Plugin for checking a backup file for existence, age and size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
### check_backup.pl | |
# By Nathan Vonnahme, Sept 2011 | |
# An example for "Writing Custom Nagios Plugins in Perl" at the Nagios | |
# World Conference North America 2011. | |
# I. Prologue | |
use strict; | |
use warnings; | |
use Nagios::Plugin; | |
use File::stat; | |
use vars qw($VERSION $PROGNAME $verbose $timeout $result); | |
$VERSION = '1.0'; | |
# get the base name of this script for use in the examples | |
use File::Basename; | |
$PROGNAME = basename($0); | |
# II. Usage/Help | |
my $p = Nagios::Plugin->new( | |
usage => "Usage: %s [ -v|--verbose ] [-t <timeout>] | |
[ -f|--file=<path/to/backup/file> ] | |
[ -a|--age=<max age in hours> ] | |
[ -s|--size=<acceptable min:max size in MB> ]", | |
version => $VERSION, | |
blurb => "Check the specified backup file's age and size", | |
extra => " | |
Examples: | |
$PROGNAME -f /backups/foo.tgz -a 24 -s 1024:2048 | |
Check that foo.tgz exists, is less than 24 hours old, and is between | |
1024 and 2048 MB. | |
" | |
); | |
# III. Command line arguments/options | |
# See Getopt::Long for more | |
$p->add_arg( | |
spec => 'file|f=s', | |
required => 1, | |
help => "-f, --file=STRING | |
The backup file to check. REQUIRED."); | |
$p->add_arg( | |
spec => 'age|a=i', | |
default => 24, | |
help => "-a, --age=INTEGER | |
Maximum age in hours. Default 24."); | |
$p->add_arg( | |
spec => 'size|s=s', | |
help => "-s, --size=INTEGER:INTEGER | |
Minimum:maximum acceptable size in MB (1,000,000 bytes)"); | |
# Parse arguments and process standard ones (e.g. usage, help, version) | |
$p->getopts; | |
# IV. Check arguments for sanity | |
# Perform sanity checking on command line options. | |
if ( (defined $p->opts->age) && $p->opts->age < 0 ) { | |
$p->nagios_die( " invalid number supplied for the age option " ); | |
} | |
# V. Check the stuff | |
# Check the backup file. | |
my $f = $p->opts->file; | |
unless (-e $f) { | |
$p->nagios_exit(CRITICAL, "File $f doesn't exist"); | |
} | |
my $mtime = File::stat::stat($f)->mtime; | |
my $age_in_hours = (time - $mtime) / 60 / 60; | |
my $size_in_mb = (-s $f) / 1_000_000; | |
my $message = sprintf "Backup exists, %.0f hours old, %.1f MB.", | |
$age_in_hours, $size_in_mb; | |
# VI. Performance Data | |
# Add perfdata, enabling pretty graphs etc. | |
$p->add_perfdata( label => "age", value => $age_in_hours, uom => "hours" ); | |
$p->add_perfdata( label => "size", value => $size_in_mb, uom => "MB" ); | |
# VII. Check against thresholds | |
my $result = $p->check_threshold( | |
check => $age_in_hours, | |
warning => undef, | |
critical => $p->opts->age | |
); | |
if ($result == OK) { | |
$result = $p->check_threshold( | |
check => $size_in_mb, | |
warning => $p->opts->size, | |
critical => undef, | |
); | |
} | |
# VIII. Exit Code | |
# Output the result and exit. | |
$p->nagios_exit( | |
return_code => $result, | |
message => $message | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment