#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Carp; use Parse::CSV; use SNMP; use SNMP::Effective; #--------------------------------------------------------- my $csv_file = shift || die "Please specify a CSV file with SNMP host info!"; my @reqired_fields = qw(HOSTIP COMMUNITY SNMPVER SNMPPORT); my @hosts = read_hosts_csv($csv_file, @reqired_fields); # This object encapsulates the desired queries to run. my $query = SNMP::Effective->new( max_sessions => 50, #master_timeout => $TIMEOUT_SECONDS, ); my @results; foreach my $host (@hosts) { my @varbinds = qw( ifDescr ifInOctets ifOutOctets ifAlias ifType ifName ifInErrors ifOutErrors ifSpeed ifAdminStatus ifOperStatus ); my $callback = sub { my $host_obj = shift; # SNMP::Effective::Host object my $error = shift; # Error message. if ($error) { warn $error; return; } push @results, [ $host_obj->address(), $host_obj->data() ]; }; $query->add( dest_host => $host->{HOSTIP}, callback => $callback, walk => [ @varbinds ], arg => { Community => $host->{COMMUNITY}, Version => $host->{SNMPVER}, RemotePort => $host->{SNMPPORT}, }, ); } # Run all the added queries with up to X # asynchronous operations in-flight at any time. foreach my $iter (1..1) { sleep 30 unless $iter == 1; $query->execute(); print Dumper \@results; } exit; #--------------------------------------------------------- # Read in the CSV file. sub read_hosts_csv { my $file = shift; my @required_fields = @_; # Parse entries from a CSV file into hashes hash my $csv_parser = Parse::CSV->new( file => $file, fields => 'auto', # Use the first line as column headers, # which become the hash keys. ); my @node_cfg; # Return a reference to this my $line_num = 0; while ( my $line = $csv_parser->fetch() ) { $line_num++; my $error_flag = 0; foreach my $field (@required_fields) { if ( ! exists $line->{$field} ) { $error_flag = 1; carp "Missing field [$field] on line [$line_num] in CSV file [$file]"; } } croak "Terminating due to errors on line [$line_num] in CSV file [$file]" if $error_flag; push @node_cfg, $line; } if ( $csv_parser->errstr() ) { croak "Fatal error parsing [$file]: " . $csv_parser->errstr(); } return @node_cfg; } 1; __END__