($host, $port, $version, $tail) = Munin::Plugin::SNMP->config_session();
This is a convenience function for the ``config'' part of the plugin - it decodes the environment/plugin name to retrieve the information needed in the configuration phase. It returns a 4 tuple consisting of:
The tail can be interesting for the ``fetch'' part of the plugin as well.
$session = Munin::Plugin::SNMP->session();
This method overrides the Net::SNMP constructor to get the connection information from the plugin name and/or environment. Please note that no error string is returned. The function handles errors internally - giving a error message and calling die. Calling die is the right thing to do.
The host name is taken from the plugin symlink, which must be on the form "snmp[v3]_<hostname>_<plugin_name>[_args]".
The ``v3'' form is taken to mean that SNMPv3 is to be used. It is also a name trick providing a separate ``namespace'' for devices that use SNMPv3 so it can be configured separately in munin/plugin-conf.d/ files. E.g.:
[snmp_*] env.version 2 env.community public [snmpv3_*] env.v3username snmpoperator env.v3authpassword s3cr1tpa55w0rd
See below for how to configure for each different case. The first case above shows Munin's default configuration.
NOTE: munin-node-configure does not yet utilize the ``v3'' thing.
The following environment variables are consulted:
The host name must be specified, but is usually specified in the plugin name. If the hostname somehow does not resolve in DNS (or the hosts file) it is possible to do this:
[snmp_*] env.version 2c env.community floppa [snmp_switch1.langfeldt.net] env.host 192.168.2.45 [snmp_switch2.langfeldt.net] env.host 192.168.2.46
Security is handled differently for versions 1/2c and 3. See below.
Note: Encryption can slow down slow or heavily loaded network devices. For most uses "authNoPriv" will be secure enough --- the password is sent over the network encrypted in any case.
"Munin::Plugin::SNMP" does not support ContextEngineIDs and such for authentication/privacy. If you see the need and know how it should be done please send patches!
For further reading on SNMP v3 security models please consult RFC3414 and the documentation for Net::SNMP.
If version is set to 3 or snmpv3 the following variables are used to define authentication:
Privacy requires a v3privprotocol as well as a v3authprotocol and a v3authpassword, but all of these are defaulted (to 'des', 'md5', and the v3privpassword value, respectively) and may therefore be left unspecified.
The implementing perl module (Net::SNMP) also supports '3des' (CBC-3DES-EDE aka Triple-DES, NIST FIPS 46-3) as specified in IETF draft-reeder-snmpv3-usm-3desede. Whether or not this works with any particular device, we do not know.
$result = $session->get_hash( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -baseoid => $oid, -cols => \%columns );
This method transforms the -baseoid and -cols to a array of -columns and calls "get_entries()" with all the other arguments. It then transforms the data into a hash of hashes in the following manner:
The keys of the main hash are the last element(s) of the OIDs, after $oid and the matching keys from %columns are removed. The values are hashes with keys corresponding to the values of %columns hash and values from the subtables corresponding to the keys of %columns.
For this to work, all the keys of "-cols" must have the same number of elements. Also, don't try to specify a next-to-next-to-leaf-node baseoid, the principle it breaks both "get_entries" and the logic in "get_hash".
If (all) the OIDs are unavailable a defined but empty hashref is returned.
Example:
$session->get_hash( -baseoid => '1.3.6.1.2.1.2.2.1', # IF-MIB -cols => { 1 => 'index', 2 => 'descr', 4 => 'mtu', } );
given the following SNMP table:
IF-MIB::ifIndex.1 = INTEGER: 1 IF-MIB::ifIndex.2 = INTEGER: 2 IF-MIB::ifDescr.1 = STRING: lo0 IF-MIB::ifDescr.2 = STRING: lna0 IF-MIB::ifType.1 = INTEGER: softwareLoopback(24) IF-MIB::ifType.2 = INTEGER: ethernetCsmacd(6) IF-MIB::ifMtu.1 = INTEGER: 32768 IF-MIB::ifMtu.2 = INTEGER: 1500 ...
will return a hash like this:
'1' => { 'index' => '1', 'mtu' => '32768', 'descr' => 'lo0' }, '2' => { 'index' => '2', 'descr' => 'lna0', 'mtu' => '1500' }
$uptime = $session->get_single("1.3.6.1.2.1.1.3.0") || 'U';
If the call fails to get a value the above call sets $uptime to 'U' which Munin interprets as ``Undefined'' and handles accordingly.
If you stop to think about it you should probably use "get_hash()" (it gets too much, but is good for arrays) or "get_entries()" - it gets exactly what you want, so you mus
my $tcpConnState = "1.3.6.1.2.1.6.13.1.1."; my $connections = $session->get_by_regex($tcpConnState, "[1-9]");
It gets all OIDs based at $tcpConnState and only returns the ones that contain a number in the value.
Nicolai Langfeldt: Actually I think it does.
All rights reserved. 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.