Perl Net::SNMP returns wrong value for some OIDs -
i'm trying data qlogic switch via snmp. when snmpwalk connunitportstatcounterror oid (.1.3.6.1.3.94.4.5.1.3) command line, hex string result each port on switch:
# snmpwalk -v1 -on -c <community> <host> .1.3.6.1.3.94.4.5.1.3 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.1 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.2 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.3 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.4 = hex-string: 00 00 00 00 00 00 00 01 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.5 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.6 = hex-string: 00 00 00 00 00 00 00 2c .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.7 = hex-string: 00 00 00 00 00 00 00 0e .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.8 = hex-string: 00 00 00 00 00 00 00 09 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.9 = hex-string: 00 00 00 00 00 00 00 38 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.10 = hex-string: 00 00 00 00 00 00 00 1c .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.11 = hex-string: 00 00 00 00 00 00 00 12 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.12 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.13 = hex-string: 00 00 00 00 00 00 00 0a .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.14 = hex-string: 00 00 00 00 00 00 00 21 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.15 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.16 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.17 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.18 = hex-string: 00 00 00 00 00 00 00 04 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.19 = hex-string: 00 00 00 00 00 00 00 08 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.20 = hex-string: 00 00 00 00 00 00 00 0a .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.21 = hex-string: 00 00 00 00 00 00 00 00 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.22 = hex-string: 00 00 00 00 00 00 00 00 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.23 = hex-string: 00 00 00 00 00 00 00 00 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.24 = hex-string: 00 00 00 00 00 00 00 00 but when call net::snmp's get_table() subroutine in perl script same oid, values of ports not match results got snmpwalk:
# ./foo <host> $var1 = { '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.1' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.2' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.3' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.4' => '0x0000000000000001', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.5' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.6' => ',', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.7' => '0x000000000000000e', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.8' => ' ', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.9' => '8', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.10' => '0x000000000000001c', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.11' => '0x0000000000000012', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.12' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.13' => '', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.14' => '!', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.15' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.16' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.17' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.18' => '0x0000000000000004', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.19' => ', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.20' => '', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.21' => '', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.22' => '', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.23' => '', '.1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.24' => '' }; notice results match until sixth port, snmpwalk returns '00 00 00 00 00 00 00 2c' , script returns ','. see similar behavior other oids connunitportstattable: results agree snmpwalk, few not. here perl script itself:
#!/usr/bin/env perl use strict; use warnings; use data::dumper; use net::snmp; # custom sort order oids. sorts on last number in oid because rest don't matter in example $data::dumper::sortkeys = sub { [ sort { (split '\.', $a)[-1] <=> (split '\.', $b)[-1] } keys %{$_[0]} ] }; $switch = shift; $port = 161; $community = <community>; # edited, ($session, $error) = net::snmp->session( -hostname => $switch, -port => $port, -timeout => 7, -retries => 2, -community => $community, ); if (!defined $session) { printf "error: connection failed\n", $error; exit 1; } $oid = '.1.3.6.1.3.94.4.5.1.3'; # connunitportstatcounterror $result = $session->get_table(-baseoid => $oid); if (!defined($result)) { printf "error: %s.\n", $session->error(); $session->close(); exit 1; } $session->close(); print dumper($result); exit 0; edit: perpetuity, here's program updated per perc's answer:
#!/usr/bin/env perl use strict; use warnings; use net::snmp; $switch = shift; $port = 161; $community = <community>; # edited, ($session, $error) = net::snmp->session( -hostname => $switch, -port => $port, -timeout => 7, -retries => 2, -community => $community, -translate => [-octetstring => 0], ); if (!defined $session) { printf "error: connection failed\n", $error; exit 1; } $oid = '.1.3.6.1.3.94.4.5.1.3'; # connunitportstatcounterror $result = $session->get_table(-baseoid => $oid); if (!defined($result)) { printf "error: %s.\n", $session->error(); $session->close(); exit 1; } $session->close(); foreach $key (sort { (split '\.', $a)[-1] <=> (split '\.', $b)[-1] } keys %$result) { printf "%s => %s\n", $key, unpack "h*", $result->{$key}; } exit 0; and output:
# ./foo <host> .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.1 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.2 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.3 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.4 => 0000000000000001 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.5 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.6 => 000000000000002c .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.7 => 000000000000000e .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.8 => 0000000000000009 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.9 => 0000000000000038 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.10 => 000000000000001c .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.11 => 0000000000000012 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.12 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.13 => 000000000000000a .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.14 => 0000000000000021 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.15 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.16 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.17 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.18 => 0000000000000004 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.19 => 0000000000000008 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.20 => 000000000000000a .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.21 => 0000000000000000 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.22 => 0000000000000000 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.23 => 0000000000000000 .1.3.6.1.3.94.4.5.1.3.16.0.0.192.221.18.202.254.0.0.0.0.0.0.0.0.24 => 0000000000000000
this because net::snmp tries translate octet_string human readable string.
see: https://metacpan.org/module/net::snmp#non-blocking-snmpv2c-get-bulk-request-for-iftable explanation , how work around this.
Comments
Post a Comment