#!/usr/bin/perl
# nagios: +epn
#
# check_cacti_thold - nagios plugin 
#
# Copyright (C) 2006 Larry Low
#
# 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.
#
# Report bugs to:  llow0@yahoo.com
#
#
use strict;
use warnings;
use lib "/usr/lib/nagios/plugins"  ;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME);
use DBI;

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
	print ("ERROR: Plugin took too long to complete (alarm)\n");
	exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);

$PROGNAME = "check_cacti_thold.pl";
sub print_help ();
sub print_usage ();

my ($opt_h,$opt_V);
my $cacti_database = "cacti";
my $cacti_username = "cacti";
my ($cacti_password,$cacti_hostname,$cacti_port);
my $prefix = '';
my $suffix = '';
my ($thold_id);

use Getopt::Long;
&Getopt::Long::config('bundling');
GetOptions(
	"V"   => \$opt_V,	"version"    => \$opt_V,
	"h"   => \$opt_h,	"help"       => \$opt_h,
	"i=i" => \$thold_id,    "id=i"       => \$thold_id,
	"p=s" => \$prefix,      "prefix=s"   => \$prefix,
	"s=s" => \$suffix,      "suffix=s"   => \$suffix,
	"cacti_database=s" => \$cacti_database,
	"cacti_username=s" => \$cacti_username,
	"cacti_password=s" => \$cacti_password,
	"cacti_hostname=s" => \$cacti_hostname,
	"cacti_port=s" => \$cacti_port
);
# -h & --help print help
if ($opt_h) { print_help(); exit $ERRORS{'OK'}; }
# -V & --version print version
if ($opt_V) { print_revision($PROGNAME,'$Revision: 0.1 $ '); exit $ERRORS{'OK'}; }
# Thold id required
if (!defined($thold_id)) {
	print "Error: thold id is required.\n";
	print_usage();
	exit $ERRORS{'UNKNOWN'};
}
# Cacti password required
if (!defined($cacti_password)) {
	print "Error: cacti_password is required (or set staticaly in config).\n";
	print_usage();
	exit $ERRORS{'UNKNOWN'};
}

# Setup Database Connection
my $dsn = "DBI:mysql:database=$cacti_database";
if (defined($cacti_hostname)) {
	$dsn .= ";host=$cacti_hostname";
}
if (defined($cacti_port)) {
	$dsn .= ";port=$cacti_port";
}
my $dbh = DBI->connect($dsn,$cacti_username,$cacti_password);

if (!defined($dbh)) {
	print ("UNKNOWN: DB error: unable to connect to cacti database\n");
	exit $ERRORS{'UNKNOWN'};
}

my $state = 'UNKNOWN';
my $output = '';
# Begin plugin check code
{

	my $sql = "select thold_hi,thold_low,thold_alert,lastread from thold_data where id=$thold_id";
	my $sth = $dbh->prepare($sql);
	$sth->execute();
	my $row = $sth->fetchrow_hashref();
	$sth->finish();
	$dbh->disconnect();

	if (!defined($row->{'thold_alert'})) {
			print ("UNKNOWN: DB error: no thold data found in cacti database\n");
			exit $ERRORS{'UNKNOWN'};
	} else {
		my $lastread = $row->{'lastread'};
		my $thold_hi = $row->{'thold_hi'};
		my $thold_low = $row->{'thold_low'};
		my $thold_alert = $row->{'thold_alert'};

		if ($thold_alert != 0) {
			$state = 'CRITICAL';
		} else {
			$state = 'OK';
		}
		if (!defined($lastread)) {
			$output = "UNKNOWN";
			$state = 'UNKNOWN';
		} else {
			$output = "$prefix $lastread$suffix";
		}

		if (    (defined($thold_hi)  && (length($thold_hi) > 0)) && 
			(defined($thold_low) && (length($thold_low) > 0)) )  {
			$output .= " (Threshold Hi $thold_hi$suffix Low $thold_low$suffix)";
		} else {
			if (defined($thold_hi)  && (length($thold_hi) > 0)) {
				$output .= " (Threshold Hi $thold_hi$suffix)";
			}
			if (defined($thold_low) && (length($thold_low) > 0)) {
				$output .= " (Threshold Low $thold_low$suffix)";
			}
		}
	}
}
print "$state - $output\n";
exit $ERRORS{$state};

sub print_help() {
	print_revision($PROGNAME,'$Revision: 0.1 $ ');
	print "Copyright (c) 2006 Larry Low\n";
	print "This program is licensed under the terms of the\n";
	print "GNU General Public License\n(check source code for details)\n";
	print "\n";
	printf "Check Threshold value in Cacti from Thold Plugin.\n";
	print "\n";
	print_usage();
	print "\n";
	print " -H (--hostname)   Hostname to query - (required)\n";
	print " -V (--version)    Plugin version\n";
	print " -h (--help)       usage help\n";
	print " -i (--id)         thold id (required)\n";
        print " -p (--prefix)     prefix/description of output\n";
        print " -s (--suffix)     suffix for threshold values\n";
	print "\n";
	print " Cacti Integration:\n";
	print " --cacti_database  cacti database to get index from (default cacti)\n";
	print " --cacti_username  cacti database username (default cacti)\n";
	print " --cacti_password  cacti database password (required to enable check)\n";
	print " --cacti_hostname  cacti database hostname (default localhost)\n";
	print " --cacti_port      cacti database port number (default to 3306)\n";
	print "\n";
	support();
}

sub print_usage() {
	print "Usage: \n";
	print "  $PROGNAME -H <HOSTNAME> -i <THOLD ID>\n";
	print "  $PROGNAME [-h | --help]\n";
	print "  $PROGNAME [-V | --version]\n";
}

