| 1 |
#!./perl |
| 2 |
# |
| 3 |
# Test bulkwalk functionality. |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use Cwd qw(abs_path); |
| 8 |
use Test; |
| 9 |
|
| 10 |
BEGIN { plan test => ($^O =~ /win32/i) ? 43 : 64; } |
| 11 |
|
| 12 |
use SNMP; |
| 13 |
|
| 14 |
require "t/startagent.pl"; |
| 15 |
use vars qw($agent_host $agent_port $comm2); |
| 16 |
|
| 17 |
$SNMP::debugging = 0; |
| 18 |
$SNMP::verbose = 0; |
| 19 |
|
| 20 |
###################################################################### |
| 21 |
# Fire up a session. |
| 22 |
my $s1 = new SNMP::Session( |
| 23 |
'DestHost' => $agent_host, |
| 24 |
'Community' => $comm2, |
| 25 |
'RemotePort' => $agent_port, |
| 26 |
'Version' => '2c', |
| 27 |
'UseNumeric' => 1, |
| 28 |
'UseEnum' => 0, |
| 29 |
'UseLongNames' => 1 |
| 30 |
); |
| 31 |
ok(defined($s1)); |
| 32 |
|
| 33 |
###################################################################### |
| 34 |
# |
| 35 |
print("# Attempt to use the bulkwalk method to get a few variables from the SNMP agent.\n"); |
| 36 |
print("# test 1\n"); |
| 37 |
my $vars = new SNMP::VarList(['sysUpTime'], ['ifNumber'], # NON-repeaters |
| 38 |
['ifSpeed'], ['ifDescr']); # Repeated variables. |
| 39 |
|
| 40 |
my $expect = scalar @$vars; |
| 41 |
my @list = $s1->bulkwalk(2, 256, $vars); |
| 42 |
|
| 43 |
ok($s1->{ErrorNum} == 0); |
| 44 |
|
| 45 |
# Did we get back the list of references to returned values? |
| 46 |
# |
| 47 |
ok(scalar @list == $expect); |
| 48 |
if (defined($list[0][0])) { |
| 49 |
# Sanity check the returned values. list[0] is sysUptime nonrepeater. |
| 50 |
ok($list[0][0]->tag, ".1.3.6.1.2.1.1.3"); # check system.sysUptime OID |
| 51 |
ok($list[0][0]->iid, "0"); # check system.sysUptime.0 IID |
| 52 |
ok($list[0][0]->val =~ m/^\d+$/); # Uptime is numeric |
| 53 |
ok($list[0][0]->type, "TICKS"); # Uptime should be in ticks. |
| 54 |
} |
| 55 |
else { |
| 56 |
ok(0); |
| 57 |
ok(0); |
| 58 |
ok(0); |
| 59 |
ok(0); |
| 60 |
} |
| 61 |
my $ifaces = 0; |
| 62 |
if (defined($list[1][0])) { |
| 63 |
# Find out how many interfaces to expect. list[1] is ifNumber nonrepeater. |
| 64 |
ok($list[1][0]->tag, ".1.3.6.1.2.1.2.1"); # Should be system.ifNumber OID. |
| 65 |
ok($list[1][0]->iid, "0"); # system.ifNumber.0 IID. |
| 66 |
ok($list[1][0]->val =~ m/^\d+$/); # Number is all numeric |
| 67 |
#XXX: test fails due SMIv1 codes being returned intstead of SMIv2... |
| 68 |
#ok($list[1][0]->type, "INTEGER32"); # Number should be integer. |
| 69 |
|
| 70 |
$ifaces = $list[1][0]->val; |
| 71 |
} |
| 72 |
else { |
| 73 |
ok(0); |
| 74 |
ok(0); |
| 75 |
ok(0); |
| 76 |
} |
| 77 |
|
| 78 |
print("# Expecting $ifaces network interfaces.\n"); |
| 79 |
|
| 80 |
# Make sure we got an ifSpeed for each interface. list[2] is ifSpeed repeater. |
| 81 |
ok(scalar @{$list[2]} == $ifaces); |
| 82 |
# Make sure we got an ifDescr for each interface. list[3] is ifDescr repeater. |
| 83 |
ok(scalar @{$list[3]} == $ifaces); |
| 84 |
|
| 85 |
if (defined($list[2][0])) { |
| 86 |
# Test for reasonable values from the agent. |
| 87 |
ok($list[2][0]->tag, ".1.3.6.1.2.1.2.2.1.5"); # Should be system.ifSpeed OID. |
| 88 |
ok($list[2][0]->iid, "1"); # Instance should be 1. |
| 89 |
ok($list[2][0]->val =~ m/^\d+$/); # Number is all numeric |
| 90 |
ok($list[2][0]->type, "GAUGE"); # Number should be a gauge. |
| 91 |
} |
| 92 |
else { |
| 93 |
ok(0); |
| 94 |
ok(0); |
| 95 |
ok(0); |
| 96 |
ok(0); |
| 97 |
} |
| 98 |
|
| 99 |
print("# Looking up loopback network interface ...\n"); |
| 100 |
ok(ref($list[3]), 'SNMP::VarList'); |
| 101 |
my $found; |
| 102 |
for my $ifdescr (@{$list[3]}) { |
| 103 |
ok(ref($ifdescr), 'SNMP::Varbind'); |
| 104 |
print("# " . $ifdescr->val . "\n"); |
| 105 |
next if (!($ifdescr->val =~ /Software Loopback Interface/) and |
| 106 |
!($ifdescr->val =~ /^lo/)); |
| 107 |
$found = $ifdescr->val; |
| 108 |
ok(1); |
| 109 |
ok($ifdescr->tag, ".1.3.6.1.2.1.2.2.1.2"); # Should be system.ifDescr OID. |
| 110 |
ok($ifdescr->iid =~ m/^\d+$/); # Instance should be 1. |
| 111 |
ok($ifdescr->type, "OCTETSTR"); # Description is a string. |
| 112 |
last; |
| 113 |
} |
| 114 |
if (!$found) { |
| 115 |
ok(0); |
| 116 |
ok(0); |
| 117 |
ok(0); |
| 118 |
ok(0); |
| 119 |
ok(0); |
| 120 |
} |
| 121 |
|
| 122 |
############################################################################### |
| 123 |
print("# Attempt to use the bulkwalk method to get only non-repeaters.\n"); |
| 124 |
print("# test 2\n"); |
| 125 |
$vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'] ); # NON-repeaters |
| 126 |
|
| 127 |
$expect = scalar @$vars; |
| 128 |
@list = $s1->bulkwalk(2, 0, $vars); |
| 129 |
ok($s1->{ErrorNum} == 0); |
| 130 |
|
| 131 |
# Did we get back the list of references to returned values? |
| 132 |
# |
| 133 |
ok(scalar @list == $expect); |
| 134 |
|
| 135 |
if (defined($list[0][0])) { |
| 136 |
# Sanity check the returned values. list[0] is sysUptime nonrepeater. |
| 137 |
ok($list[0][0]->tag, ".1.3.6.1.2.1.1.3"); # check system.sysUptime OID |
| 138 |
ok($list[0][0]->iid, "0"); # check system.sysUptime.0 IID |
| 139 |
ok($list[0][0]->val =~ m/^\d+$/); # Uptime is numeric |
| 140 |
ok($list[0][0]->type, "TICKS"); # Uptime should be in ticks. |
| 141 |
} |
| 142 |
else { |
| 143 |
ok(0); |
| 144 |
ok(0); |
| 145 |
ok(0); |
| 146 |
ok(0); |
| 147 |
} |
| 148 |
|
| 149 |
if (defined($list[1][0])) { |
| 150 |
# Find out how many interfaces to expect. list[1] is ifNumber nonrepeater. |
| 151 |
ok($list[1][0]->tag, ".1.3.6.1.2.1.2.1"); # Should be system.ifNumber OID. |
| 152 |
ok($list[1][0]->iid, "0"); # system.ifNumber.0 IID. |
| 153 |
ok($list[1][0]->val =~ m/^\d+$/); # Number is all numeric |
| 154 |
#XXX: test fails due SMIv1 codes being returned intstead of SMIv2... |
| 155 |
#ok($list[1][0]->type, "INTEGER32"); # Number should be integer. |
| 156 |
$ifaces = $list[1][0]->val; |
| 157 |
} |
| 158 |
else { |
| 159 |
ok(0); |
| 160 |
ok(0); |
| 161 |
ok(0); |
| 162 |
} |
| 163 |
|
| 164 |
############################################################################### |
| 165 |
print("# Attempt to use the bulkwalk method to get only repeated variables\n"); |
| 166 |
print("# test 3\n"); |
| 167 |
$vars = new SNMP::VarList ( ['ifIndex'], ['ifSpeed'] ); # repeaters |
| 168 |
|
| 169 |
$expect = scalar @$vars; |
| 170 |
@list = $s1->bulkwalk(0, 256, $vars); |
| 171 |
ok($s1->{ErrorNum} == 0); |
| 172 |
|
| 173 |
# Did we get back the list of references to returned values? |
| 174 |
# |
| 175 |
ok(scalar @list == $expect); |
| 176 |
|
| 177 |
# Make sure we got an ifIndex for each interface. list[0] is ifIndex repeater. |
| 178 |
ok(scalar @{$list[0]} == $ifaces); |
| 179 |
|
| 180 |
# Make sure we got an ifSpeed for each interface. list[0] is ifSpeed repeater. |
| 181 |
ok(scalar @{$list[1]} == $ifaces); |
| 182 |
|
| 183 |
if (defined($list[0][0])) { |
| 184 |
# Test for reasonable values from the agent. |
| 185 |
ok($list[0][0]->tag, ".1.3.6.1.2.1.2.2.1.1"); # Should be system.ifIndex OID. |
| 186 |
ok($list[0][0]->iid, "1"); # Instance should be 1. |
| 187 |
ok($list[0][0]->val =~ m/^\d+$/); # Number is all numeric |
| 188 |
#XXX: test fails due SMIv1 codes being returned intstead of SMIv2... |
| 189 |
#ok($list[0][0]->type, "INTEGER32"); # Number should be an integer. |
| 190 |
} |
| 191 |
else { |
| 192 |
ok(0); |
| 193 |
ok(0); |
| 194 |
ok(0); |
| 195 |
} |
| 196 |
|
| 197 |
if (defined($list[1][0])) { |
| 198 |
ok($list[1][0]->tag, ".1.3.6.1.2.1.2.2.1.5"); # Should be system.ifSpeed OID. |
| 199 |
ok($list[1][0]->iid, "1"); # Instance should be 1. |
| 200 |
ok($list[1][0]->val =~ m/^\d+$/); # Number is all numeric |
| 201 |
ok($list[1][0]->type, "GAUGE"); # Number should be a gauge. |
| 202 |
} |
| 203 |
else { |
| 204 |
ok(0); |
| 205 |
ok(0); |
| 206 |
ok(0); |
| 207 |
ok(0); |
| 208 |
} |
| 209 |
|
| 210 |
###################################################################### |
| 211 |
# Asynchronous Bulkwalk Methods |
| 212 |
###################################################################### |
| 213 |
# |
| 214 |
print("# Attempt to use the bulkwalk method to get a few variables from the SNMP agent.\n"); |
| 215 |
print("# test 4\n"); |
| 216 |
sub async_cb1 { |
| 217 |
my ($vars, $list) = @_; |
| 218 |
ok(defined $list && ref($list) =~ m/ARRAY/); |
| 219 |
ok(defined $vars && ref($vars) =~ m/SNMP::VarList/); |
| 220 |
|
| 221 |
ok(scalar @$list == scalar @$vars); |
| 222 |
|
| 223 |
my $vbr; |
| 224 |
|
| 225 |
if (defined($list->[0][0])) { |
| 226 |
# Sanity check the returned values. First is sysUptime nonrepeater. |
| 227 |
$vbr = $list->[0][0]; |
| 228 |
ok($vbr->tag, ".1.3.6.1.2.1.1.3"); # check system.sysUptime OID |
| 229 |
ok($vbr->iid, "0"); # check system.sysUptime.0 IID |
| 230 |
ok($vbr->val =~ m/^\d+$/); # Uptime is numeric |
| 231 |
ok($vbr->type, "TICKS"); # Uptime should be in ticks. |
| 232 |
} |
| 233 |
else { |
| 234 |
ok(0); |
| 235 |
ok(0); |
| 236 |
ok(0); |
| 237 |
ok(0); |
| 238 |
} |
| 239 |
|
| 240 |
if (defined($list->[1][0])) { |
| 241 |
# Find out how many interfaces to expect. Next is ifNumber nonrepeater. |
| 242 |
$vbr = $list->[1][0]; |
| 243 |
ok($vbr->tag, ".1.3.6.1.2.1.2.1"); # Should be system.ifNumber OID. |
| 244 |
ok($vbr->iid, "0"); # system.ifNumber.0 IID. |
| 245 |
ok($vbr->val =~ m/^\d+$/); # Number is all numeric |
| 246 |
#XXX: test fails due SMIv1 codes being returned intstead of SMIv2... |
| 247 |
# ok($vbr->type, "INTEGER32"); # Number should be integer. |
| 248 |
$ifaces = $vbr->[2]; |
| 249 |
} |
| 250 |
else { |
| 251 |
ok(0); |
| 252 |
ok(0); |
| 253 |
ok(0); |
| 254 |
} |
| 255 |
|
| 256 |
# Test for reasonable values from the agent. |
| 257 |
ok(scalar @{$list->[2]} == $ifaces); |
| 258 |
|
| 259 |
if (defined($list->[2][0])) { |
| 260 |
$vbr = $list->[2][0]; |
| 261 |
ok($vbr->tag, ".1.3.6.1.2.1.2.2.1.5"); # Should be ifSpeed OID |
| 262 |
ok($vbr->iid, "1"); # Instance should be 1. |
| 263 |
ok($vbr->val =~ m/^\d+$/); # Number is all numeric |
| 264 |
ok($vbr->type, "GAUGE"); # Should be a gauge. |
| 265 |
|
| 266 |
ok(scalar @{$list->[3]} == $ifaces); |
| 267 |
} |
| 268 |
else { |
| 269 |
ok(0); |
| 270 |
ok(0); |
| 271 |
ok(0); |
| 272 |
ok(0); |
| 273 |
ok(0); |
| 274 |
} |
| 275 |
|
| 276 |
for my $ifdescr (@{$list->[3]}) { |
| 277 |
next if (!($ifdescr->val =~ /Software Loopback Interface/) and |
| 278 |
!($ifdescr->val =~ /^lo/)); |
| 279 |
ok(1); |
| 280 |
# Should be system.ifDescr OID. |
| 281 |
ok($ifdescr->tag, ".1.3.6.1.2.1.2.2.1.2"); |
| 282 |
ok($ifdescr->iid >= 1); # Instance should be >= 1. |
| 283 |
ok($ifdescr->type, "OCTETSTR"); # Description is a string. |
| 284 |
last; |
| 285 |
} |
| 286 |
if (!defined($list->[3][0])) { |
| 287 |
ok(0); |
| 288 |
ok(0); |
| 289 |
ok(0); |
| 290 |
ok(0); |
| 291 |
} |
| 292 |
|
| 293 |
SNMP::finish(); |
| 294 |
} |
| 295 |
|
| 296 |
$vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'], # NON-repeaters |
| 297 |
['ifSpeed'], ['ifDescr']); # Repeated variables. |
| 298 |
|
| 299 |
if ($^O =~ /win32/i) { |
| 300 |
warn "Win32/Win64 detected - skipping async calls\n"; |
| 301 |
} |
| 302 |
else { |
| 303 |
@list = $s1->bulkwalk(2, 256, $vars, [ \&async_cb1, $vars ] ); |
| 304 |
ok($s1->{ErrorNum} == 0); |
| 305 |
SNMP::MainLoop(); |
| 306 |
} |
| 307 |
ok(1); |
| 308 |
|
| 309 |
snmptest_cleanup(); |
| 310 |
|