/usr/share/munin/plugins/dhcpd3 is in munin-plugins-extra 2.0.37-1.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/perl -w
# -*- cperl -*-
=head1 NAME
dhcpd3 - Plugin to monitor dhcpd3 leases
=head1 APPLICABLE SYSTEMS
Any system running dhcpd3.
This plugins requires these Perl modules to work: Net::Netmask and
HTTP::Date.
=head1 CONFIGURATION
The following environment settings are the default configuration. The
"user" setting is needed and must be set explicitly.
[dhcpd3]
user root
env.leasefile /var/lib/dhcp3/dhcpd.leases
env.configfile /etc/dhcp3/dhcpd.conf
env.filter ^10\.140\.
env.critical 0.95
env.warning 0.9
The optional filter setting is used to strip parts of ranges for the
network labels (example will show 10.140.80.0 as 80.0). Both critical
and warning are optional settings, default for warning is 0.9 (90%)
and 0.95 for critical (95%).
=head1 INTERPRETATION
The plugin shows the number of used leases by subnet.
=head1 MAGIC MARKERS
#%# family=contrib
#%# capabilities=autoconf
=head1 VERSION
$Id$
=head1 BUGS
If a DHCP config file contains multiple subnets but none of them has a
dynamic range, the dhcp3 plugin only detects this situation for the
last subnet. Need to to improve the parser to properly detect the end
of a subnet definition (Munin trac ticket #829)
=head1 AUTHOR
Rune Nordbøe Skillingstad.
=head1 LICENSE
GPLv2
=cut
my $ret = undef;
if(! eval "require Net::Netmask") {
$ret = "Net::Netmask not found";
}
if(! eval "require HTTP::Date") {
$ret = "HTTP::Date not found";
}
if(! eval "require Net::IP") {
$ret = "Net::IP not found";
}
use strict;
my %leases = ();
my %limits = ();
my %networks = ();
my %ips = ();
my $DEBUG = $ENV{DEBUG} || 0;
my $LEASEFILE = $ENV{leasefile} || "/var/lib/dhcp3/dhcpd.leases";
my $CONFIGFILE = $ENV{configfile} || "/etc/dhcp3/dhcpd.conf";
my $FILTER = $ENV{filter} || "";
my $CRITICAL = $ENV{critical} || 0.95;
my $WARNING = $ENV{warning} || 0.9;
if($ARGV[0] and $ARGV[0] eq "autoconf" ) {
if($ret) {
print "no ($ret)\n";
exit 0;
}
if(-f $LEASEFILE) {
if(-r $LEASEFILE) {
if(-f $CONFIGFILE) {
if(-r $CONFIGFILE) {
print "yes\n";
exit 0;
} else {
print "no (config file not readable)\n";
}
} else {
print "no (config file not found)\n";
}
} else {
print "no (leasefile not readable)\n";
}
} else {
print "no (leasefile not found)\n";
}
exit 0;
}
print "# DEBUG: CONFIGFILE == $CONFIGFILE\n# DEBUG: LEASEFILE == $LEASEFILE\n" if $DEBUG;
Net::Netmask->import();
HTTP::Date->import();
if(! -f $LEASEFILE and ! -f $CONFIGFILE and !$ARGV[0]) {
print "net.value U\n";
exit 0;
}
parseconfig($CONFIGFILE);
if($ARGV[0] and $ARGV[0] eq "config") {
print "graph_title dhcp leases\n";
print "graph_category network\n";
print "graph_args --base 1000 -v leases -l 0\n";
print "graph_order ".join(" ",sort(keys(%leases)))."\n";
foreach my $network (sort(keys %leases)) {
my $name = $network;
$name =~ s/_/\./g;
$name =~ s/\.\./\//g;
print "$network.info $name\n";
$name =~ s/($FILTER)//;
print "$network.label $name\n";
print "$network.min 0\n";
print "$network.max " . $limits{$network} ."\n"
if ($limits{$network} > 0);
my $warn = int($limits{$network} * 0.9);
my $crit = int($limits{$network} * 0.95);
print "$network.warning $warn\n" if $warn;
print "$network.critical $crit\n" if $crit;
}
exit 0;
}
parseleases();
foreach my $network (sort(keys %leases)) {
print "$network.value ".$leases{$network}."\n";
}
sub parseconfig {
my($configfile) = @_;
local(*IN);
open(IN, "<$configfile") or exit 4;
my $name = undef;
LINE: while(<IN>) {
if(/subnet\s+((?:\d+\.){3}\d+)\s+netmask\s+((?:\d+\.){3}\d+)/ && ! /^\s*#/) {
$name = &initnet($1,$2);
print "# DEBUG: Found a subnet: $name\n" if $DEBUG;
}
if($name && /^\}$/) {
if(!exists $limits{$name}) {
print "# DEBUG: End of subnet... NO RANGE?\n" if $DEBUG;
delete($leases{$name});
}
$name = "";
}
if($name && /range\s+((?:\d+\.){3}\d+)\s+((?:\d+\.){3}\d+)/) {
print "# DEBUG: range $1 -> $2\n" if $DEBUG;
$limits{$name} += &rangecount($1, $2);
print "# DEBUG: limit for $name is " . $limits{$name} . "\n" if $DEBUG;
}
if(/^include \"([^\"]+)\";/) {
my $includefile = $1;
print "# DEBUG: found included file: $includefile\n" if $DEBUG;
if(!-f $includefile) {
$includefile = dirname($CONFIGFILE) . "/" . $includefile;
if(!-f $includefile) {
next LINE;
}
}
parseconfig($includefile);
}
}
close(IN);
}
sub rangecount {
my ($from, $to) = @_;
$from = ((new Net::IP($from))->intip())->numify();
$to = ((new Net::IP($to))->intip())->numify();
if($from < $to) {
return ($to - $from) + 1;
} else {
return ($from - $to) + 1;
}
}
sub parseleases {
my $ip = 0;
my $abandon = 0;
my $time = time();
open(IN, "<$LEASEFILE") or exit 4;
while(<IN>) {
if(/lease\s+(\d+\.\d+\.\d+\.\d+)\s+\{/) {
print "# DEBUG: in $1\n" if $DEBUG;
$ip = $1;
}
if($ip && /ends\s+\d+\s+([^;]+);/) {
# 2037/12/31 23:59:59 is max date on perl <= 5.6
print "# DEBUG: end $1\n" if $DEBUG;
my $end = HTTP::Date::str2time($1, "GMT");
# we asume that missing $end is valid due to
# restrictions in Time::Local on perl <= 5.6
if($end && $end < $time) {
print "# DEBUG: old $end $time:(\n" if $DEBUG;
$abandon = 1;
}
}
if($ip && /^\s*abandoned;$/) {
print "# DEBUG: abandoned\n" if $DEBUG;
$abandon = 1;
}
if($ip && /^\s*\}\s*$/) {
my $net = checkip($ip);
if($net && !$abandon) {
if(!counted($ip)) {
$leases{$net}++;
}
}
$abandon = 0;
$ip = 0;
print "# DEBUG: out\n" if $DEBUG;
}
}
close(IN);
}
sub initnet {
my ($net, $mask) = @_;
my $block = new Net::Netmask($net, $mask);
$networks{$block->desc()} = $block;
my $name = $block->desc();
$name =~ s/\//__/g;
$name =~ s/\./_/g;
$leases{$name} = 0;
$limits{$name} = 0;
return $name;
}
sub checkip {
my ($ip) = @_;
foreach my $block (keys %networks) {
if($networks{$block}->match($ip)) {
my $name = $block;
$name =~ s/\//__/g;
$name =~ s/\./_/g;
return $name;
}
}
return 0;
}
sub counted {
my ($ip) = @_;
if($ips{$ip}) {
print "# DEBUG: $ip already counted\n" if $DEBUG;
return 1;
}
$ips{$ip} = $ip;
print "# DEBUG: Counted $ip once!\n" if $DEBUG;
return 0;
}
exit();
|