| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# <Note from="Sami Kerola"> |
| 4 |
# This script is anonymous user contribution (due legal |
| 5 |
# department did not let the person the get credit because |
| 6 |
# they are afraid open source contributions). You know who you |
| 7 |
# are, and I am graceful to get this to be part of the |
| 8 |
# package. |
| 9 |
# </Note> |
| 10 |
|
| 11 |
# 1) modify snmpd.conf and add the following line. Note that the |
| 12 |
# last field must match the path/name of the script: |
| 13 |
# |
| 14 |
# pass_persist .1.3.6.1.4.1.2021.250.255 /root/snmptest.pl |
| 15 |
# |
| 16 |
# This assumes you've already configured snmpd, i.e. community |
| 17 |
# strings, etc. |
| 18 |
# |
| 19 |
# 2) Install the following script. Note that if you changed the |
| 20 |
# OID string above, then you'll need to change the value for |
| 21 |
# $root in this file, too. Be sure that $datafile is set to |
| 22 |
# the location of the .CSV output from dhcpd-pools. NOTE: if |
| 23 |
# you set $dbg to 1 then output will be generated in /tmp. |
| 24 |
|
| 25 |
use strict; |
| 26 |
use NetSNMP::OID; |
| 27 |
|
| 28 |
# Version info: |
| 29 |
my $SNMPver = "snmp1.1"; |
| 30 |
my $DHCPver = "dhcp1.0"; |
| 31 |
my $VERSION = "$SNMPver/$DHCPver"; |
| 32 |
# |
| 33 |
# set $dbg to a non-zero value to get debug output in /tmp |
| 34 |
# |
| 35 |
my $dbg=0; |
| 36 |
|
| 37 |
# |
| 38 |
# Here's an example entry for snmpd.conf: |
| 39 |
# pass_persist .1.3.6.1.4.1.2021.250.255 /path/to/this/script.pl |
| 40 |
# so in this case $root should be '.1.3.6.1.4.1.2021.250.255' |
| 41 |
# |
| 42 |
# $root.1.x : shared network name for index x |
| 43 |
# $root.2.x : IP range name for index x |
| 44 |
# $root.3.x : Range Contained-In for index x |
| 45 |
# $root.4.x : Shared Net Stats for index x |
| 46 |
# $root.5.x : IP range stats for index x |
| 47 |
# |
| 48 |
# $root.4.1.x : 'max' value for shared network at index=x |
| 49 |
# $root.4.2.x : 'cur' value for shared network at index=x |
| 50 |
# $root.4.3.x : 'tou' value for shared network at index=x |
| 51 |
# |
| 52 |
# $root.5.1.x : 'max' value for IP range at index=x |
| 53 |
# $root.5.2.x : 'cur' value for IP range at index=x |
| 54 |
# $root.5.3.x : 'tou' value for IP range at index=x |
| 55 |
|
| 56 |
# |
| 57 |
# $root must match pass_persis entry in snmpd.conf |
| 58 |
# |
| 59 |
my $root='.1.3.6.1.4.1.2021.250.255'; |
| 60 |
|
| 61 |
# |
| 62 |
# $datafile is the location of the .CSV file created by dhcpd-pools. |
| 63 |
# NOTE: It is assumed that this file is routinely updated, i.e. at least |
| 64 |
# every 5 minutes, by some other process, i.e. cron. |
| 65 |
# |
| 66 |
my $datafile='/var/lib/dhcp/dhcpstats.csv'; |
| 67 |
|
| 68 |
# |
| 69 |
# $cachetime determines how long we can cache parse data from $datafile. If |
| 70 |
# more than $cachetime has elapsed we'll re-read $datafile, otherwise we'll |
| 71 |
# just send back the last-parsed data |
| 72 |
# |
| 73 |
# Generally 60 seconds seems pretty reasonable, but any positive value is |
| 74 |
# permitted. |
| 75 |
# |
| 76 |
my $cachetime = 60; |
| 77 |
|
| 78 |
# |
| 79 |
# global variables for DHCP |
| 80 |
# |
| 81 |
my %SharedNetwork; |
| 82 |
my %RangeName; |
| 83 |
my %RangeContained; |
| 84 |
my @SNNindex; |
| 85 |
my @IPRNindex; |
| 86 |
|
| 87 |
# |
| 88 |
# global variables for SNMP |
| 89 |
# |
| 90 |
my @validoidlist; |
| 91 |
|
| 92 |
########## BEGIN UNIQUE-CODE FOR YOUR SNMP-QUERY NEEDS |
| 93 |
# |
| 94 |
# ParseDataFile does just that, and stores info in %SharedNetwork for later |
| 95 |
# processing. |
| 96 |
# |
| 97 |
# The routine ParseDataFile is calls at invocation to initialize all |
| 98 |
# datasets and also periodically (based on $cachetime). |
| 99 |
# |
| 100 |
my @where = qw( unknown Ranges SharedNets SumData ); |
| 101 |
sub ParseDataFile () { |
| 102 |
print DBG "ParseDataFile\n" if $dbg; |
| 103 |
my $where = 0; # location in file unknown |
| 104 |
# used to generate ifIndex-like values for each Shared network |
| 105 |
my $shareindex = 1; |
| 106 |
# used to generate ifIndex-like values for each Shared network |
| 107 |
my $rangeindex = 1; |
| 108 |
|
| 109 |
undef %SharedNetwork; |
| 110 |
undef %RangeName; |
| 111 |
undef %RangeContained; |
| 112 |
undef @SNNindex; |
| 113 |
undef @IPRNindex; |
| 114 |
@validoidlist = (); |
| 115 |
|
| 116 |
open(IN, $datafile) || return undef; |
| 117 |
while (<IN>) { |
| 118 |
chomp; |
| 119 |
print DBG "...Read: $_\n" if $dbg; |
| 120 |
next if /^\s*$/; # skip blank lines |
| 121 |
next if /^"shared net name"/; # skip titles |
| 122 |
next if /^"name","max","cur"/; # skip titles |
| 123 |
if (/^"Ranges/) { $where = 1; next; } |
| 124 |
if (/^"Shared/) { $where = 2; next; } |
| 125 |
if (/^"Sum of/) { $where = 3; next; } |
| 126 |
print DBG "...Found data for @where[$where]: $_\n" if $dbg; |
| 127 |
# |
| 128 |
# "Ranges:" |
| 129 |
# 0 1 2 3 4 5 6 7 8 |
| 130 |
# "shared net name","1stIP","lastIP","max","cur","%","touch","t+c","t+c %" |
| 131 |
# |
| 132 |
# "Shared networks:" and "Sum of all ranges:" |
| 133 |
# 0 1 2 3 4 5 6 |
| 134 |
# "name","max","cur","%","touch","t+c","t+c %" |
| 135 |
# |
| 136 |
my @data = split /,/; |
| 137 |
my ($nam, $max, $cur, $tou, $fir); |
| 138 |
($nam = @data[0]) =~ s/"//g; # shared net name |
| 139 |
|
| 140 |
($fir = @data[1]) =~ s/"//g if ($where == 1); # Range ID (1st IP) |
| 141 |
|
| 142 |
($max = @data[3]) =~ s/"//g if ($where == 1); # max # of IPs |
| 143 |
($max = @data[1]) =~ s/"//g if ($where > 1); # max # of IPs |
| 144 |
|
| 145 |
($cur = @data[4]) =~ s/"//g if ($where == 1); # cur # of IPs |
| 146 |
($cur = @data[2]) =~ s/"//g if ($where > 1); # cur # of IPs |
| 147 |
|
| 148 |
($tou = @data[6]) =~ s/"//g if ($where == 1); # touched IP's |
| 149 |
($tou = @data[4]) =~ s/"//g if ($where > 1); # touched IP's |
| 150 |
|
| 151 |
|
| 152 |
print DBG "...idx sh/rg=$shareindex/$rangeindex : nam=$nam : max=$max : cur=$cur : tou=$tou\n" if $dbg; |
| 153 |
|
| 154 |
# |
| 155 |
# Summary data for each IP range |
| 156 |
# |
| 157 |
if ($where == 1) { # Store IP range data |
| 158 |
if (defined($RangeName{$fir})) { |
| 159 |
print DBG "...WARNING: duplicate name '$fir'\n" if $dbg |
| 160 |
} |
| 161 |
$IPRNindex[$rangeindex] = $fir; |
| 162 |
$RangeName{$fir} = pack("LLLL", $rangeindex, $max, $cur, $tou); |
| 163 |
$RangeContained{$fir} = $nam; |
| 164 |
push @validoidlist, "$root.2.$rangeindex"; # IP range name |
| 165 |
push @validoidlist, "$root.3.$rangeindex"; # Contained In |
| 166 |
push @validoidlist, "$root.5.1.$rangeindex"; # range stats |
| 167 |
push @validoidlist, "$root.5.2.$rangeindex"; # range stats |
| 168 |
push @validoidlist, "$root.5.3.$rangeindex"; # range stats |
| 169 |
$rangeindex++; |
| 170 |
} |
| 171 |
|
| 172 |
# |
| 173 |
# Summary data for each Shared network |
| 174 |
# |
| 175 |
if ($where == 2) { # Store Shared network summary data |
| 176 |
if (defined($SharedNetwork{$nam})) { |
| 177 |
print DBG "...WARNING: duplicate name '$nam'\n" if $dbg |
| 178 |
} |
| 179 |
$SNNindex[$shareindex] = $nam; |
| 180 |
$SharedNetwork{$nam} = pack("LLLL", $shareindex, $max, $cur, $tou); |
| 181 |
push @validoidlist, "$root.1.$shareindex"; # shared name |
| 182 |
push @validoidlist, "$root.4.1.$shareindex"; # shared stats |
| 183 |
push @validoidlist, "$root.4.2.$shareindex"; # shared stats |
| 184 |
push @validoidlist, "$root.4.3.$shareindex"; # shared stats |
| 185 |
$shareindex++; |
| 186 |
} |
| 187 |
|
| 188 |
# |
| 189 |
# Summary data for everything |
| 190 |
# |
| 191 |
if ($where == 3) { # Store "All" network summary data |
| 192 |
print DBG "...We don't store 'All' info yet!\n" if $dbg; |
| 193 |
} |
| 194 |
} |
| 195 |
close IN; |
| 196 |
if ($dbg) { |
| 197 |
foreach (sort Oidcmp @validoidlist) { print DBG "ValidOID: $_\n"; } |
| 198 |
} |
| 199 |
|
| 200 |
if ($dbg) { |
| 201 |
foreach (@IPRNindex) { |
| 202 |
print DBG "IP range $_\n"; |
| 203 |
} |
| 204 |
foreach (@SNNindex) { |
| 205 |
print DBG "Shared Net $_\n"; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
############################################################################# |
| 211 |
############################################################################# |
| 212 |
############################################################################# |
| 213 |
# Forward Declarations |
| 214 |
# |
| 215 |
sub SendReturnNone (); |
| 216 |
sub SendReturn($$$); |
| 217 |
|
| 218 |
# |
| 219 |
# GetData gets data, but will leverage caching of data if last parse was |
| 220 |
# over 60 seconds ago |
| 221 |
# |
| 222 |
my $lasttime = 0; |
| 223 |
sub GetData ($) { |
| 224 |
my $oid = shift; |
| 225 |
|
| 226 |
(my $suboid = $oid) =~ s/$root//; |
| 227 |
|
| 228 |
# |
| 229 |
# If enough time has elapsed, go fetch fresh data, otherwise use cached |
| 230 |
# data |
| 231 |
# |
| 232 |
print DBG "GetData: Time is: ", time, " : oid $oid->$suboid\n" if $dbg; |
| 233 |
if ((time - $lasttime) > $cachetime) { |
| 234 |
ParseDataFile(); |
| 235 |
$lasttime = time; |
| 236 |
} |
| 237 |
|
| 238 |
# |
| 239 |
# Quick sanity check of OID string: |
| 240 |
# |
| 241 |
if ( (!($oid =~ /^$root/)) || |
| 242 |
($suboid eq '') ) { |
| 243 |
SendReturnNone(); |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
# |
| 248 |
# split the remaining OID pieces to analyse |
| 249 |
# |
| 250 |
my @oidlist = split(/\./, $suboid); |
| 251 |
|
| 252 |
###################### EDIT THIS FOR YOUR APPLICATION ###################### |
| 253 |
# |
| 254 |
# If we only get a single value, then barf (we need 2) |
| 255 |
# |
| 256 |
my $good = 0; |
| 257 |
$good = 1 if (($oidlist[1] eq '1') && ($#oidlist == 2)); # shared name |
| 258 |
$good = 1 if (($oidlist[1] eq '2') && ($#oidlist == 2)); # ip range name |
| 259 |
$good = 1 if (($oidlist[1] eq '3') && ($#oidlist == 2)); # contained in |
| 260 |
$good = 1 if (($oidlist[1] eq '4') && ($#oidlist == 3)); # shared-range stats |
| 261 |
$good = 1 if (($oidlist[1] eq '5') && ($#oidlist == 3)); # ip-range stats |
| 262 |
|
| 263 |
if (!$good) { |
| 264 |
print DBG "oidlistcount = $#oidlist and oidlist1 is ", $oidlist[1], "\n" if $dbg; |
| 265 |
SendReturnNone(); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
# $oidlist[1] = 1-5 |
| 270 |
# $oidlist[2] = index for shared-name or range-name |
| 271 |
# or datatype to return |
| 272 |
# $oidlist[3] = index for datatype |
| 273 |
# |
| 274 |
if ($oidlist[1] eq '1') { # looking for name associated with shared net |
| 275 |
SendReturn($oid, 'string', $SNNindex[$oidlist[2]]); |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
if ($oidlist[1] eq '2') { # looking for name associated with ip range |
| 280 |
print DBG "Responding with IPRNindex[$oidlist[2]]\n" if $dbg; |
| 281 |
SendReturn($oid, 'string', $IPRNindex[$oidlist[2]]); |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
if ($oidlist[1] eq '3') { # looking for contained-in info |
| 286 |
if (defined($IPRNindex[$oidlist[2]])) { # valid subnet! |
| 287 |
my $fir = $IPRNindex[$oidlist[2]]; |
| 288 |
my $con = $RangeContained{$fir}; |
| 289 |
my @vals= unpack("LLLL", $SharedNetwork{$con}); |
| 290 |
print DBG "fir=$fir : con=$con : vals=@vals : vals0=$vals[0]\n" if $dbg; |
| 291 |
SendReturn($oid, 'integer', @vals[0]); |
| 292 |
return; |
| 293 |
} else { |
| 294 |
SendReturnNone(); |
| 295 |
return; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if ($oidlist[1] eq '4') { |
| 300 |
if (defined($SNNindex[$oidlist[3]])) { # valid subnet! |
| 301 |
my $nam = $SNNindex[$oidlist[3]]; |
| 302 |
my @vals= unpack("LLLL", $SharedNetwork{$nam}); |
| 303 |
print DBG "nam=$nam, vals=@vals, oidlist[2]=$oidlist[2]\n" if $dbg; |
| 304 |
SendReturn($oid, 'integer', @vals[$oidlist[2]]); |
| 305 |
return; |
| 306 |
} else { |
| 307 |
print DBG "invalid subnet SNN $oidlist[3]\n" if $dbg; |
| 308 |
SendReturnNone(); |
| 309 |
return; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if ($oidlist[1] eq '5') { |
| 314 |
if (defined($IPRNindex[$oidlist[3]])) { # valid subnet! |
| 315 |
my $fir = $IPRNindex[$oidlist[3]]; |
| 316 |
my @vals= unpack("LLLL", $RangeName{$fir}); |
| 317 |
print DBG "fir=$fir, vals=@vals, oidlist[2]=", $oidlist[2], "\n" if $dbg; |
| 318 |
SendReturn($oid, 'integer', @vals[$oidlist[2]]); |
| 319 |
return; |
| 320 |
} else { |
| 321 |
print DBG "invalid subnet IPRN $oidlist[3]\n" if $dbg; |
| 322 |
SendReturnNone(); |
| 323 |
return; |
| 324 |
} |
| 325 |
} |
| 326 |
################## END EDIT THIS FOR YOUR APPLICATION ###################### |
| 327 |
} |
| 328 |
|
| 329 |
############################################################################# |
| 330 |
############################################################################# |
| 331 |
############################################################################# |
| 332 |
# |
| 333 |
# From here down the routines should NEVER change |
| 334 |
# |
| 335 |
# |
| 336 |
# SNMP-specific routines |
| 337 |
# |
| 338 |
|
| 339 |
{ ######## limit scope for some variable |
| 340 |
# |
| 341 |
# Compare looks at the OID validoid and userquery |
| 342 |
# and returns +1 if userquery > validoid |
| 343 |
# and returns -1 if userquery < validoid |
| 344 |
# and returns 0 if userquery = validoid |
| 345 |
# |
| 346 |
my @validoid; |
| 347 |
my @userquery; |
| 348 |
sub Compare () { |
| 349 |
my $i=1; |
| 350 |
while (($i <= $#validoid) && ($i <= $#userquery)) { |
| 351 |
# print DBG "Comparing $validoid[$i] vs $userquery[$i]\n" if $dbg; |
| 352 |
if ($userquery[$i] > $validoid[$i]) { return +1; } |
| 353 |
if ($userquery[$i] < $validoid[$i]) { return -1; } |
| 354 |
$i++; |
| 355 |
} |
| 356 |
$i--; |
| 357 |
my $returnval = 0; |
| 358 |
$returnval = +1 if (($i < $#userquery) && ($i == $#validoid)); |
| 359 |
$returnval = -1 if (($i == $#userquery) && ($i < $#validoid)); |
| 360 |
return $returnval; |
| 361 |
} |
| 362 |
|
| 363 |
# |
| 364 |
# FindNext looks through the list of validoid's trying to find the Next |
| 365 |
# oid after $oid (aka @userquery) |
| 366 |
# |
| 367 |
sub FindNext ($) { |
| 368 |
my $userqueryoid = shift; |
| 369 |
print DBG "FindNext($userqueryoid)\n" if $dbg; |
| 370 |
my $next = $validoidlist[0]; |
| 371 |
|
| 372 |
@userquery = split (/\./, $userqueryoid); |
| 373 |
my $found = 0; |
| 374 |
foreach (sort Oidcmp @validoidlist) { |
| 375 |
$next = $_; |
| 376 |
print DBG "Comparing $userqueryoid vs. $_\n" if $dbg; |
| 377 |
@validoid = split (/\./, $_); |
| 378 |
my $x = Compare(); |
| 379 |
if ($x < 0) { |
| 380 |
$found = 1; |
| 381 |
last; |
| 382 |
} |
| 383 |
} |
| 384 |
if (!$found) { return undef; } |
| 385 |
|
| 386 |
print DBG "Returning $next as next valid OID\n" if $dbg; |
| 387 |
return $next; |
| 388 |
} |
| 389 |
|
| 390 |
} ######### end scope limit |
| 391 |
|
| 392 |
sub SendReturnNone () { |
| 393 |
print "NONE\n"; |
| 394 |
print DBG "Sent NONE\n" if $dbg; |
| 395 |
} |
| 396 |
|
| 397 |
sub SendReturn($$$) { |
| 398 |
printf "%s\n%s\n%s\n", shift, shift, shift; |
| 399 |
} |
| 400 |
|
| 401 |
my $line=0; |
| 402 |
sub Get ($$) { |
| 403 |
my $cmd = shift; |
| 404 |
my $oid = shift; |
| 405 |
GetData($oid); |
| 406 |
$line = 0; |
| 407 |
} |
| 408 |
|
| 409 |
sub GetNext ($$) { |
| 410 |
my $cmd = shift; |
| 411 |
my $oid = shift; |
| 412 |
$oid = FindNext($oid); |
| 413 |
if (defined($oid)) { |
| 414 |
GetData($oid); |
| 415 |
} else { |
| 416 |
SendReturnNone(); |
| 417 |
} |
| 418 |
$line = 0; |
| 419 |
} |
| 420 |
|
| 421 |
sub Set ($$$) { |
| 422 |
my $cmd = shift; |
| 423 |
my $oid = shift; |
| 424 |
my $tv = shift; |
| 425 |
print "not-writable\n"; # we don't support snmpset |
| 426 |
print DBG "Sent not-writable\n" if $dbg; |
| 427 |
$line = 0; |
| 428 |
} |
| 429 |
|
| 430 |
sub Pong { |
| 431 |
print "PONG\n"; |
| 432 |
print DBG "PONG Sent\n" if $dbg; |
| 433 |
$line = 0; |
| 434 |
} |
| 435 |
|
| 436 |
sub Oidcmp { |
| 437 |
my $oida = new NetSNMP::OID($a); |
| 438 |
my $oidb = new NetSNMP::OID($b); |
| 439 |
snmp_oid_compare($oida, $oidb); |
| 440 |
} |
| 441 |
|
| 442 |
################################## START ################################## |
| 443 |
# |
| 444 |
# Main |
| 445 |
# |
| 446 |
|
| 447 |
select((select(STDOUT), $| = 1)[0]); |
| 448 |
|
| 449 |
if ($dbg) { |
| 450 |
open(DBG, ">/tmp/snmp.dbg") || die ("Can't open debug!"); |
| 451 |
select((select(DBG), $| = 1)[0]); |
| 452 |
print DBG "Version $VERSION\n"; |
| 453 |
} |
| 454 |
|
| 455 |
# Initialize Data |
| 456 |
ParseDataFile(); |
| 457 |
|
| 458 |
my $cmd; |
| 459 |
my $oid; |
| 460 |
my $tv; |
| 461 |
while (<STDIN>) { |
| 462 |
$line++; |
| 463 |
chomp; |
| 464 |
s/ //g; |
| 465 |
tr/A-Z/a-z/; |
| 466 |
printf DBG "%3d '%s'\n", $line, $_ if $dbg; |
| 467 |
last if ($_ eq ''); |
| 468 |
$cmd = $_ if ($line == 1); |
| 469 |
$oid = $_ if ($line == 2); |
| 470 |
$tv = $_ if ($line == 3); |
| 471 |
Pong() if ($cmd eq 'ping'); |
| 472 |
Get($cmd, $oid) if (($cmd eq 'get') && ($line == 2)); |
| 473 |
GetNext($cmd, $oid) if (($cmd eq 'getnext') && ($line == 2)); |
| 474 |
Set($cmd, $oid, $tv) if (($cmd eq 'set') && ($line == 3)); |
| 475 |
} |
| 476 |
|
| 477 |
print DBG "Clean Exit\n" if $dbg; |
| 478 |
close DBG if $dbg; |
| 479 |
exit 0; |