3 # collectd - snmp-probe-host.px
4 # Copyright (C) 2008 Florian octo Forster
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; only version 2 of the License is applicable.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # Florian octo Forster <octo at noris.net>
26 use Config::General ('ParseConfig');
27 use Getopt::Long ('GetOptions');
35 %conf = ParseConfig (-ConfigFile => $file,
37 -UseApacheInclude => 1,
38 -IncludeDirectories => 1,
39 ($Config::General::VERSION >= 2.38) ? (-IncludeAgain => 0) : (),
40 -MergeDuplicateBlocks => 1,
57 if (!$conf->{'table'} || !$conf->{'values'})
59 warn "No 'table' or 'values' setting";
63 @oids = split (/"\s*"/, $conf->{'values'});
64 if ($conf->{'table'} =~ m/^(true|yes|on)$/i)
67 if (defined ($conf->{'instance'}))
69 push (@oids, $conf->{'instance'});
75 #print "probe_one: \@oids = (" . join (', ', @oids) . ");\n";
82 if ($oid_orig =~ m/[^0-9\.]/)
84 my $tmp = SNMP::translateObj ($oid_orig);
87 warn ("Cannot translate OID $oid_orig");
93 $vb = SNMP::Varbind->new ([$oid_orig]);
97 $status = $sess->get ($vb);
98 if ($sess->{'ErrorNum'} != 0)
102 if (!defined ($status))
106 if ("$status" eq 'NOSUCHOBJECT')
115 $status = $sess->getnext ($vb);
116 if ($sess->{'ErrorNum'} != 0)
121 $oid_copy = $vb->[0];
122 if ($oid_copy =~ m/[^0-9\.]/)
124 my $tmp = SNMP::translateObj ($oid_copy);
127 warn ("Cannot translate OID $oid_copy");
133 #print "$oid_orig > $oid_copy ?\n";
134 if (substr ($oid_copy, 0, length ($oid_orig)) ne $oid_orig)
140 #print STDOUT Data::Dumper->Dump ([$oid_orig, $status], [qw(oid_orig status)]);
149 my $community = shift;
159 @status = getaddrinfo ($host, 'snmp');
162 my $family = shift (@status);
163 my $socktype = shift (@status);
164 my $proto = shift (@status);
165 my $saddr = shift (@status);
166 my $canonname = shift (@status);
170 ($host, $port) = getnameinfo ($saddr, NI_NUMERICHOST);
177 warn ("getnameinfo failed: $host");
190 $sess = new SNMP::Session (DestHost => $host,
191 Community => $community,
206 if (probe_one ($sess, $data->{$name}))
208 push (@valid_data, $name);
211 if ((@valid_data == 0) && ((time () - $begin) > 10))
225 } # while ($version > 0)
236 Community "$community"
238 for (sort (@valid_data))
240 print " Collect \"$_\"\n";
251 Usage: snmp-probe-host.px --host <host> [options]
254 -H | --host Hostname of the device to probe.
255 -C | --config Path to config file holding the SNMP data blocks.
256 -c | --community SNMP community to use. Default: `public'.
257 -h | --help Print this information and exit.
265 snmp-probe-host.px - Find out what information an SNMP device provides.
269 ./snmp-probe-host.px --host switch01.mycompany.com --community ei2Acoum
273 The C<snmp-probe-host.px> script can be used to automatically generate SNMP
274 configuration snippets for collectd's snmp plugin (see L<collectd-snmp(5)>).
276 This script parses the collectd configuration and detecs all "data" blocks that
277 are defined for the SNMP plugin. It then queries the device specified on the
278 command line for all OIDs and registeres which OIDs could be answered correctly
279 and which resulted in an error. With that information the script figures out
280 which "data" blocks can be used with this hosts and prints an appropriate
281 "host" block to standard output.
283 The script first tries to contact the device via SNMPv2. If after ten seconds
284 no working "data" block has been found, it will try to downgrade to SNMPv1.
285 This is a bit a hack, but works for now.
290 my $file = '/etc/collectd/collectd.conf';
291 my $community = 'public';
297 The following command line options are accepted:
301 =item B<--host> I<hostname>
303 Hostname of the device. This B<should> be a fully qualified domain name (FQDN),
304 but anything the system can resolve to an IP address will word. B<Required
307 =item B<--config> I<config file>
309 Sets the name of the collectd config file which defined the SNMP "data" blocks.
310 Due to limitations of the config parser used in this script
311 (C<Config::General>), C<Include> statements cannot be parsed correctly.
312 Defaults to F</etc/collectd/collectd.conf>.
314 =item B<--community> I<community>
316 SNMP community to use. Should be pretty straight forward.
322 GetOptions ('H|host|hostname=s' => \$host,
323 'C|conf|config=s' => \$file,
324 'c|community=s' => \$community,
325 'h|help' => \&exit_usage) or die;
329 print STDERR "No hostname given. Please use `--host'.\n";
333 $conf = get_config ($file) or die ("Cannot read config");
335 if (!defined ($conf->{'plugin'})
336 || !defined ($conf->{'plugin'}{'snmp'})
337 || !defined ($conf->{'plugin'}{'snmp'}{'data'}))
339 print STDERR "Error: No <plugin>, <snmp>, or <data> block found.\n";
343 probe_all ($host, $community, $conf->{'plugin'}{'snmp'}{'data'});
353 C<Include> statements in the config file are not handled correctly.
357 SNMPv2 / SNMPv1 detection is a hack.
363 Copyright (c) 2008 by Florian octo Forster
364 E<lt>octoE<nbsp>atE<nbsp>noris.netE<gt>. Licensed under the terms of the GPLv2.
365 Written for the norisE<nbsp>networkE<nbsp>AG L<http://noris.net/>.
369 # vim: set sw=2 sts=2 ts=8 et :