#!/usr/bin/perl -w
#
# File: snmp__gwia_bytes__
# Copyright (C) 2007 Gabriele Pohl
#
# 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.
# * gwiaStatBytesIn - 1.3.6.1.4.1.23.2.70.1.6.
# * gwiaStatBytesOut - 1.3.6.1.4.1.23.2.70.1.5.
#
# Usage:
# --------------
# Link this file snmp__gwia_bytes_ to your nodes servicedir [/etc/munin/plugins] 
#
# as: 
#   snmp_<host>_gwia_bytes_<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_bytes_ \
#    /etc/munin/plugins/snmp_foo.example.com_gwia_bytes_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_bytes_*]
#   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_example.com_gwia_bytes_1]
#   env.port 166
#   env.community example
#
# $Log$
# Revision 1.0	2007/09/06 16:49	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;

my $GRAPH_CATEGORY = "Groupwise";
my $GRAPH_PERIOD = "minute";
my $GRAPH_VLABEL = "bytes per $GRAPH_PERIOD in(-) / out(+)";
my $BYTES_LABEL='Bytes';

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.6. [\\d*]\n"; # gwiaStatBytesIn
	print "require 1.3.6.1.4.1.23.2.70.1.5. [\\d*]\n"; # gwiaStatBytesOut

	exit 0;
}

if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_gwia_bytes_(\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 $GRAPH_CATEGORY
graph_args --base 1000
graph_period $GRAPH_PERIOD
graph_title GWIA byte 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.<br />The plugin fetches the following values (GWIAMIB):<br />gwiaStatBytesOut - Size of outgoing messages in bytes.<br />gwiaStatBytesIn - Size of incoming messages in bytes.
graph_vlabel $GRAPH_VLABEL
bytes_in.label $BYTES_LABEL
bytes_in.info gwiaStatBytesIn (1.3.6.1.4.1.23.2.70.1.6.)
bytes_in.type DERIVE
bytes_in.min 0
bytes_in.draw AREA
bytes_in.graph no
bytes_out.label $BYTES_LABEL
bytes_out.info gwiaStatBytesOut (1.3.1.4.1.23.2.70.1.5.)
bytes_out.type DERIVE
bytes_out.min 0
bytes_out.draw AREA
bytes_out.negative bytes_in";

	exit 0;
}
	# fetch the data and print
	print "bytes_in.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.6.$pos"), "\n"; # gwiaStatBytesIn
	print "bytes_out.value ", &get_single ($session, "1.3.6.1.4.1.23.2.70.1.5.$pos"), "\n"; # gwiaStatBytesOut


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

