Skip to content

Instantly share code, notes, and snippets.

@thowe
Created November 1, 2017 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thowe/e961f8c8f2d67465cf41a406b9b5f6c7 to your computer and use it in GitHub Desktop.
Save thowe/e961f8c8f2d67465cf41a406b9b5f6c7 to your computer and use it in GitHub Desktop.
find interfaces and output Nagios config
#!/usr/bin/env perl
#
use 5.010;
use strict;
use warnings;
use Net::SNMP;
use Getopt::Long;
use List::MoreUtils qw(firstidx);
my $ifIndexOID = '1.3.6.1.2.1.2.2.1.1';
my $ifAliasOID = '1.3.6.1.2.1.31.1.1.1.18';
my $ifDescOID = '1.3.6.1.2.1.2.2.1.2';
my $ifNameOID = '1.3.6.1.2.1.31.1.1.1.1';
my $ifTypeOID = '1.3.6.1.2.1.2.2.1.3';
my $ifAdminStatusOID = '1.3.6.1.2.1.2.2.1.7';
# the indexes of the interfaces we are dealing with
my @indexes;
my $hostname;
my $community;
my @types; # interface types we want
my $data_res;
# the nagios service template to use
my $template = 'generic-service';
my $description;
# Print information on correct command line usage.
sub usage {
my $instructions = <<'END';
USAGE:
get_interfaces.pl --community 'community string' --type ifType [--type ifType]
-- host
The host can be an ip address or fully qualified domain name of the device
to be queried. The community string is the (v1) SNMP community string passed
to Net-SNMP for authentication on the specified host. At least one interface
type must be specified. These are the ifTypes specified by the
AIANifType-MIB found here: http://www.iana.org/assignments/ianaiftype-mib).
Common ifTypes are 6 (for all ethernet-like interfaces), 49 (AAL5 over ATM),
or 18 (ds1).
Interfaces that are administratively down or that lack a description are
ignored.
END
say $instructions;
exit;
}
# Assign settings from command line options.
sub set_options {
GetOptions('community=s' => \$community,
'type=i' => \@types);
if ($ARGV[0] && $community && @types){
$hostname = $ARGV[0];
}
else {
usage();
}
return;
}
set_options();
my ($session, $error) = Net::SNMP->session(
-hostname => $hostname,
-community => $community,
-nonblocking => 0,);
# we get all the interface indexes.
my $ifIndex_res = $session->get_table(
-baseoid => $ifIndexOID);
# We then put the indexes into an array.
foreach my $oid ( sort { $ifIndex_res->{$a} <=> $ifIndex_res->{$b} }
keys(%{$ifIndex_res}) ) {
push(@indexes, $ifIndex_res->{$oid});
}
# That array is gone through to grab the info for each interface
# and we build a config from the data.
for my $i (@indexes) {
$data_res = $session->get_request(
-varbindlist => [ "$ifAliasOID.$i",
"$ifNameOID.$i",
"$ifTypeOID.$i",
"$ifAdminStatusOID.$i" ] );
# skip if not part
next if 0 > firstidx { $_ == $data_res->{"$ifTypeOID.$i"} } @types;
# skip if no description
next if ! $data_res->{"$ifAliasOID.$i"};
# If the interface is adminstratively down
next if 1 < $data_res->{"$ifAdminStatusOID.$i"};
#say $data_res->{"$ifAliasOID.$i"};
# If the description begins with "NOMON" skip it
next if $data_res->{"$ifAliasOID.$i"} =~ /^NOMON/;
$description = $data_res->{"$ifAliasOID.$i"};
# convert comma to colon in description
$description =~ s/\,/:/g;
# short list of valid characters
$description =~ s/[^:\w\s\-\/\.]//g;
# the Nagios configuration block.
my $ncblock = "define service {\n" .
" use $template;\n" .
" host_name $hostname;\n" .
' service_description ' . $data_res->{"$ifNameOID.$i"} .
" $description" . ";\n" .
"check_command check_snmp!-C $community -o ifOperStatus.$i " .
"-c 1 -m RFC1213-MIB;\n" .
'}';
say $ncblock;
}
# no reason we shouldn't be neat
$session->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment