#!/usr/bin/perl ############################# Created and written by Jeffrey Scott ############### # geof2001@gmail.com # copyright (c) 2015 Jeffrey Scott # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # contact the author directly for more information at: geof2001@gmail.com ################################################################################# # keep Check on any file in any directory # This plugin requires basic perl plugin on the system. # # Added by Stephane Lapie on 2017/05/26: # - a "-i" option to ignore the absence of a file, and return OK in that case use strict; use warnings; use English; use Getopt::Long; use File::stat; use vars qw($PROGNAME); use lib "/usr/lib/nagios/plugins"; use utils qw (%ERRORS &print_revision &support); sub print_help (); sub print_usage (); my ($opt_c, $opt_f, $opt_w, $opt_r, $opt_h, $opt_V, $opt_i); $PROGNAME="check_file_size"; $opt_w = 1024000; $opt_c = 4096000; $opt_f = ""; $opt_r = ""; Getopt::Long::Configure('bundling'); GetOptions( "V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "i" => \$opt_i, "ignore-absence" => \$opt_i, "r" => \$opt_r, "reverse-check" => \$opt_r, "f=s" => \$opt_f, "file" => \$opt_f, "w=f" => \$opt_w, "warning-size=f" => \$opt_w, "c=f" => \$opt_c, "critical-size=f" => \$opt_c); if ($opt_V) { print_revision($PROGNAME, '1.0.1'); exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } $opt_f = shift unless ($opt_f); if (! $opt_f) { print "FILE_SIZE UNKNOWN: No file specified\n"; exit $ERRORS{'UNKNOWN'}; } # check that file exists if ($opt_i) { unless (-e $opt_f) { print "FILE_SIZE OK: File not found - $opt_f\n"; exit $ERRORS{'OK'}; } } else { unless (-e $opt_f) { print "FILE_SIZE CRITICAL: File not found - $opt_f\n"; exit $ERRORS{'CRITICAL'}; } } my $st = File::stat::stat($opt_f); my $size = $st->size; my $result = 'OK'; if ($opt_r and $opt_c > $size) { $result = 'CRITICAL'; } elsif ((!$opt_r) and ($opt_c and $size >= $opt_c)) { $result = 'CRITICAL'; } elsif ((!$opt_r) and ($opt_w and $size >= $opt_w)) { $result = 'WARNING'; } print "FILE_SIZE $result: $opt_f is $size bytes \n"; exit $ERRORS{$result}; sub print_usage () { print "Options:\n"; print " -r Check if file is too small rather than too large.\n"; print " Warn size is not required and will be ignored if it is when checking if file is too small.\n"; print " e.g. file size expected is 4096 bytes but is only 1024 bytes.\n"; print " ./check_file_size -c 4095 -r -f /tmp/ \n\n"; print "Usage:\n"; print " $PROGNAME [-w ] [-c ] [-r ] -f \n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n"; } sub print_help () { print_revision($PROGNAME, '1.0.1'); print "Copyright (c) 2015 Jeffrey Scott\n\n"; print_usage(); print "\n"; print " File must be no larger than this size (default: warn 1024000 bytes, crit 4096000)\n"; print " -r File must be no smaller than this size (default: crit 4096000 bytes)\n"; print "\n"; support(); }