#!/usr/bin/perl

#-----------------------------------------------------------------
# Copyright 2007 Sebastiaan Oostlander <sebastiaan@oostlander.com>
#
# GroupWise MTA check script
#-----------------------------------------------------------------

use Net::SNMP;
use Getopt::Std;
use Getopt::Long;
&Getopt::Long::config('bundling');
use strict;

my $script_name = "check_gwmta";
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 $opt_h;

### Warn levels ###
my $warn_msg = 10000;		# Nummer of messages last 10 minutes
my $warn_undlmsg = 10;		# Nummer of undeliverable messages last 10 minutes
my $warn_errormsg = 5;		# Nummer of error messages last 10 minutes
my $warn_undlmsgtot = 1000;	# Nummer of undeliverable messages total
my $warn_errormsgtot = 50;	# Nummer of error messages total

## FETCHING ARGS STUFF ##
my $status = GetOptions(
	"h"              => \$opt_h,
	"H=s"            => \$ipaddress,
	"C=s"            => \$defaultcommunity
);

if ( $status == 0 ) {
	print "\nERROR: Too few arguments\n";
    usage();
	exit(0);
}

if ($opt_h)
{
	usage();
	exit(0);
}

### OID ###
my $oid_gwmtaload = ".1.3.6.1.2.1.27.1.1.6.1";      # gwmta.nlm loaded
my $oid_gwdom = ".1.3.6.1.4.1.23.2.37.1.1.1.3.1";     # Number of Domains serviced by this MTA
my $oid_gwdomcl = ".1.3.6.1.4.1.23.2.37.1.1.1.4.1";   # Number of closed Domains serviced by this MTA
my $oid_gwpo = ".1.3.6.1.4.1.23.2.37.1.1.1.5.1";      # Number of Post Offices serviced by this MTA
my $oid_gwpocl = ".1.3.6.1.4.1.23.2.37.1.1.1.6.1";    # Number of closed Post Offices serviced by this MTA
my $oid_gwgw = ".1.3.6.1.4.1.23.2.37.1.1.1.7.1";      # Number of Gateways serviced by this MTA
my $oid_gwgwcl = ".1.3.6.1.4.1.23.2.37.1.1.1.8.1";    # Number of closed Gateways serviced by this MTA
my $oid_gwmsg = ".1.3.6.1.4.1.23.2.37.1.1.1.9.1";     # Number of messages routed since startup by this MTA
my $oid_gwmsg10 = ".1.3.6.1.4.1.23.2.37.1.1.1.10.1";  # Number of messages routed in the past 10 minutes by this MTA
my $oid_gwundl = ".1.3.6.1.4.1.23.2.37.1.1.1.11.1";   # Number of messages undeliverable since startup by this MTA
my $oid_gwundl10 = ".1.3.6.1.4.1.23.2.37.1.1.1.12.1"; # Number of messages undeliverable in the past 10 minutes by this MTA
my $oid_gwerr = ".1.3.6.1.4.1.23.2.37.1.1.1.13.1";    # Number of messages found in error since startup by this MTA
my $oid_gwerr10 = ".1.3.6.1.4.1.23.2.37.1.1.1.14.1";  # Number of messages found in error in the past 10 minutes by this MTA
my $oid_gwdb = ".1.3.6.1.4.1.23.2.37.1.1.1.30.1";     # Domain database status: Normal, DB Error, Unknown (String)
my $oid_gwname = ".1.3.6.1.4.1.23.2.37.1.1.1.2.1";    # Domain name
my $oid_generic = ".1.3.6.1.2.1.1.5.0";

### Results ###
my $result_gwmtaload = 0;
my $result_gwdom = 0;
my $result_gwdomcl = 0;
my $result_gwpo = 0;
my $result_gwpocl = 0;
my $result_gwgw = 0;
my $result_gwgwcl = 0;
my $result_gwmsg = 0;
my $result_gwmsg10 = 0;
my $result_gwundl = 0;
my $result_gwundl10 = 0;
my $result_gwerr = 0;
my $result_gwerr10 = 0;
my $result_gwdb = "";
my $result_gwname = "";

## 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 msg10=$result_gwmsg10 undl=$result_gwundl undl10=$result_gwundl10 err=$result_gwerr err10=$result_gwerr10";

if ($status == $OK)
{
	$statusstring = "- Messages (Total/Last10min) Normal $result_gwmsg/$result_gwmsg10  Undeliverable $result_gwundl/$result_gwundl10  Error $result_gwerr/$result_gwerr10";
	print "Domain $result_gwname is OK $statusstring $prefdata\n";
}
elsif ($status == $WARNING)
{
	print "Domain $result_gwname is WARNING $returnstring $prefdata\n";
}
elsif ($status == $CRITICAL)
{
	print "Domain $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_gwmtaload, $result_gwmtaload);
	getinfo($oid_gwdom, $result_gwdom);
	getinfo($oid_gwdomcl, $result_gwdomcl);
	getinfo($oid_gwpo, $result_gwpo);
	getinfo($oid_gwpocl, $result_gwpocl);
	getinfo($oid_gwgw, $result_gwgw);
	getinfo($oid_gwgwcl, $result_gwgwcl);
	getinfo($oid_gwmsg, $result_gwmsg);
	getinfo($oid_gwmsg10, $result_gwmsg10);
	getinfo($oid_gwundl, $result_gwundl);
	getinfo($oid_gwundl10, $result_gwundl10);
	getinfo($oid_gwerr, $result_gwerr);
	getinfo($oid_gwerr10, $result_gwerr10);
	getinfo($oid_gwdb, $result_gwdb);
	getinfo($oid_gwname, $result_gwname);

	# Checking
	if ($result_gwmtaload < 1)
	{
		# Error MTA not loadded
		$status = $CRITICAL;
		$returnstring .= "- MTA not loaded ";
	}
	else
	{
		# MTA Loaded
		if ($result_gwdb ne "Normal")
		{
			# DB Error
			$status = $CRITICAL;
			$returnstring .= "- Domain database error ";
		}
		else
		{
			### Closed Domains
			if ($result_gwdomcl > 0)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwdomcl Domains closed ";
			}

			### Closed Post Offices
			if ($result_gwpocl > 0)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwpocl Post Offices closed ";
			}

			### Closed Gateways
			if ($result_gwgwcl > 0)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwgwcl Gateways closed ";
			}

			### Messages routed last 10 min
			if ($result_gwmsg10 >= $warn_msg)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwmsg10 messages routed last 10 min ";
			}

			### Undeliverable messages last 10 min
			if ($result_gwundl10 >= $warn_undlmsg)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwundl10 undeliverable messages last 10 min ";
			}

			### Error messages last 10 mins
			if ($result_gwerr10 >= $warn_errormsg)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwerr10 error messages last 10 min ";
			}

			### Undeliverable messages total
			if ($result_gwundl >= $warn_undlmsgtot)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwundl undeliverable messages ";
			}

			### Error messages total
			if ($result_gwerr >= $warn_errormsgtot)
			{
				$status = $WARNING;
				$returnstring .= "- $result_gwerr error messages ";
			}
		}
	}
}

sub usage {
    print << "USAGE";

-----------------------------------------------------------------
$script_name v$script_version

Monitors GroupWise MTA via SNMP.

Usage: $script_name -H <hostname> [-C <community>]

Options: -H     Hostname or IP address
         -C     Community (default is public)

-----------------------------------------------------------------
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;
}