#!/usr/bin/perl -w
#
# $Log: snmp__fc_if_err_.in,v $
# Revision 1.1  2004/04/30 22:22:07  jimmyo
# Added to SNMP based fibre-channel plugins: fc_if and fc_if_err.
#
# 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 $response;

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
	print "number  1.3.6.1.2.1.8888.1.1.3.1.4.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0\n";
	print "index   1.3.6.1.2.1.8888.1.1.6.1.1.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0.\n";
	print "require 1.3.6.1.2.1.8888.1.1.6.1.5.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0. ^2\$\n"; # state = online
	print "require 1.3.6.1.2.1.8888.1.1.6.1.14.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0. [1-9]\n"; # Speed
	exit 0;
}

if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_fc_if_err_(.+)$/)
{
	$host  = $1;
	$iface = $2;
	if ($host =~ /^([^:]+):(\d+)$/)
	{
		$host = $1;
		$port = $2;
	}
}
else
{
	print "# Debug: $0 -- $1 -- $2\n" if $DEBUG;
	die "# Error: couldn't understand what I'm supposed to monitor.";
}

my $fcEntryDescr     = "1.3.6.1.2.1.8888.1.1.6.1.16.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0.$iface"; 
my $fcEntrySpeed     = "1.3.6.1.2.1.8888.1.1.6.1.14.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0.$iface";
my $fcEntryStatus    = "1.3.6.1.2.1.8888.1.1.6.1.6.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0.$iface";
my $fcEntryErrs      = "1.3.6.1.2.1.8888.1.3.1.1.2.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0.$iface";
my $fcPhysPort       = "1.3.6.1.2.1.8888.1.1.6.1.17.16.0.8.0.136.3.52.64.0.0.0.0.0.0.0.0.$iface";

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

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($fcEntryDescr)))
	{
		die "Croaking: " . $session->error();
	}
	my $name = $response->{$fcEntryDescr};
	if (defined ($response = $session->get_request($fcPhysPort)))
	{
		$name .= " (" . $response->{$fcPhysPort} . ")";
	}
	print "graph_title FC $name errors\n";
	print "graph_order errs\n";
	print "graph_args --base 1000\n";
	print "graph_vlabel errors / second\n";
	print "errs.label errors\n";
	print "errs.type COUNTER\n";
	print "errs.max 4000000000\n";
	exit 0; 
}

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

if ($status != 3)
{
	print "errs.value U\n";
	exit 0;
}

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

