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