/usr/lib/x86_64-linux-gnu/perl5/5.24/NetSNMP/agent/Support.pm is in libsnmp-perl 5.7.3+dfsg-1.7+deb9u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | ####################################################################
#
# SnmpAgent module for use with the code generated by mib2c with the
# embedded perl configuration file mib2c.perl.conf
#
# Copyright Tripleplay Services Limited 2005
# All rights reserved.
#
# Use is subject to license terms specified in the COPYING file
# distributed with the Net-SNMP package.
#
####################################################################
package NetSNMP::agent::Support;
require Exporter;
use NetSNMP::OID (':all');
use NetSNMP::agent (':all');
use NetSNMP::ASN (':all');
use NetSNMP::default_store (':all');
use Data::Dumper;
use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
@ISA = qw(Exporter getLeaf);
@EXPORT = qw(registerAgent getOidElement setOidElement);
@EXPORT_OK = qw();
$VERSION = '5.0703';
use strict;
use warnings;
BEGIN {
print STDERR "Module SnmpAgent.pm loaded ok\n";
}
# set to 1 to get extra debugging information
my $debugging = 0;
# if we're not embedded, this will get auto-set below to 1
my $subagent = 0;
################################################################
# The oidtable is used for rapid random access to elements with known
# oids, such as when doing gets/sets. It is not so good for
# ordered access. For that we build a tree (see below) and setup
# next links in the oidtable to aid in evaluating where to go next
# for GETNEXT requests.
################################################################
my $oidtable;
my $oidtree;
# oidroot holds the top most oid record in the table.
my $oidroot = "";
################################################################
# Build a tree for handling getnext requests
# Some parts borrowed from the perl cookbook
################################################################
sub buildTree {
my ($new_oidtable) = @_;
foreach my $key (keys %$new_oidtable) {
insert($oidtree, $key);
}
}
sub printTree {
my ($tree) = @_;
return unless $tree;
printTree($tree->{LEFT});
print $tree->{VALUE}. "\n";
printTree($tree->{RIGHT});
}
my $prev="";
##############################################################
# Walk the sorted oid tree and set the 'next' links in the
# oidtable.
##############################################################
sub doLinks {
my ($tree) = @_;
return unless $tree;
doLinks($tree->{LEFT});
if($oidroot eq "") {
$oidroot = $tree->{VALUE};
# print "Setting oidroot to $oidroot\n";
}
if($prev ne "") {
$oidtable->{$prev}->{next} = $tree->{VALUE};
}
$prev = $tree->{VALUE};
my $node = $oidtable->{$tree->{VALUE}};
doLinks($tree->{RIGHT});
}
################################################################
# Insert a node into the tree
################################################################
sub insert {
my ($tree, $value) = @_;
unless ($tree) {
$tree = {}; # Allocate memory
$tree->{VALUE} = $value;
$tree->{LEFT} = undef;
$tree->{RIGHT} = undef;
$_[0] = $tree;
return ;
}
my $tv = new NetSNMP::OID($tree->{VALUE});
my $mv = new NetSNMP::OID($value);
if ($tv > $mv) {
insert($tree->{LEFT}, $value);
} elsif ($tv < $mv) {
insert($tree->{RIGHT}, $value);
} else {
print "ERR: Duplicate insert of $value\n";
}
}
#################################################################
## Main interface.
## registerAgent(oid);
#################################################################
sub registerAgent {
my $agent = shift;
my $regat = shift;
my $new_oidtable = shift;
foreach (keys %$new_oidtable) { $oidtable->{$_} = $new_oidtable->{$_}; }
# This is necessary to avoid problems traversing trees where the
# IID index is range-limited to not include .0, which is used as a
# placeholder in the oidtable.
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_DONT_CHECK_RANGE, 1);
print STDERR "Building OID tree\n";
buildTree($new_oidtable);
doLinks($oidtree);
# print Dumper($oidtable);
# Debug. Print the list of oids in their next ordering
my $node = $oidroot;
while($node) {
# print $node . "\n";
$node = $oidtable->{$node}->{next};
}
# where we are going to hook onto
my $regoid = new NetSNMP::OID($regat);
print "registering at ",$regoid,"\n" if ($debugging);
# If we're not running embedded within the agent, then try to start
# our own subagent instead.
if (! $agent) {
$agent = new NetSNMP::agent('Name' => 'test', # reads test.conf
'AgentX' => 1); # make us a subagent
$subagent = 1;
print STDERR "started us as a subagent ($agent)\n"
}
# we register ourselves with the master agent we're embedded in. The
# global $agent variable is how we do this:
print Dumper($agent) if ($debugging);
$agent->register('myname',$regoid, \&my_snmp_handler);
}
######################################################################
# The subroutine to handle the incoming requests to our
# part of the OID tree. This subroutine will get called for all
# requests within the OID space under the registration oid made above.
######################################################################
sub my_snmp_handler {
my ($handler, $registration_info, $request_info, $requests) = @_;
my $request;
my $reqnum=1;
# print STDERR "refs: ",join(", ", ref($handler), ref($registration_info),
# ref($request_info), ref($requests)),"\n";
print "==============================================\n" if ($debugging);
print STDERR "processing a request of type " .
$request_info->getMode() . "\n" if ($debugging) ;
#
# Process each varbind in teh list of requests
#
for($request = $requests; $request; $request = $request->next()) {
my $oid = $request->getOID();
print STDERR "-- processing request of $oid (request $reqnum) \n" if ($debugging);
#
# Handle the different request types
#
my $mode = $request_info->getMode();
if ($mode == MODE_GET) {
getLeaf($oid, $request, $request_info);
} elsif ($mode == MODE_GETNEXT) {
getNextOid($oid, $request, $request_info);
} else {
print STDERR "Request type $mode not supported\n";
}
$reqnum++;
}
print STDERR " finished processing\n"
if ($debugging);
}
##########################################################
# Given an oid see if there is an entry in the oidtable
# and get the record if there is.
#
# Passed the oid as a NetSNMP oid
#
# Returns the record if found
#
##########################################################
sub findOid {
my $oid = shift;
# Convert the OID to a string
# The index items are temporarily set to zero to cater for tables
my @indexes = $oid->to_array();
my $idxoffset = $oid->length() - 1;
# Locate the record in the table if it exists
# If no match then try setting index values to zero until
# we have a match of we exhaust the oid
while($idxoffset) {
my $oidstr="." . join ".", @indexes;
my $rec = $oidtable->{$oidstr};
# Return the record if found and the repaired index array
if($rec) {
print "Found OID $oid ($oidstr) in the table\n" if ($debugging);
return ($rec);
} else {
# Not found. Set the next potential index to zero and
# try again
$indexes[$idxoffset] = 0;
$idxoffset--;
}
}
return (0);
}
##########################################################
# Sub to return an element of an OID
# This is commonly used to get an index item from
# an OID for table accesses.
##########################################################
sub getOidElement {
my ($oid, $idx) = @_;
my @idx = $oid->to_array();
my $len = $oid->length();
my $val = $idx[$idx];
return $val;
}
##########################################################
# Sub to set an element of an OID
# Returns a new NetSNMP::OID object
##########################################################
sub setOidElement {
my ($oid, $offset, $val) = @_;
my @idx = $oid->to_array();
$idx[$offset] = $val;
my $str = "." . join ".", @idx;
return new NetSNMP::OID($str);;
}
##########################################################
# Given scalar record in the oidtable get the value.
# Passed the record and the request.
##########################################################
sub getScalar {
my ($rec, $request) = @_;
# Got a scalar node from the table
my $type = $rec->{type};
# Call the GET function
my $val = $rec->{func}();
$request->setValue($type, $val);
}
############################################################
# Given a record in the OID table that is a columnar object
# locate any objects that have the required index.
#
# Passed the record, the oid object and the request
############################################################
sub getColumnar {
my ($rec, $oid, $request) = @_;
print "Get Columnar $oid\n" if ($debugging);
my $type = $rec->{type};
my $args = $rec->{args};
# Check the index is in range and exists
if($rec->{check}($oid)) {
# Call the handler function with the oid
my $val = $rec->{func}($oid);
# Set the value found in the request
$request->setValue($type, $val);
}
}
######################################################################
#
# If the oid is in range then set the data in the supplied request
# object.
#
# Tries to get a scalar first then checks the coumnar second
#
# Return 1 if successful or 0 if not
#
#######################################################################
sub getLeaf {
my $oid = shift;
my $request = shift;
my $request_info = shift;
print "getLeaf: $oid\n" if ($debugging);
# Find an oid entry in the table
my ($rec) = findOid($oid);
if($rec) {
# Record found. Use the istable flag to pass control to the
# scalar or coulmnar handler
if($rec->{istable} == 1) {
return getColumnar($rec, $oid, $request);
} else {
return getScalar($rec, $request);
}
}
}
#####################################################
#
# getNextOid
#
# The workhorse for GETNEXT.
# Given an OID, locates the next oid and if valid does
# a getLeaf on that OID. It does this by walking the list of
# OIDs. We try to otimise the walk by first looking for an oid
# in the list as follows:
#
# 1. Try to locate an oid match in the table.
# If that succeeds then look for the next object in the table
# using the next attribute and get that object.
#
# 2. If the OID found is a table element then use the table
# specific index handler to see if there is an item with the
# next index.This will retutn either an oid which we get, or 0.
# If there is not then we continue our walk along the tree
#
# 3.If the supplied oid is not found, but is in the range of our oids
# then we start at the root oid and walk the list until we either
# drop of the end, or we fnd an OID that is greater than the OID supplied.
# In all cases if we sucessfully find an OID to retrieve,
# then we set the next OID in the resposnse.
#
######################################################
sub getNextOid {
my $oid = shift;
my $request = shift;
my $request_info = shift;
my $curoid = new NetSNMP::OID($oidroot); # Current OID in the walk
# Find the starting position if we can
my $current = findOid($oid);
# print Dumper($current);
if($current) {
# Found the start in the table
$curoid = new NetSNMP::OID($oid);
# If the node we found is not a table then start at the
# next oid in the table
unless($current->{istable}) {
my $nextoid = $current->{next};
$curoid = new NetSNMP::OID($nextoid);
# print "Not a table so using the next $nextoid\n";
$current = $oidtable->{$nextoid};
}
}
# If we cannot find the starting point in the table, then start
# at the top and
# walk along the list until we drop off the end
# or we get to an oid that is >= to the one we want.
else {
# print "Not found so using the top ($oidroot)\n";
$current = $oidtable->{$oidroot};
while($current && $curoid <= $oid) {
my $nextoid = $current->{next};
print "Trying $nextoid\n" if ($debugging);
$current = $oidtable->{$nextoid};
$curoid = $current ? new NetSNMP::OID($nextoid) : undef;
}
}
##
## Got a starting point
## $current points to the node in the table
## $curoid is a NetSNMP::OID object with the oid we are trying
##
print "Got a startng point of " . Dumper($current) if ($debugging);
while($current) {
if($current->{istable}) {
# Got a table oid. See if the next is valid for the table
# print "Trying table\n";
my $nextoid = $current->{nextoid}($curoid);
# print Dumper($nextoid);
if($nextoid) {
getColumnar($current, $nextoid, $request);
$request->setOID($nextoid);
return;
}
# No not this one so try the next
$nextoid = $current->{next};
$current = $oidtable->{$nextoid};
$curoid = $current ? new NetSNMP::OID($nextoid) : undef;
print "Trying next $curoid $nextoid\n" if ($debugging);
} else {
# Must be the current node
if(getScalar($current, $request)) {
$request->setOID($curoid);
return 1;
}
}
}
}
# Return true from this module
1;
|