Today is: 8 January, 2012
Check todays hot topics

Nagios check for log filesizes

#!/usr/bin/perl
 
#
# This is a very simple file size checking script.  It scans all files in 
# a specified directory for files which pass the specified thresholds.  The
# default notifications are 4GB for warning, and 5GB for critical.  Both 
# the thresholds and directory can be overwritten via the command line.
#
# Note that bz2, gz, and pid extensions are automatically ignored.
#
# $Id: check_local_logs,v 1.4 2007-08-28 22:26:05 taylor Exp $
#
 
use warnings;
use strict;
 
use File::Glob ':glob';
use File::stat;
use Getopt::Long;
 
my ($crit, $dir, $help, $warn);
 
GetOptions(	'c|crit=s'	=>	\$crit,
		'd|dir=s'	=>	\$dir,
		'h|help'	=>	\$help,
		'w|warn=s'	=>	\$warn,
);
 
sub stat_logs ($)
{
 
	my $log_dir = shift;
	my @glob = glob($log_dir . "/*");
 
	my @files = ();
 
	foreach my $filename (@glob) {
 
		next if ( $filename =~ m/[\.](bz2|gz|pid)$/ );
		next if ( -d $filename );
		push (@files, $filename);
 
	}
 
	nag_exit(2, "No log files were found.") if ( scalar(@files) == 0 );
 
	my %files_hash;
 
	foreach my $log (@files) {			
 
		my $file_stat = stat($log);
		my $file_size = $file_stat->size;
		$files_hash{$log} = $file_size;
 
	}
 
	return \%files_hash;
}
 
sub calculate_size ($) 
{
 
	my $value = shift;
	my $symbol = chop($value);
	my $bytes;
 
	$symbol =~ tr/A-Z/a-z/;
 
	if ($symbol eq "k") {
 
		$bytes = $value * 1024;
 
	} elsif ($symbol eq "m") {
 
		$bytes = $value * ( 1024 ** 2 );
 
	} elsif ($symbol eq "g") {
 
		$bytes = $value * ( 1024 ** 3 );
 
	} else {
 
		nag_exit(3, "Unknown byte symbol");
	}
 
	return $bytes;
 
}
 
sub nag_exit ($$)
{
 
   my ( $status, $msg ) = @_;
 
   if ( $status == 0 ) {
      print "OK - $msg\n";
   } elsif ( $status == 1 ) {
      print "WARNING - $msg\n";
   } elsif ( $status == 2 ) {
      print "CRITICAL - $msg\n";
   } else {
      print "UNKNOWN - $msg\n";
   }
 
   exit $status;
 
}
 
sub usage_exit 
{
 
   print <<ENDUSAGE;
 
Usage: $0 [OPTION]...
 
    Check the size of log files.  You can either specify a directory 
    and file size thresholds, or you can use the pre-defined defaults.  
 
    This script ignores files with the bz2, gz, and pid extensions.    
 
 
    --crit -c [critical threshold]   Define the critical threshold
                                     for a log file. 
 
                                     default: 5g
 
    --dir  -d [directory to scan]    Define the directory which will be 
                                     parsed for logs that exceed the 
	                             defined size.   
 
                                     default: /var/log
 
    --warn -w [warning threshold]    Define the warning threshold
                                     for a log file.
 
                                     default: 4g
 
    --help -h                        Display this usage info.
 
 
    Examples:
 
	$0 -d /var/log -w 100k -c 500k
	$0 -d /var/log -c 10g
	$0 -w 1g -c 5g
 
ENDUSAGE
 
   exit 0;
 
}
 
if ( defined($help) ) {
	usage_exit;
}
 
$dir = "/var/log" unless (defined($dir);
$crit = "5g" unless defined($crit);
$warn = "4g" unless defined($warn);
 
my $crit_bytes = calculate_size($crit);
my $warn_bytes = calculate_size($warn);
 
if ($warn_bytes > $crit_bytes) {
 
	nag_exit(2, "Warning settings cannot be greater than critical settings.");
 
}
 
if ( ! -d $dir ) {
 
	nag_exit(2, "Directory is not accessible.");
 
}
 
my %files_hash = %{ stat_logs($dir) };
 
foreach my $file_name (sort keys %files_hash) {
 
	foreach my $file_size ( $files_hash{$file_name} ) {
 
		if ($file_size > $crit_bytes) {
 
			nag_exit(2, "Log has exceeded pre-defined log size settings ( $file_name > $crit )");
 
		} elsif ($file_size > $warn_bytes && $file_size < $crit_bytes) {
 
			nag_exit(1, "Log has exceeded pre-defined log size settings ( $file_name > $warn )"); 
 
		}
 
	}
}
 
nag_exit(0, "All log files returned OK");