#!/usr/bin/perl -w
#
# File: snmp__gwia_msgs__
# 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 Internet Agent (GWIA)
# ------------------------------------------------------------
# 
# Management Information Base (MIB) GWIAMIB 
#
# 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 GWIA, type
# snmpwalk -v1 -c public -m GWIAMIB <HOST> gwia
#
# This plugin fetches:  
#
# * gwiaGatewayName - 1.3.6.1.4.1.23.2.70.1.1.
# * gwiaStatMsgsOut - 1.3.6.1.4.1.23.2.70.1.7.
# * gwiaStatMsgsIn - 1.3.6.1.4.1.23.2.70.1.8.
# * gwiaStatStatusesOut - 1.3.6.1.4.1.23.2.70.1.9.
# * gwiaStatStatusesIn - 1.3.6.1.4.1.23.2.70.1.10. 
# * gwiaStatErrorsOut - 1.3.6.1.4.1.23.2.70.1.11. 
# * gwiaStatErrorsIn - 1.3.6.1.4.1.23.2.70.1.12.
#
# Usage:
# --------------
# Link this file snmp__gwia_msgs_ to your nodes servicedir [/etc/munin/plugins] 
#
# as: 
#   snmp_<host>_gwia_msgs_<pos>  
#
# with:
#   <host> = Name or IP-Number of host 
#   <pos> = table index of the GWIA Object
# 
# E.g.
#    ln -s /usr/share/munin/plugins/snmp__gwia_msgs_ \
#    /etc/munin/plugins/snmp_foo.example.com_gwia_msgs_0
# ...will monitor a single GWIA object on host foo.example.com. 
#
# 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 to your need via configuration in plugin-conf.d/munin-node:
#   [snmp_*_gwia_msgs_*]
#   env.port <your_port_number>
#   env.community <your SNMP community string>
#   env.pos <your objects table position. Values: 0,1,2,..> 
#   env.host <name or IP of your host>
#
# Parameters can also be specified on a per GWIA basis, eg:
#   [snmp_foo.example.com_gwia_msgs_1]
#   env.port 166
#   env.community example
#
# $Log$
# Revision 1.0	2007/09/08 16:08	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} || 0;

my $response;

# fix values
my $GRAPH_CATEGORY = "Groupwise";
my $GRAPH_PERIOD = "minute";
my $GRAPH_VLABEL = "messages per $GRAPH_PERIOD in(-) / out(+)";
my $MSGS_LABEL='Messages';
my $STATS_LABEL='Status-Msgs';
my $ERRORS_LABEL='Errors';
my $ERRORS_CRITICAL=10;

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{	
	print "require 1.3.6.1.4.1.23.2.70.1.1. [.*]\n"; # gwiaGatewayName 
	print "require 1.3.6.1.4.1.23.2.70.1.7. [\\d*]\n"; # gwiaStatMsgsOut
	print "require 1.3.6.1.4.1.23.2.70.1.8. [\\d*]\n"; # gwiaStatMsgsIn
	print "require 1.3.6.1.4.1.23.2.70.1.11. [\\d*]\n"; # gwiaStatErrorsOut
	print "require 1.3.6.1.4.1.23.2.70.1.12. [\\d*]\n"; # gwiaStatErrorsIn
	print "require 1.3.6.1.4.1.23.2.70.1.9. [\\d*]\n"; # gwiaStatStatusesOut
	print "require 1.3.6.1.4.1.23.2.70.1.10. [\\d*]\n"; # gwiaStatStatusesIn

	exit 0;
}

if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_gwia_msgs_(\d+)*$/)
{
	$host  = $1;
	$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 name of Internet Agent 
	my $gwname = &get_single ($session, "1.3.6.1.4.1.23.2.70.1.1.$pos"); # gwiaGatewayName 

        # output to munin
	print "host_name $host
graph_category Groupwise
graph_args --base 1000
graph_period $GRAPH_PERIOD
graph_title GWIA msg load ($gwname)
graph_info Shows per minute activity of the Groupwise Internet Agent (GWIA), here: $gwname.<br />Outgoing data will be reported as positive value, incoming as negative value.
graph_vlabel $GRAPH_VLABEL
msgs_in.label $MSGS_LABEL
msgs_in.info gwiaStatMsgsIn (1.3.6.1.4.1.23.2.70.1.8.)
msgs_in.type DERIVE
msgs_in.min 0
msgs_in.draw AREA
msgs_in.graph no
msgs_out.label $MSGS_LABEL
msgs_out.info gwiaStatMsgsOut (1.3.6.1.4.1.23.2.70.1.7.)
msgs_out.type DERIVE
msgs_out.min 0
msgs_out.draw AREA
msgs_out.negative msgs_in
msgs_out.graph yes
stats_in.label $STATS_LABEL
stats_in.info gwiaStatStatusesIn (1.3.6.1.4.1.23.2.70.1.10.)
stats_in.type DERIVE
stats_in.min 0
stats_in.draw LINE1
stats_in.graph no
stats_out.label $STATS_LABEL
stats_out.info gwiaStatStatusesOut (1.3.6.1.4.1.23.2.70.1.9.)
stats_out.type DERIVE
stats_out.min 0
stats_out.draw LINE1
stats_out.negative stats_in
stats_out.graph yes
errors_in.label $ERRORS_LABEL
errors_in.info gwiaStatErrorsIn (1.3.6.1.4.1.23.2.70.1.12.)
errors_in.critical $ERRORS_CRITICAL
errors_in.type DERIVE
errors_in.min 0
errors_in.draw LINE1
errors_in.graph no
errors_out.label $ERRORS_LABEL
errors_out.info gwiaStatErrorsOut (1.3.6.1.4.1.23.2.70.1.11.)
errors_out.critical $ERRORS_CRITICAL
errors_out.type DERIVE
errors_out.negative errors_in
errors_out.min 0
errors_out.draw LINE1
errors_out.graph yes";

	exit 0;
}
	# fetch the data and print
	print "msgs_in.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.8.$pos"), "\n"; # gwiaStatMsgsIn
	print "msgs_out.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.7.$pos"), "\n"; # gwiaStatMsgsOut
	print "stats_in.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.10.$pos"), "\n"; # gwiaStatStatusesIn
	print "stats_out.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.9.$pos"), "\n"; # gwiaStatStatusesOut
	print "errors_in.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.12.$pos"), "\n"; # gwiaStatErrorsIn
	print "errors_out.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.11.$pos"), "\n"; # gwiaStatErrorsOut

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};
	}
}

