# Now, run the SNMP command. my $response = undef; $response = $session->get_request($cmd); # Check for SNMP command error. if (!defined($response)) { #### #### # Just for informational purposes, not in my script this way. # Usually loaded in from a db and usually more than one structure. #### $snmpInfo = { node => '100', ip => '192.168.1.101', location => 'Somewhere, PA', snmpCmd => [ '.1.3.6.1.4.1.449.2.1.3.1.1.26.1.1.6.19', '.1.3.6.1.4.1.449.2.1.3.1.1.26.1.1.6.20' ], snmpExecDesc => [ 'Port 19', 'Port 20' ], snmpCmdResult => [ 'Disconnected', 'Disconnected' ], snmpCmdDesc => [ 'Checking Somewhere's port 19', 'Checking Somewhere's port 20' ] }; ### # From here on is my code, with some logging cut out to lower the post content. ### for my $cr (sort keys %{$snmpInfo}) { # Create SNMP Session. my ($session, $error) = Net::SNMP->session ( -hostname => "$snmpInfo->{$cr}->{'ip'}", -community => "$community", -timeout => $config->{'snmp_timeout'} ); # Check for SNMP Session Error. if (!defined($session)) { print $LOG "\n There was an error defining an SNMP session for "; print $LOG " $snmpInfo->{$cr}->{'ip'}.\n"; print $LOG " Error: $error\n"; print $LOG " Error Time: " . grab_time() . "\n\n"; # Update value to error. update_snmp_value($dbc, "Error - Check Log", $snmpInfo->{$cr}->{'node'}, '', 'node'); } else { # Loop through the snmp arrays and run the snmp commands. for my $x (0 .. $#{$snmpInfo->{$cr}->{'snmpCmd'}}) { my ($cmd, $snmpCmdResult, $execDesc) = ''; $cmd = $snmpInfo->{$cr}->{'snmpCmd'}[$x]; $snmpCmdResult = $snmpInfo->{$cr}->{'snmpCmdResult'}[$x]; $execDesc = $snmpInfo->{$cr}->{'snmpExecDesc'}[$x]; # Now, run the SNMP command. my $response = undef; $response = $session->get_request($cmd); # Check for SNMP command error. if (!defined($response)) { print $LOG " : **********************************************\n"; print $LOG " : There was an error running an SNMP Command for "; print $LOG "$snmpInfo->{$cr}->{'ip'}.\n"; print $LOG " : Error: " . $session->error() . "\n"; print $LOG " : Error Time: " . grab_time() . "\n"; print $LOG " : **********************************************\n"; # Update value to error. if ($snmpCmdResult ne "Error - Check Log") { update_snmp_value($dbc, "Error - Check Log", $snmpInfo->{$cr}->{'node'}, $cmd, 'cmd'); } } else { print $LOG " : Value - [$response->{$cmd}]\n" if ($logEnabled); # Check response, if not same as before, then update value. if ($response->{$cmd} ne $snmpCmdResult) { update_snmp_value($dbc, $response->{$cmd}, $snmpInfo->{$cr}->{'node'}, $cmd, 'cmd'); print $LOG " : Value different [$snmpCmdResult] - db updated.\n" if ($logEnabled); } } } } # Close the SNMP session. $session->close() if (defined($session)); $session = undef; }