#!@@PERL@@ -w
# -*- perl -*-
# vim: ft=perl

=head1 NAME

snmp__cpuload - Munin plugin to monitor CPU usage by use of SNMP.

=head1 APPLICABLE SYSTEMS

Any SNMP-capable device that reports the
HOST-RESOURCES-MIB::hrProcessorLoad OID.

=head1 CONFIGURATION

As a rule SNMP plugins need site specific configuration.  The default
configuration (shown here) will only work on insecure sites/devices.

   [snmp_*]
	env.version 2
        env.community public

In general SNMP is not very secure at all unless you use SNMP version
3 which supports authentication and privacy (encryption).  But in any
case the community string for your devices should not be "public".

Please see 'perldoc Munin::Plugin::SNMP' for further configuration
information.

=head1 INTERPRETATION

The average, over the last minute, of the percentage of time that
this processor was not idle.

=head1 MIB INFORMATION

This plugin requires support for the HOST-RESOURCES-MIB (RFC 2790).  It
reports the contents of the hrProcessorLoad OID for each entry in
hrProcessorTable.

=head1 MAGIC MARKERS

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

=head1 BUGS

None known.

=head1 AUTHOR

Copyright (C) 2006 Lars Strand

Documented and updated to use Munin::Plugin::SNMP by Matthew Boyle.

=head1 LICENSE

GPLv2.

=cut

use strict;
use Munin::Plugin::SNMP;

my $oid = '1.3.6.1.2.1.25.3.3.1.2';

if (defined $ARGV[0] and $ARGV[0] eq 'snmpconf') {
	print "require ${oid}.\n";
	exit 0;
}

my $session = Munin::Plugin::SNMP->session();

my $result = $session->get_entries(-columns => [ $oid ]);

my %cpus;

while (my ($oid, $value) = each %$result) {
	my $index = (split /\./, $oid)[-1];
	$cpus{$index} = $value;
}

if (defined $ARGV[0] and $ARGV[0] eq "config") {
	my $host = $session->hostname;

	print "host_name $host\n" unless ($host eq 'localhost');
	print <<'EOC';
graph_title CPU usage
graph_category system
graph_args --upper-limit 100 -l 0
graph_vlabel %
graph_info This graph shows the CPU load on the system.
EOC
	foreach my $cpu (sort(keys %cpus)) {
		# CPU #1 will have $fieldname "cpu" to retain compatability with
		# old snmp_cpuload plugin.
		my $cpun = $cpu;
		$cpun = '' if $cpun == 1;
		print "cpu$cpun.label CPU $cpu\n";
		print "cpu$cpun.info CPU load on CPU $cpu\n";
	}
	exit 0;
}

# the values
while (my ($cpu, $load) = each(%cpus)) {
	$cpu = '' if $cpu == 1;
	print "cpu$cpu.value $load\n";
}

