#!/usr/bin/perl -w
# -*- perl -*-
#
# 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.
#
#
#%# family=snmpauto
#%# capabilities=snmpconf

=head2 PLUGIN CONFIGURATION

This plugin expects to be linked with the hostname and interface name
defined in the linkage, eg,

     ln -s /usr/share/munin/plugins/snmp__if_ \
       /etc/munin/plugins/snmp_router.host.domain_if_3

A port number can be defined in the linkage as well:

     ln -s /usr/share/munin/plugins/snmp__if_ \
       /etc/munin/plugins/snmp_router.host.domain:3922_if_3

SNMP configuration variables are passed in via environment
variables specified in $(MUNIN_PLUGIN_CONFIGDIR), eg,
/etc/munin/plugin-conf.d/snmp_router.host.domain_if_3

Valid SNMP configuration variables are:

env.host              Overrides hostname specified in linkage
env.port              Overrides port number specified in linkage
env.community         Specify the SNMP v1 or SNMP v2c community string.
env.snmpV3            Set to 1 to enable SNMP v3 auth connections
snmpV3_username               SNMPv3 username
snmpV3_authpassword   SNMPv3 auth password
snmpV3_authprotocol   SNMPv3 auth protocol
snmpV3_privpassword   SNMPv3 private password

=cut


use strict;
use Net::SNMP;

my $DEBUG = 0;

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

my $useMibEntryDescr = $ENV{useMibEntryDescr} || undef;

my ($snmpV3_username,     $snmpV3_authpassword,
    $snmpV3_authprotocol, $snmpV3_privpassword,);

if ($ENV{snmpV3}) {
  $snmpV3_username     = $ENV{snmpV3_username};
  $snmpV3_authpassword = $ENV{snmpV3_authpassword};
  $snmpV3_authprotocol = $ENV{snmpV3_authprotocol};
  $snmpV3_privpassword = $ENV{snmpV3_privpassword};
}

my $response;

# This is the snmpwalk:
# .1.3.6.1.2.1.2.1.0 = INTEGER: 2
# .1.3.6.1.2.1.2.2.1.1.1 = INTEGER: 1
# .1.3.6.1.2.1.2.2.1.1.65539 = INTEGER: 65539
# .1.3.6.1.2.1.2.2.1.2.1 = STRING: MS TCP Loopback interface
# .1.3.6.1.2.1.2.2.1.2.65539 = STRING: Broadcom NetXtreme Gigabit Ethernet
# .1.3.6.1.2.1.2.2.1.3.1 = INTEGER: softwareLoopback(24)
# .1.3.6.1.2.1.2.2.1.3.65539 = INTEGER: ethernetCsmacd(6)
# .1.3.6.1.2.1.2.2.1.4.1 = INTEGER: 1520
# .1.3.6.1.2.1.2.2.1.4.65539 = INTEGER: 1500
# .1.3.6.1.2.1.2.2.1.5.1 = Gauge32: 10000000
# .1.3.6.1.2.1.2.2.1.5.65539 = Gauge32: 1000000000
# .1.3.6.1.2.1.2.2.1.6.1 = STRING:
# .1.3.6.1.2.1.2.2.1.6.65539 = STRING: 0:30:48:75:65:5e
# .1.3.6.1.2.1.2.2.1.7.1 = INTEGER: up(1)
# .1.3.6.1.2.1.2.2.1.7.65539 = INTEGER: up(1)
# .1.3.6.1.2.1.2.2.1.8.1 = INTEGER: up(1)
# .1.3.6.1.2.1.2.2.1.8.65539 = INTEGER: up(1)

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|23)\$\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;
	$iface = $2;
	if ($host =~ /^([^:]+):(\d+)$/)
	{
		$host = $1;
		$port = $2;
	}
}
elsif (!defined($host))
{
	print "# Debug: $0 -- $1 -- $2\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 $ifEntryAlias     = "1.3.6.1.2.1.31.1.1.1.18.$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 $sysDescr         = '1.3.6.1.2.1.1.1.0';

my ($session, $error);

if ($ENV{snmpV3}) {
    ($session, $error) =
	Net::SNMP->session(
			   -hostname       => $host,
			   -version        => 'snmpv3',
			   -port           => $port,
			   -username       => $snmpV3_username,
			   -authpassword   => $snmpV3_authpassword,
			   -authprotocol   => $snmpV3_authprotocol,
			   -privpassword   => $snmpV3_privpassword,
			   );
} else {
   ($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" unless $host eq 'localhost';
	if (!defined ($response = $session->get_request($ifEntryDescr)))
	{
		die "Croaking: " . $session->error();
	}
	my $name = $response->{$ifEntryDescr};
	$name =~ s/[^\w\s]//g;
	my $alias = undef;
	if (defined ($response = $session->get_request($ifEntryAlias)))
	{
		$alias = $response->{$ifEntryAlias};
	}
	$alias = $alias || $name;
	my $warn = undef;
	if (defined ($response = $session->get_request($ifEntrySpeed))) {
	    $warn = $response->{$ifEntrySpeed}/8;
	}
	print "graph_title Interface $alias traffic\n";
	print "graph_order recv send\n";
	print "graph_args --base 1000\n";
	print "graph_vlabel bits in (-) / out (+) per \${graph_period}\n";
	print "graph_category network\n";
	print "graph_info This graph shows traffic for the \"$alias\" network interface.\n";
	print "send.info Bits sent/received by this interface.\n";
	print "recv.label recv\n";
	print "recv.type DERIVE\n";
	print "recv.graph no\n";
	print "recv.cdef recv,8,*\n";
	print "recv.max 2000000000\n";
	print "recv.min 0\n";
	print "recv.warn ", (-$warn), "\n" if defined $warn;
	print "send.label bps\n";
	print "send.type DERIVE\n";
	print "send.negative recv\n";
	print "send.cdef send,8,*\n";
	print "send.max 2000000000\n";
	print "send.min 0\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";
}
