#!/usr/bin/perl

#-----------------------------------------------------------------
# Copyright 2007 Sebastiaan Oostlander <sebastiaan@oostlander.com>
#
# GroupWise Internet Agent check script
#-----------------------------------------------------------------

use Net::SNMP;
use Getopt::Std;
use Getopt::Long;
&Getopt::Long::config('bundling');
use strict;

my $script_name = "check_gwia";
my $script_version = "1.0";

## 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_queuemsg = 10000;	# nr of messages in queue
my $warn_queuedefer = 500;	# nr of defer messages in queue
my $warn_intmsg = 1000;		# nr of messages in interval
my $warn_interr = 100;		# nr of error messages in interval

## 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_gwianame = ".1.3.6.1.4.1.23.2.70.1.1.0";	# GWIA name
my $oid_gwmsgout = ".1.3.6.1.4.1.23.2.70.1.7.0";	# Out messages
my $oid_gwmsgin = ".1.3.6.1.4.1.23.2.70.1.8.0";		# In messages
my $oid_gwqsend = ".1.3.6.1.4.1.23.2.70.1.62.0";	# Queue send
my $oid_gwqrecv = ".1.3.6.1.4.1.23.2.70.1.63.0";	# Queue receive
my $oid_gwqdefer = ".1.3.6.1.4.1.23.2.70.1.64.0";	# Queue defer
my $oid_gwintmsgin = ".1.3.6.1.4.1.23.2.70.1.20.0";	# Interval messages in
my $oid_gwintmsgout = ".1.3.6.1.4.1.23.2.70.1.19.0";	# Interval messages out
my $oid_gwinterrin = ".1.3.6.1.4.1.23.2.70.1.24.0";	# Interval error in
my $oid_gwinterrout = ".1.3.6.1.4.1.23.2.70.1.23.0";	# Interval error out
my $oid_gwintstatin = ".1.3.6.1.4.1.23.2.70.1.22.0";	# Interval status in
my $oid_gwintstatout = ".1.3.6.1.4.1.23.2.70.1.21.0";	# Interval status out
my $oid_generic = ".1.3.6.1.2.1.1.5.0";

### Results ###
my $result_gwianame = "";
my $result_gwmsgout = 0;
my $result_gwmsgin = 0;
my $result_gwqsend = 0;
my $result_gwqrecv = 0;
my $result_gwqdefer = 0;
my $result_gwintmsgin = 0;
my $result_gwintmsgout = 0;
my $result_gwinterrin = 0;
my $result_gwinterrout = 0;
my $result_gwintstatin = 0;
my $result_gwintstatout = 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 = "| msgtotin=$result_gwmsgin msgtotout=$result_gwmsgout msgintin=$result_gwintmsgin msgintout=$result_gwintmsgout";

if ($status == $OK)
{
	$statusstring = "- Messages in/out  Last interval $result_gwintmsgin\/$result_gwintmsgout  Total $result_gwmsgin\/$result_gwmsgout";
	print "Internet Agent $result_gwianame is OK $statusstring $prefdata\n";
}
elsif ($status == $WARNING)
{
	print "Internet Agent $result_gwianame is WARNING $returnstring $prefdata\n";
}
elsif ($status == $CRITICAL)
{
	print "Internet Agent $result_gwianame 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_gwianame, $result_gwianame);
	getinfo($oid_gwmsgout, $result_gwmsgout);
	getinfo($oid_gwmsgin, $result_gwmsgin);
	getinfo($oid_gwqsend, $result_gwqsend);
	getinfo($oid_gwqrecv, $result_gwqrecv);
	getinfo($oid_gwqdefer, $result_gwqdefer);
	getinfo($oid_gwintmsgin, $result_gwintmsgin);
	getinfo($oid_gwintmsgout, $result_gwintmsgout);
	getinfo($oid_gwinterrin, $result_gwinterrin);
	getinfo($oid_gwinterrout, $result_gwinterrout);
	getinfo($oid_gwintstatin, $result_gwintstatin);
	getinfo($oid_gwintstatout, $result_gwintstatout);

	# Checking
	if (length($result_gwianame) == 0)
	{
		# Error not loadded
		$status = $CRITICAL;
		$returnstring .= "- Internet Agent not loaded ";
	}
	else
	{
		if ($result_gwqsend >= $warn_queuemsg)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwqsend messages in the send queue ";
		}
		if ($result_gwqrecv >= $warn_queuemsg)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwqrecv messages in the receive queue ";
		}
		if ($result_gwqdefer >= $warn_queuedefer)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwqdefer messages in the defer queue ";
		}

		if ($result_gwintmsgin >= $warn_intmsg)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwintmsgin inbound messages in the last interval ";
		}
		if ($result_gwintmsgout >= $warn_intmsg)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwintmsgout outbound messages in the last interval ";
		}

		if ($result_gwinterrin >= $warn_interr)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwinterrin inbound error messages in the last interval ";
		}
		if ($result_gwinterrout >= $warn_interr)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwinterrout outbound error messages in the last interval ";
		}
		if ($result_gwintstatin >= $warn_interr)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwintstatin inbound status messages in the last interval ";
		}
		if ($result_gwintstatout >= $warn_interr)
		{
			$status = $WARNING;
			$returnstring .= "- $result_gwintstatout outbound status messages in the last interval ";
		}
	}
}

sub usage
{
    print << "USAGE";

-----------------------------------------------------------------
$script_name v$script_version

Monitors GroupWise Internet Agent 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;
}