#!/usr/bin/ruby

"
=head1 NAME

snmp__linksys_poe - Munin plugin to monitor the current supplied by Linksys PoE
switches.

Requires ruby and the ruby SNMP library.

=head1 APPLICABLE SYSTEMS

I wrote this to query SRW2008MP switches and determined the OIDs by trial and
error.  There may be other Linksys devices that this will also work for.

=head1 CONFIGURATION

This plugin defaults to SNMP version 2c and a community string of 'public'.  The
defaults can be overridden in the usual way:

    [snmp_*]
        env.version 1
        env.community private

SNMP version 3 is not supported.

=head1 INTERPRETATION

The plugin simply reports the current being supplied on each of the device's
PoE ports.

=head1 MIB INFORMATION

Information is gathered from Linksys' private MIB space, so it's probably only
applicable to Linksys devices.  I have been unable to get an actual copy of
the appropriate MIB, so I don't know the actual names of the values I'm 
retrieving.

=head1 MAGIC MARKERS

  #%# family=snmpauto contrib
  #%# capabilities=snmpconf

=head1 VERSION

  1.0

=head1 BUGS

None known.

=head1 AUTHOR

Written by Phil Gold <phil_g@pobox.com>.

=head1 LICENSE

CC0 <http://creativecommons.org/publicdomain/zero/1.0/>

To the extent possible under law, all copyright and related or neighboring
rights to this plugin are waived.  Do with it as you wish.

=cut
"

require 'snmp'

idx_oid = "enterprises.3955.89.108.1.1.2"
max_oid = "enterprises.3955.89.108.1.1.6"
cur_oid = "enterprises.3955.89.108.1.1.5"

community = ENV['community'] || "public"
version = ENV['version'] == '1' ? :SNMPv1 : :SNMPv2c

case ARGV[0]
when "autoconf"
  puts "no"
  exit 1
when "snmpconf"
  puts "require 1.3.6.1.4.1.3955.89.108.1.1.2.1. [0-9]"
  puts "require 1.3.6.1.4.1.3955.89.108.1.1.5.1. [0-9]"
  puts "require 1.3.6.1.4.1.3955.89.108.1.1.6.1. [0-9]"
  exit 0;
when "config"
  host = $0.match('^(?:|.*\/)snmp_([^_]+)')[1]
  puts "host_name #{host}"
  puts "graph_title PoE Power Usage"
  puts "graph_vlabel Watts"
  puts "graph_category sensors"
  max_current = 0
  SNMP::Manager.open(:Host => host,
                     :Community => community,
                     :Version => version) do |manager|
    manager.walk([idx_oid, max_oid]) do |row|
      puts "iface_#{row[0].value}.label Port #{row[0].value}"
      puts "iface_#{row[0].value}.cdef iface_#{row[0].value},1000,/"
      puts "iface_#{row[0].value}.line #{row[1].value.to_f / 1000}"
      if row[1].value > max_current
        max_current = row[1].value
      end
    end
  end
  puts "graph_args --upper-limit #{max_current.to_f / 1000}"
  exit 0
else
  host = $0.match('^(?:|.*\/)snmp_([^_]+)')[1]
  SNMP::Manager.open(:Host => host,
                     :Community => community,
                     :Version => version) do |manager|
    manager.walk([idx_oid, cur_oid]) do |row|
      puts "iface_#{row[0].value}.value #{row[1].value}"
    end
  end
end
