#!/usr/bin/perl -w
#
# File: snmp__gwpoa_
# Copyright (C) 2007 Gabriele Pohl (contact@dipohl.de)
#
# Derived from plugin snmp__load
# Copyright (C) 2004 Jimmy Olsen, Dagfinn Ilmari Mannsaaker
#
# 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; version 2 dated June,
# 1991.
#
# 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.
#
# ------------------------------------------------------------
# Plugin to monitor Novell Groupwise Post Office Agent (POA)
# ------------------------------------------------------------
# 
# Management Information Base (MIB) GWPOA 
#
# Naming Tree: 1.3.6.1.4.1.23
#      iso(1) org(3) dod(6) internet(1) private(4) enterprises(1) novell(23)
#
# To see all values available for your GWPOA, type
# snmpwalk -v1 -c public -m GWPOA-MIB <HOST> gwpoa
#
# This plugin fetches:  
#
# * poaPostOfficeName - 1.3.6.1.4.1.23.2.38.1.1.1.2.
# * poaTotalMsgs - 1.3.6.1.4.1.23.2.38.1.1.1.3.
# * poaUndeliverableMsgs - 1.3.6.1.4.1.23.2.38.1.1.1.8.
# * poaProblemMsgs - 1.3.6.1.4.1.23.2.38.1.1.1.4.
# * poaAdmErrorMsg - 1.3.6.1.4.1.23.2.38.1.1.1.27.
#
# Usage:
# --------------
# Plugin needs to be linked to your servicedir [/etc/munin/plugins]
# with the hostname (host) and the table index (pos) of the POA-Object
# defined in the linkage.
#
#   snmp_<host>_gwpoa_<pos>  
#
# with:
#   <host> = Name or IP-Number of host 
#   <pos> = Table index of the POA Object
#
#
# E.g.
#    ln -s /usr/share/munin/plugins/snmp__gwpoa_ \
#    /etc/munin/plugins/snmp_foo.example.com_gwpoa_1
#
# ...will monitor the first POA-Object on host foo.example.com. 
#
#
# Configuration:
# --------------
# Parameters
#	community - Specify wich community string to use (Default: public)
# 	port - Specify which port to read from (Default: 161)
#	host - Specify which host to monitor (Default: Read from link in servicedir)
#	pos - Specify which table Object to read (Default: Read from link in servicedir, 
# 
# You may adjust settings via configuration in plugin-conf.d/munin-node:
#   [snmp_*_gwpoa_*]
#   env.port <your_port_number>
#   env.community <your SNMP community string>
#   env.pos <your table position. Values: 0,1,2,..> 
#   env.host <name or IP of your host>
#
# Parameters can also be specified on a per POA basis, eg:
#   [snmp_foo.example.com_gwpoa_2]
#   env.port 166
#   env.community example
#
# $Log$
# Revision 1.0	2007/09/02 11:27	gap
# Created by gap
#
#
#%# family=snmpauto
#%# capabilities=snmpconf

use strict;
use Net::SNMP;

my $DEBUG = 0;

my $host      = $ENV{host}      || undef;
my $port      = $ENV{port}      || 161;
my $community = $ENV{community} || "public";
my $pos       = $ENV{pos} || 1;

my $response;

my $GRAPH_PERIOD = "minute";
my $GRAPH_VLABEL = "messages per $GRAPH_PERIOD";
my $TOTAL_LABEL = "TotalMsgs";
my $UNDELIVERABLE_LABEL = "UndeliverableMsgs";	
my $PROBLEM_LABEL = "ProblemMsgs";
my $ERRORS_LABEL = "AdmErrorMsgs";
my $ERRORS_CRITICAL = 10;

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
	print "index 1.3.6.1.4.1.23.2.38.1.1.1.1.\n"; # gwpoa 
	print "require 1.3.6.1.4.1.23.2.38.1.1.1.2. [.*]\n"; # poaPostOfficeName 
	print "require 1.3.6.1.4.1.23.2.38.1.1.1.3. [\\d*]\n"; # poaTotalMsgs
	print "require 1.3.6.1.4.1.23.2.38.1.1.1.4. [\\d*]\n"; # poaProblemMsgs
	print "require 1.3.6.1.4.1.23.2.38.1.1.1.8. [\\d*]\n"; # poaUndeliverableMsgs
	print "require 1.3.6.1.4.1.23.2.38.1.1.1.27. [\\d*]\n"; # poaAdmErrorMsgs

	exit 0;
}
if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_gwpoa_(\d+)*$/)
{
	$host  = $1;
	# take default value for pos if not set in link
	if (defined($2)) { $pos   = $2; }
}
elsif (!defined($host))
{
	print "# Debug: $0 -- $1\n" if $DEBUG;
	die "# Error: couldn't understand what I'm supposed to monitor.";
}

my ($session, $error) = Net::SNMP->session(
		-hostname  => $host,
		-community => $community,
		-port      => $port
	);

if (!defined ($session))
{
	die "Croaking: $error";
}

if (defined $ARGV[0] and $ARGV[0] eq "config")
{

	# get Post Office Name 
	my $poname = &get_single ($session, "1.3.6.1.4.1.23.2.38.1.1.1.2.$pos"); # poaPostOfficeName 

	# output to munin
	print "host_name $host
graph_category mail
graph_args --base 1000
graph_period $GRAPH_PERIOD
graph_title GWPOA load ($poname)
graph_info Shows per minute activity of the Groupwise PostOfficeAgent (GWPOA), here: $poname.<br />
graph_vlabel $GRAPH_VLABEL
total_msgs.label $TOTAL_LABEL
total_msgs.info poaTotalMsgs
total_msgs.type DERIVE
total_msgs.min 0
total_msgs.draw LINE2
total_msgs.graph yes
problem_msgs.label $PROBLEM_LABEL
problem_msgs.info poaProblemMsgs
problem_msgs.type DERIVE
problem_msgs.min 0
problem_msgs.draw LINE2
problem_msgs.graph yes
undeliverable_msgs.label $UNDELIVERABLE_LABEL
undeliverable_msgs.info poaUndeliverableMsgs
undeliverable_msgs.type DERIVE
undeliverable_msgs.min 0
undeliverable_msgs.draw LINE2
undeliverable_msgs.graph yes
errors.label $ERRORS_LABEL
errors.info poaAdmErrorMsgs
errors.critical $ERRORS_CRITICAL
errors.type DERIVE
errors.min 0
errors.draw LINE2
errors_out.graph yes";

	exit 0;
}
	# fetch the data and print
	print "total_msgs.value ", &get_single ($session, "1.3.6.1.4.1.23.2.38.1.1.1.3.$pos"), "\n"; # poaTotalMsgs
	print "problem_msgs.value ", &get_single ($session, "1.3.6.1.4.1.23.2.38.1.1.1.4.$pos"), "\n"; # poaProblemMsgs
	print "undeliverable_msgs.value ", &get_single ($session, "1.3.6.1.4.1.23.2.38.1.1.1.8.$pos"), "\n"; # poaUndeliverableMsgs
	print "errors.value ", &get_single ($session, "1.3.6.1.4.1.23.2.38.1.1.1.27.$pos"), "\n"; # poaAdmErrorMsgs


sub get_single
{
	my $handle = shift;
	my $oid    = shift;

	print "# Getting single $oid...\n" if $DEBUG;

	$response = $handle->get_request ($oid);

	if (!defined $response->{$oid})
	{
	    return undef;
	}
	else
	{
	    return $response->{$oid};
	}
}

