#!/usr/bin/perl -w
#
# $Log: snmp__if__,v $
# Revision 1.1  2006/04/20 13:01:21  salles
# On regroupe les plugins munin dans un répertoire commun à toutes les machines
#
# Revision 1.1  2004/11/14 10:36:39  salles
# Ajout des plugins personnalisés
#
# Revision 1.2  2004/07/27 14:32:46  salles
# nom en premier pour qu'on les ait dans l'ordre alphabétique. mo²
#
# Revision 1.1  2004/07/27 12:17:28  bernat
# Et tournez manège !
#
# Revision 1.1  2004/07/27 11:37:16  bernat
# Deplacement (encore !)
#
# Revision 1.1  2004/07/27 11:27:43  bernat
# Deplacement (pour pas qu'il soit executé)
#
# Revision 1.1  2004/07/27 11:19:26  bernat
# SNMP un peu modifié pour avoir le nom des bats en "clair"
#
# Revision 1.4  2004/04/30 16:58:14  jimmyo
# Added max.
#
# Revision 1.3  2004/02/22 20:17:58  jimmyo
# Typo fix
#
# Revision 1.2  2004/02/18 21:54:56  jimmyo
# Did a bit of work on the snmp-thingie.
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.1  2003/12/19 20:53:45  jimmyo
# Created by jo
#
#

use strict;
use Net::SNMP;

my $DEBUG = 1;

my $host      = $ENV{host}      || undef;
my $port      = $ENV{port}      || 161;
my $community = $ENV{community} || "public";
my $iface     = $ENV{interface} || undef;
my $name      = undef;

my $response;

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
	print "number  1.3.6.1.2.1.2.1.0\n";
	print "index   1.3.6.1.2.1.2.2.1.1.\n";
	print "require 1.3.6.1.2.1.2.2.1.3. ^6\$\n"; # Type
	print "require 1.3.6.1.2.1.2.2.1.5. [1-9]\n"; # Speed
	exit 0;
}

if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_if_([^_]+)_(.+)$/)
{
    $host  = $1;
    $name = $2;
    $iface = $3;
    if ($host =~ /^([^:]+):(\d+)$/)
    {
        $host = $1;
        $port = $2;
    }
    if ($host !~ /\.adm\.crans\.org$/)
    {
        $host .= ".adm.crans.org";
    }

}
else
{
	print "# Debug: $0 -- $1 -- $3\n" if $DEBUG;
	die "# Error: couldn't understand what I'm supposed to monitor.";
}

my $ifEntryDescr     = "1.3.6.1.2.1.2.2.1.2.$iface"; 
my $ifEntrySpeed     = "1.3.6.1.2.1.2.2.1.5.$iface";
my $ifEntryStatus    = "1.3.6.1.2.1.2.2.1.8.$iface";
my $ifEntryInOctets  = "1.3.6.1.2.1.2.2.1.10.$iface";
my $ifEntryOutOctets = "1.3.6.1.2.1.2.2.1.16.$iface";

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

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

if ($ARGV[0] and $ARGV[0] eq "config")
{
	print "host_name $host\n";
#	if (!defined ($response = $session->get_request($ifEntryDescr)))
#	{
#		die "Croaking: " . $session->error();
#	}
#	my $name = $response->{$ifEntryDescr};
	my $warn = undef;
	if (defined ($response = $session->get_request($ifEntrySpeed)))
	{
		$warn = $response->{$ifEntrySpeed}/8;
	}
	print "graph_title $name traffic\n";
	print "graph_order recv send\n";
	print "graph_args --base 1000\n";
	print "graph_vlabel bps in (-) / out (+)\n";
	print "recv.label recv\n";
	print "recv.type COUNTER\n";
	print "recv.graph no\n";
	print "recv.cdef recv,8,*\n";
	print "recv.max 2000000000\n";
	print "recv.warn ", (-$warn), "\n" if defined $warn;
	print "send.label bps\n";
	print "send.type COUNTER\n";
	print "send.negative recv\n";
	print "send.cdef send,8,*\n";
	print "send.max 2000000000\n";
	print "send.warn $warn\n" if defined $warn;
	exit 0; 
}

my $status = 1;
if (defined ($response = $session->get_request($ifEntryStatus)))
{
	$status = $response->{$ifEntryStatus};
}

if ($status == 2)
{
	print "recv.value U\n";
	print "send.value U\n";
	exit 0;
}

if (defined ($response = $session->get_request($ifEntryInOctets)))
{
	print "recv.value ", $response->{$ifEntryInOctets}, "\n";
}
else
{
	print "recv.value U\n";
}

if (defined ($response = $session->get_request($ifEntryOutOctets)))
{
	print "send.value ", $response->{$ifEntryOutOctets}, "\n";
}
else
{
	print "send.value U\n";
}
