#!/usr/bin/perl

#-----------------------------------------------------------------
# Copyright 2007 Sebastiaan Oostlander <sebastiaan@oostlander.com>
#
# GroupWise POA check script
#-----------------------------------------------------------------

use Net::SNMP;
use Getopt::Std;
use Getopt::Long;
&Getopt::Long::config('bundling');
use strict;

my $script_name = "check_gwpoa";
my $script_version = "1.1";

## CONNECTION STUFF ##
my $ipaddress = "";					# there is not default ip address, sorry
my $version = 1;					# SNMP version, old UM_LINK hw works with version 1!!.
my $timeout = 2;					# SNMP query timeout
my $defaultcommunity = "public";	# Default community string
my $instance = 1;
my $opt_h;

### Warn levels ###
my $warn_undlmsg = 1000;	# Nummer of undeliverable messages
my $warn_probmsg = 100;		# Nummer of problem message

## FETCHING ARGS STUFF ##
my $status = GetOptions(
	"h"              => \$opt_h,
	"H=s"            => \$ipaddress,
	"C=s"            => \$defaultcommunity,
	"I=i"            => \$instance,
	"u=i"            => \$warn_undlmsg,
	"p=i"            => \$warn_probmsg
);

if ( $status == 0 ) {
	print "\nERROR: Too few arguments\n";
    usage();
	exit(0);
}

if ($opt_h)
{
	usage();
	exit(0);
}


### OID ###
my $oid_gwpoaload = ".1.3.6.1.2.1.27.1.1.6.2";      			# gwpoa.nlm loaded
my $oid_gwname = ".1.3.6.1.4.1.23.2.38.1.1.1.2.$instance";    	# Post Office name
my $oid_gwdb = ".1.3.6.1.4.1.23.2.38.1.1.1.29.$instance";		# Post Office database status: Normal, DB Error, Unknown (String)
my $oid_gwmsg = ".1.3.6.1.4.1.23.2.38.1.1.1.3.$instance";     	# Number of total messages
my $oid_gwundl = ".1.3.6.1.4.1.23.2.38.1.1.1.8.$instance";     	# Number of undeliverable messages
my $oid_gwprob = ".1.3.6.1.4.1.23.2.38.1.1.1.4.$instance";     	# Number of problem messages
my $oid_gwcsconn = ".1.3.6.1.4.1.23.2.38.1.1.1.21.$instance";	# Number of current C/S connections
my $oid_generic = ".1.3.6.1.2.1.1.5.0";

### Results ###
my $result_gwpoaload = 0;
my $result_gwname = "";
my $result_gwdb = "";
my $result_gwmsg = 0;
my $result_gwundl = 0;
my $result_gwprob = 0;
my $result_gwcsconn = 0;
my $result_generic = 0;

## INTERESTING DEFINES ##
my $OK = 0;
my $WARNING = 1;
my $CRITICAL = 2;

## RETURN INFO VARIABLES ##
my $status = $OK;
my $returnstring = "";
my $statusstring = "";
my $prefdata = "";

## MAKING SNMP CONNECTION ##
my ($s, $e) = Net::SNMP->session(
     -community  =>  $defaultcommunity,
     -hostname   =>  $ipaddress,
     -version    =>  $version,
     -timeout    =>  $timeout,
);

## TESTING SNMP CONNECTION WITH A GENERIC QUERY ##
if (!defined($s->get_request($oid_generic)))
{
	$returnstring = "- SNMP server not responding, host down?";
	$status = $CRITICAL;
}
else
{
	## DOING ALL WORK ##
	main();
}

## CONNECTION TO /DEV/NULL ##
$s->close();

## STUDYING THE OUTPUT ##
$prefdata = "| msg=$result_gwmsg undl=$result_gwundl prob=$result_gwprob csconn=$result_gwcsconn";

if ($status == $OK)
{
	$statusstring = "- $result_gwcsconn C/S Connections - Messages (Total) Normal $result_gwmsg  Undeliverable $result_gwundl  Problem $result_gwprob";
	print "Post Office $result_gwname is OK $statusstring $prefdata\n";
}
elsif ($status == $WARNING)
{
	print "Post Office $result_gwname is WARNING $returnstring $prefdata\n";
}
elsif ($status == $CRITICAL)
{
	print "Post Office $result_gwname is CRITICAL $returnstring $prefdata\n";
}

## GOOD BYE ##
exit $status;

##
## getinfo: make a snmp query with OID (arg0) and put it in arg1.

sub getinfo
{
	if(!defined($s->get_request(@_[0])))
	{
		# print "OID ";
		# print @_[0];
		# print " not exists, and can't be checked, skipping\n";
		$_[1] = undef;
		return;
	}

	foreach ($s->var_bind_names()) {
		$_[1] = $s->var_bind_list()->{$_};
	}
}

##
## main: all queries and sets

sub main
{
	## GETTING DATA ##
	getinfo($oid_gwpoaload, $result_gwpoaload);
	getinfo($oid_gwname, $result_gwname);
	getinfo($oid_gwdb, $result_gwdb);
	getinfo($oid_gwmsg, $result_gwmsg);
	getinfo($oid_gwundl, $result_gwundl);
	getinfo($oid_gwprob, $result_gwprob);
	getinfo($oid_gwcsconn, $result_gwcsconn);

	# Checking
	if ($result_gwpoaload < 1)
	{
		# Error MTA not loadded
		$status = $CRITICAL;
		$returnstring .= "- POA not loaded ";
	}
	else
	{
		# MTA Loaded
		if ($result_gwdb ne "Normal")
		{
			# DB Error
			$status = $CRITICAL;
			$returnstring .= "- Post Office database error ";
		}
		else
		{
			### Undeliverable messages
			if ($result_gwundl >= $warn_undlmsg)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwundl undeliverable messages ";
			}

			### Problem messages
			if ($result_gwprob >= $warn_probmsg)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwprob problem messages ";
			}
		}
	}

}

sub usage
{
    print << "USAGE";

-----------------------------------------------------------------
$script_name v$script_version

Monitors GroupWise POA via SNMP.

Usage: $script_name -H <hostname> [-C <community>]

Options: -H     Hostname or IP address
         -C     Community (default is public)
		 -i     Instance id (default is 1)
         -u     Warning undeliverable messages
         -p     Warning problem messages

-----------------------------------------------------------------
Copyright 2007 Sebastiaan Oostlander <sebastiaan\@oostlander.com>

Thanks to the plugin from Ignacio Barrientos <chipi\@criptonita.com>

This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
-----------------------------------------------------------------

USAGE
     exit 1;
}