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
11 # 1) modify snmpd.conf and add the following line. Note that the
12 # last field must match the path/name of the script:
14 # pass_persist .1.3.6.1.4.1.2021.250.255 /root/snmptest.pl
16 # This assumes you've already configured snmpd, i.e. community
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.
28 my $SNMPver = "snmp1.0";
29 my $DHCPver = "dhcp1.0";
30 my $VERSION = "$SNMPver/$DHCPver";
32 # set $dbg to a non-zero value to get debug output in /tmp
37 # Here's an example entry for snmpd.conf:
38 # pass_persist .1.3.6.1.4.1.2021.250.255 /path/to/this/script.pl
39 # so in this case $root should be '.1.3.6.1.4.1.2021.250.255'
41 # $root.1.x : shared network name for index x
42 # $root.2.x : IP range name for index x
43 # $root.3.x : Range Contained-In for index x
44 # $root.4.x : Shared Net Stats for index x
45 # $root.5.x : IP range stats for index x
47 # $root.4.1.x : 'max' value for shared network at index=x
48 # $root.4.2.x : 'cur' value for shared network at index=x
49 # $root.4.3.x : 'tou' value for shared network at index=x
51 # $root.5.1.x : 'max' value for IP range at index=x
52 # $root.5.2.x : 'cur' value for IP range at index=x
53 # $root.5.3.x : 'tou' value for IP range at index=x
56 # $root must match pass_persis entry in snmpd.conf
58 my $root='.1.3.6.1.4.1.2021.250.255';
61 # $datafile is the location of the .CSV file created by dhcpd-pools.
62 # NOTE: It is assumed that this file is routinely updated, i.e. at least
63 # every 5 minutes, by some other process, i.e. cron.
65 my $datafile='/var/lib/dhcp/dhcpstats.csv';
68 # $cachetime determines how long we can cache parse data from $datafile. If
69 # more than $cachetime has elapsed we'll re-read $datafile, otherwise we'll
70 # just send back the last-parsed data
72 # Generally 60 seconds seems pretty reasonable, but any positive value is
78 # global variables for DHCP
87 # global variables for SNMP
91 ########## BEGIN UNIQUE-CODE FOR YOUR SNMP-QUERY NEEDS
93 # ParseDataFile does just that, and stores info in %SharedNetwork for later
96 # The routine ParseDataFile is calls at invocation to initialize all
97 # datasets and also periodically (based on $cachetime).
99 my @where = qw( unknown Ranges SharedNets SumData );
100 sub ParseDataFile () {
101 print DBG "ParseDataFile\n" if $dbg;
102 my $where = 0; # location in file unknown
103 # used to generate ifIndex-like values for each Shared network
105 # used to generate ifIndex-like values for each Shared network
108 undef %SharedNetwork;
110 undef %RangeContained;
115 open(IN, $datafile) || return undef;
118 print DBG "...Read: $_\n" if $dbg;
119 next if /^\s*$/; # skip blank lines
120 next if /^"shared net name"/; # skip titles
121 next if /^"name","max","cur"/; # skip titles
122 if (/^"Ranges/) { $where = 1; next; }
123 if (/^"Shared/) { $where = 2; next; }
124 if (/^"Sum of/) { $where = 3; next; }
125 print DBG "...Found data for @where[$where]: $_\n" if $dbg;
129 # "shared net name","1stIP","lastIP","max","cur","%","touch","t+c","t+c %"
131 # "Shared networks:" and "Sum of all ranges:"
133 # "name","max","cur","%","touch","t+c","t+c %"
135 my @data = split /,/;
136 my ($nam, $max, $cur, $tou, $fir);
137 ($nam = @data[0]) =~ s/"//g; # shared net name
139 ($fir = @data[1]) =~ s/"//g if ($where == 1); # Range ID (1st IP)
141 ($max = @data[3]) =~ s/"//g if ($where == 1); # max # of IPs
142 ($max = @data[1]) =~ s/"//g if ($where > 1); # max # of IPs
144 ($cur = @data[4]) =~ s/"//g if ($where == 1); # cur # of IPs
145 ($cur = @data[2]) =~ s/"//g if ($where > 1); # cur # of IPs
147 ($tou = @data[6]) =~ s/"//g if ($where == 1); # touched IP's
148 ($tou = @data[4]) =~ s/"//g if ($where > 1); # touched IP's
151 print DBG "...idx sh/rg=$shareindex/$rangeindex : nam=$nam : max=$max : cur=$cur : tou=$tou\n" if $dbg;
154 # Summary data for each IP range
156 if ($where == 1) { # Store IP range data
157 if (defined($RangeName{$fir})) {
158 print DBG "...WARNING: duplicate name '$fir'\n" if $dbg
160 $IPRNindex[$rangeindex] = $fir;
161 $RangeName{$fir} = pack("LLLL", $rangeindex, $max, $cur, $tou);
162 $RangeContained{$fir} = $nam;
163 push @validoidlist, "$root.2.$rangeindex"; # IP range name
164 push @validoidlist, "$root.3.$rangeindex"; # Contained In
165 push @validoidlist, "$root.5.1.$rangeindex"; # range stats
166 push @validoidlist, "$root.5.2.$rangeindex"; # range stats
167 push @validoidlist, "$root.5.3.$rangeindex"; # range stats
172 # Summary data for each Shared network
174 if ($where == 2) { # Store Shared network summary data
175 if (defined($SharedNetwork{$nam})) {
176 print DBG "...WARNING: duplicate name '$nam'\n" if $dbg
178 $SNNindex[$shareindex] = $nam;
179 $SharedNetwork{$nam} = pack("LLLL", $shareindex, $max, $cur, $tou);
180 push @validoidlist, "$root.1.$shareindex"; # shared name
181 push @validoidlist, "$root.4.1.$shareindex"; # shared stats
182 push @validoidlist, "$root.4.2.$shareindex"; # shared stats
183 push @validoidlist, "$root.4.3.$shareindex"; # shared stats
188 # Summary data for everything
190 if ($where == 3) { # Store "All" network summary data
191 print DBG "...We don't store 'All' info yet!\n" if $dbg;
196 foreach (sort @validoidlist) { print DBG "ValidOID: $_\n"; }
200 foreach (@IPRNindex) {
201 print DBG "IP range $_\n";
203 foreach (@SNNindex) {
204 print DBG "Shared Net $_\n";
209 #############################################################################
210 #############################################################################
211 #############################################################################
212 # Forward Declarations
214 sub SendReturnNone ();
218 # GetData gets data, but will leverage caching of data if last parse was
219 # over 60 seconds ago
225 (my $suboid = $oid) =~ s/$root//;
228 # If enough time has elapsed, go fetch fresh data, otherwise use cached
231 print DBG "GetData: Time is: ", time, " : oid $oid->$suboid\n" if $dbg;
232 if ((time - $lasttime) > $cachetime) {
238 # Quick sanity check of OID string:
240 if ( (!($oid =~ /^$root/)) ||
247 # split the remaining OID pieces to analyse
249 my @oidlist = split(/\./, $suboid);
251 ###################### EDIT THIS FOR YOUR APPLICATION ######################
253 # If we only get a single value, then barf (we need 2)
256 $good = 1 if (($oidlist[1] eq '1') && ($#oidlist == 2)); # shared name
257 $good = 1 if (($oidlist[1] eq '2') && ($#oidlist == 2)); # ip range name
258 $good = 1 if (($oidlist[1] eq '3') && ($#oidlist == 2)); # contained in
259 $good = 1 if (($oidlist[1] eq '4') && ($#oidlist == 3)); # shared-range stats
260 $good = 1 if (($oidlist[1] eq '5') && ($#oidlist == 3)); # ip-range stats
263 print DBG "oidlistcount = $#oidlist and oidlist1 is ", $oidlist[1], "\n" if $dbg;
269 # $oidlist[2] = index for shared-name or range-name
270 # or datatype to return
271 # $oidlist[3] = index for datatype
273 if ($oidlist[1] eq '1') { # looking for name associated with shared net
274 SendReturn($oid, 'string', $SNNindex[$oidlist[2]]);
278 if ($oidlist[1] eq '2') { # looking for name associated with ip range
279 print DBG "Responding with IPRNindex[$oidlist[2]]\n" if $dbg;
280 SendReturn($oid, 'string', $IPRNindex[$oidlist[2]]);
284 if ($oidlist[1] eq '3') { # looking for contained-in info
285 if (defined($IPRNindex[$oidlist[2]])) { # valid subnet!
286 my $fir = $IPRNindex[$oidlist[2]];
287 my $con = $RangeContained{$fir};
288 my @vals= unpack("LLLL", $SharedNetwork{$con});
289 print DBG "fir=$fir : con=$con : vals=@vals : vals0=$vals[0]\n" if $dbg;
290 SendReturn($oid, 'integer', @vals[0]);
298 if ($oidlist[1] eq '4') {
299 if (defined($SNNindex[$oidlist[3]])) { # valid subnet!
300 my $nam = $SNNindex[$oidlist[3]];
301 my @vals= unpack("LLLL", $SharedNetwork{$nam});
302 print DBG "nam=$nam, vals=@vals, oidlist[2]=$oidlist[2]\n" if $dbg;
303 SendReturn($oid, 'integer', @vals[$oidlist[2]]);
306 print DBG "invalid subnet SNN $oidlist[3]\n" if $dbg;
312 if ($oidlist[1] eq '5') {
313 if (defined($IPRNindex[$oidlist[3]])) { # valid subnet!
314 my $fir = $IPRNindex[$oidlist[3]];
315 my @vals= unpack("LLLL", $RangeName{$fir});
316 print DBG "fir=$fir, vals=@vals, oidlist[2]=", $oidlist[2], "\n" if $dbg;
317 SendReturn($oid, 'integer', @vals[$oidlist[2]]);
320 print DBG "invalid subnet IPRN $oidlist[3]\n" if $dbg;
325 ################## END EDIT THIS FOR YOUR APPLICATION ######################
328 #############################################################################
329 #############################################################################
330 #############################################################################
332 # From here down the routines should NEVER change
335 # SNMP-specific routines
338 { ######## limit scope for some variable
340 # Compare looks at the OID validoid and userquery
341 # and returns +1 if userquery > validoid
342 # and returns -1 if userquery < validoid
343 # and returns 0 if userquery = validoid
349 while (($i <= $#validoid) && ($i <= $#userquery)) {
350 # print DBG "Comparing $validoid[$i] vs $userquery[$i]\n" if $dbg;
351 if ($userquery[$i] > $validoid[$i]) { return +1; }
352 if ($userquery[$i] < $validoid[$i]) { return -1; }
357 $returnval = +1 if (($i < $#userquery) && ($i == $#validoid));
358 $returnval = -1 if (($i == $#userquery) && ($i < $#validoid));
363 # FindNext looks through the list of validoid's trying to find the Next
364 # oid after $oid (aka @userquery)
367 my $userqueryoid = shift;
368 print DBG "FindNext($userqueryoid)\n" if $dbg;
369 my $next = $validoidlist[0];
371 @userquery = split (/\./, $userqueryoid);
373 foreach (sort @validoidlist) {
375 print DBG "Comparing $userqueryoid vs. $_\n" if $dbg;
376 @validoid = split (/\./, $_);
383 if (!$found) { return undef; }
385 print DBG "Returning $next as next valid OID\n" if $dbg;
389 } ######### end scope limit
391 sub SendReturnNone () {
393 print DBG "Sent NONE\n" if $dbg;
396 sub SendReturn($$$) {
397 printf "%s\n%s\n%s\n", shift, shift, shift;
411 $oid = FindNext($oid);
424 print "not-writable\n"; # we don't support snmpset
425 print DBG "Sent not-writable\n" if $dbg;
431 print DBG "PONG Sent\n" if $dbg;
435 ################################## START ##################################
440 select((select(STDOUT), $| = 1)[0]);
443 open(DBG, ">/tmp/snmp.dbg") || die ("Can't open debug!");
444 select((select(DBG), $| = 1)[0]);
445 print DBG "Version $VERSION\n";
459 printf DBG "%3d '%s'\n", $line, $_ if $dbg;
461 $cmd = $_ if ($line == 1);
462 $oid = $_ if ($line == 2);
463 $tv = $_ if ($line == 3);
464 Pong() if ($cmd eq 'ping');
465 Get($cmd, $oid) if (($cmd eq 'get') && ($line == 2));
466 GetNext($cmd, $oid) if (($cmd eq 'getnext') && ($line == 2));
467 Set($cmd, $oid, $tv) if (($cmd eq 'set') && ($line == 3));
470 print DBG "Clean Exit\n" if $dbg;