1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
# -*- mode: perl -*-
#
# Copyright (C) 2004 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.
#
package Munin::Plugin::SNMP;
use Net::SNMP;
@ISA = qw(Net::SNMP);
# $Id: SNMP.pm.in 860 2005-03-29 20:32:59Z ilmari $
=head1 NAME
Munin::Plugin::SNMP - Net::SNMP subclass for Munin plugins
=head1 SYNOPSIS
The Munin::Plugin::SNMP module extends Net::SNMP with methods useful for
Munin plugins.
=head1 METHODS
=head2 session() - create new Munin::Plugin::SNMP object
($session, $error) = Munin::Plugin::SNMP->session();
This method overrides the Net::SNMP constructor to get the connection
info from the plugin name and/or environment. The hostname is taken from
the plugin symlink, which must be on the form
C<snmp_E<lt>hostnameE<gt>_E<lt>plugin_nameE<gt>[_args]>. The following
environment variables are consulted:
=over
=item host
Used as the hostname to connect to (and output from the plugin) if the
symlink does not contain a host name. If neither are set, an error is
returned.
=item port
The port to connect to. Default 161.
=item community
The community name. Default public.
=item version
The SNMP version to use for the connection. Default auto.
=back
=cut
sub session {
my $class = shift;
my $host = $ENV{host} || undef;
my $port = $ENV{port} || 161;
my $community = $ENV{community} || 'public';
my $version = $ENV{version} || undef;
if ($0 =~ /^(?:.*\/)?snmp_([^_]+)_/) {
$host = $1;
if ($host =~ /^([^:]+):(\d+)$/) {
$host = $1;
$port = $2;
}
} elsif (!defined($host)) {
return unless wantarray; # Scalar or void context
return (undef, 'Couldn not find monitoring target.'); # Array context
}
return $class->SUPER::session(-hostname => $host,
-community => $community,
-port => $port,
($version ? (-version => $version) : ()));
}
=head2 get_hash() - retrieve a table as a hash of hashes
$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 calls C<get_table()> with all the given arguments except
C<-cols>, and 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
C<$oid> and the matching keys from C<%columns> are removed. The values
are hashes with keys corresponding to the values of C<%columns> hash and
values from the subtables corresonding to the keys of C<%columns>.
For this to work, all the keys of C<-cols> must have the same number of
elements.
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'
}
=cut
sub get_hash {
my $self = shift;
my %args = @_;
my %ret;
my $base = $args{'-baseoid'};
my $cols = delete $args{'-cols'} or return;
my $table = $self->get_table(%args)
or return;
my $subtabs = join '|', keys %$cols;
my $re = qr/^\Q$base.\E($subtabs)\.(.*)/;
for my $key (keys %$table) {
$key =~ $re;
next unless defined($1 && $2);
$ret{$2}{$cols->{$1}} = $table->{$key};
}
return \%ret;
}
=head1 TODO
Lots.
=head1 BUGS
C<get_hash()> doesn't handle tables with sparse indices.
=back
=head1 SEE ALSO
L<Net::SNMP>
=head1 AUTHOR
Dagfinn Ilmari Mannsker E<lt>ilmari@linpro.noE<gt>
=head1 COPYRIGHT
Copyright (c) 2004 Dagfinn Ilmari Mannsker E<lt>ilmari@linpro.noE<gt>.
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.
=cut
|