#! /usr/bin/perl -w
#
#  check_file_line_count.pl
#  by Zach Armstrong
#
#  This nagios check checks the number of lines in a file
#  and alerts when exceeding the given warn/crit values
#
# Copyright (c) 2010 Zachary Armstrong
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# 0 = ok
# 1 = warn
# 2 = crit
# 3 = unknown

#configurable options:
my $debug = 0;

# No changes below, unless you want to change exit codes
use lib "/usr/local/nagios/libexec" ;
use Getopt::Std;
use strict;
use warnings;
use utils qw( %ERRORS);
my $VERSION = '0.2';

%ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3);
my $exit_value = $ERRORS{'UNKNOWN'} ;

my %options=();

getopts("f:w:c:", \%options);

if (scalar(keys(%options))<3 )
{
	show_usage();
}
elsif ($options{'w'} > $options{'c'})
{
	$exit_value = $ERRORS{'CRITICAL'};
	print "ERROR: Warning value cannot be higher than critical value.";
	show_usage();
}

my $warnVal=$options{'w'};
my $critVal=$options{'c'};
my $filePath=$options{'f'};
my $exit_value_human="";
my $retval;

if (-e "$filePath")
{
	my $cmd = "wc -l $filePath |awk {'print \$1'}";
	$retval = `$cmd`;
	$retval=~ s/\s+$//;
}
else
{
	print "File \"$filePath\" does not exist. STATUS: UNKNOWN\n";
	exit $exit_value;
}

if ($debug)
{
  print "Count: $retval --";
} 

if (($retval >= $critVal))
{
	$exit_value = $ERRORS{'CRITICAL'};
} 
elsif (($retval >= $warnVal))
{
	$exit_value = $ERRORS{'WARNING'};
}

elsif (($retval < $warnVal) && ($retval < $critVal))
{
	$exit_value = $ERRORS{'OK'};
}

if ($exit_value == 0)
{
  $exit_value_human="OK";    
}
elsif ($exit_value == 1)
{
  $exit_value_human="WARNING";    
}
elsif ($exit_value == 2)
{
  $exit_value_human="CRITICAL";    
}
elsif ($exit_value == 3)
{
  $exit_value_human="UNKNOWN";    
}

print "There are $retval lines in $filePath. STATUS: $exit_value_human\n";
exit $exit_value;
		
sub show_usage
{
	print "\n\n";
	print "check_file_line_count.pl\n";
	print "by Zach Armstrong\n";
	print "\n";
	print "This nagios check checks the number of lines in a file\n";
	print "and alerts when exceeding the given warn/crit values\n";
	print "\n";
	print "usage:\n";
	print "./check_file_line_count.pl -f /path/to/file -w warnVal -c critVal\n";
	print "\n";
	print "-f /path/to/file\t\t The file you want to count\n";
	print "-w warnVal\t\tNumber of lines to warn at\n";
	print "-c critVal\t\tNumber of lines to crit at\n";
	print "\n\n";

	exit $exit_value; # This will register as unknown in nagios
}