#! /usr/bin/perl -w

# check_file_pass.pl Copyright (C) 2010 Bobbie Stivers, Information Products Inc
#               Bobbie.S@InformationProductsInc.com
#
# Read a simple output file. Returns the contents and flags accordingly.
#
#
# 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 (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA

use strict;
use English;
use Getopt::Long;
use File::stat;
use vars qw($PROGVER);
use vars qw($PROGNAME);
use lib "/usr/lib/nagios/plugins";
use utils qw (%ERRORS &print_revision &support);

sub print_help ();
sub print_usage ();
sub trim ($);


my ($file_in,$status,$contents,$LINE_VAR);
my ($opt_f, $opt_h, $opt_V);
my ($result, $message);

$PROGNAME="check_file_pass";
$PROGVER="0.1";

$opt_f = "";

Getopt::Long::Configure('bundling');
GetOptions(
	"V"   => \$opt_V, "version"	=> \$opt_V,
	"h"   => \$opt_h, "help"	=> \$opt_h,
	"f=s" => \$opt_f, "file"	=> \$opt_f
);

if ($opt_V) {
	print_revision($PROGNAME, $PROGVER);
	exit $ERRORS{'OK'};
}

if ($opt_h) {
	print_help();
	exit $ERRORS{'OK'};
}

$opt_f = shift unless ($opt_f);

if (! $opt_f) {
	print "FILE_PASS UNKNOWN: No file specified\n";
	exit $ERRORS{'UNKNOWN'};
}

# Check that file exists (can be directory or link)
unless (-e $opt_f) {
	print "FILE_PASS CRITICAL: File not found - $opt_f\n";
	exit $ERRORS{'CRITICAL'};
}


if (! -r $opt_f)
{ 
    print "FILE_PASS CRITICAL: Bad permissions on $opt_f\n";
    exit $ERRORS{'CRITICAL'};
}
open(FILE,"< $opt_f");
$file_in = <FILE>;
close(FILE);
if(!defined $file_in)
{
    print "FILE_PASS CRITICAL: $opt_f could not be opened\n";
    exit $ERRORS{'CRITICAL'};
}

foreach $LINE_VAR ($file_in)
{
    chomp($LINE_VAR);
    ($status,$contents)=split(/\---/,$LINE_VAR);
    print "$contents\n";
    exit $ERRORS{trim($status)};
} 





sub print_usage () {
	print "Usage:\n";
	print "  $PROGNAME [-f <file>\n";
	print "  $PROGNAME [-h | --help]\n";
	print "  $PROGNAME [-V | --version]\n";
}
sub print_help () {
	print_revision($PROGNAME, $PROGVER);
	print "Copyright (C) 2010 Bobbie Stivers, Information Products Inc\n\n";
	print_usage();
	print "\n";
	print "  File contents must begin with either 'OK', 'WARNING'\n";
	print "       'CRITICAL', or 'UNKNOWN' followed by '---'\n";
	print "  There is no limit on the rest of the contents but it would best\n";
	print "       to limit it to 80 characters.\n";
	print "\n";
	support();
}
sub trim ($) {
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
