root / plugins / dhcp / dhcp-pool @ d2161137
Historique | Voir | Annoter | Télécharger (5,47 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# |
| 3 |
# Copyright (C) 2008 Rien Broekstra <rien@rename-it.nl> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License |
| 7 |
# as published by the Free Software Foundation; version 2 dated June, |
| 8 |
# 1991. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 |
# |
| 19 |
# |
| 20 |
# Munin plugin to measure saturation of DHCP pools. |
| 21 |
# |
| 22 |
# Configuration variables: |
| 23 |
# |
| 24 |
# conffile - path to dhcpd's configuration file (default "/etc/dhcpd.conf") |
| 25 |
# leasefile - path to dhcpd's leases file (default "/var/lib/dhcp/dhcpd.leases") |
| 26 |
# |
| 27 |
# Parameters: |
| 28 |
# |
| 29 |
# config (required) |
| 30 |
# |
| 31 |
# Version 1.0, 2-12-2008 |
| 32 |
|
| 33 |
use POSIX; |
| 34 |
use Time::Local; |
| 35 |
use strict; |
| 36 |
|
| 37 |
my $CONFFILE = exists $ENV{'conffile'} ? $ENV{'conffile'} : "/etc/dhcpd.conf";
|
| 38 |
my $LEASEFILE = exists $ENV{'leasefile'} ? $ENV{'leasefile'} : "/var/lib/dhcp/dhcpd.leases";
|
| 39 |
|
| 40 |
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
|
| 41 |
|
| 42 |
} |
| 43 |
elsif ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
|
| 44 |
my (%pools, $start, $label); |
| 45 |
|
| 46 |
# Print general information |
| 47 |
print "graph_title DHCP pool usage (in %)\n"; |
| 48 |
print "graph_args --upper-limit 100 -l 0\n"; |
| 49 |
print "graph_vlabel %\n"; |
| 50 |
print "graph_category network\n"; |
| 51 |
|
| 52 |
# Determine the available IP pools |
| 53 |
%pools = determine_pools(); |
| 54 |
|
| 55 |
# Print a label for each pool |
| 56 |
foreach $start (keys %pools) {
|
| 57 |
$label = ip2string($start); |
| 58 |
$label =~ s/\./\_/g; |
| 59 |
print "$label.label Pool ".ip2string($start)."\n"; |
| 60 |
print "$label.warning 75\n"; |
| 61 |
print "$label.critical 100\n"; |
| 62 |
} |
| 63 |
} |
| 64 |
else {
|
| 65 |
my (@activeleases, %pools, $start, $end, $size, $free, $label, $lease); |
| 66 |
|
| 67 |
# Determine all leased IP addresses |
| 68 |
@activeleases = determine_active_leases(); |
| 69 |
|
| 70 |
# Determine the available IP pools |
| 71 |
%pools = determine_pools(); |
| 72 |
|
| 73 |
# For each pool, count how many leases from that pool are currently active |
| 74 |
foreach $start (keys %pools) {
|
| 75 |
$size = $pools{$start};
|
| 76 |
$end = $start+$size-1; |
| 77 |
$free = $size; |
| 78 |
|
| 79 |
foreach $lease (@activeleases) {
|
| 80 |
if ($lease >= $start && $lease <= $end) {
|
| 81 |
$free--; |
| 82 |
} |
| 83 |
} |
| 84 |
$label = ip2string($start); |
| 85 |
$label =~ s/\./\_/g; |
| 86 |
print "$label.value ".sprintf("%.1f", 100*($size-$free)/$size)."\n";
|
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
# Parse dhcpd.conf for range statements. |
| 91 |
# |
| 92 |
# Returns a hash with start IP -> size |
| 93 |
sub determine_pools {
|
| 94 |
my (%pools, @conffile, $line, $start, $end, $size); |
| 95 |
|
| 96 |
open(CONFFILE, "<${CONFFILE}") || exit -1;
|
| 97 |
@conffile = <CONFFILE>; |
| 98 |
close (CONFFILE); |
| 99 |
|
| 100 |
foreach $line (@conffile) {
|
| 101 |
next if $line =~ /^\s*#/; |
| 102 |
|
| 103 |
if ($line =~ /range[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)/) {
|
| 104 |
$start = string2ip($1); |
| 105 |
$end = string2ip($2); |
| 106 |
|
| 107 |
defined($start) || next; |
| 108 |
defined($end) || next; |
| 109 |
|
| 110 |
# The range statement gives the lowest and highest IP addresses in a range. |
| 111 |
$size = $end - $start + 1; |
| 112 |
|
| 113 |
$pools{$start} = $size;
|
| 114 |
} |
| 115 |
} |
| 116 |
return %pools; |
| 117 |
} |
| 118 |
|
| 119 |
# Very simple parser for dhcpd.leases. This will break very easily if dhcpd decides to |
| 120 |
# format the file differently. Ideally a simple recursive-descent parser should be used. |
| 121 |
# |
| 122 |
# Returns an array with currently leased IP's |
| 123 |
sub determine_active_leases {
|
| 124 |
my (@leasefile, $startdate, $enddate, $lease, @activeleases, $mytz, $line, %saw); |
| 125 |
|
| 126 |
open(LEASEFILE, "<${LEASEFILE}") || exit -1;
|
| 127 |
@leasefile = <LEASEFILE>; |
| 128 |
close (LEASEFILE); |
| 129 |
|
| 130 |
@activeleases = (); |
| 131 |
|
| 132 |
# Portable way of converting a GMT date/time string to timestamp is setting TZ to UTC, and then calling mktime() |
| 133 |
$mytz = $ENV{'TZ'};
|
| 134 |
$ENV{'TZ'} = 'UTC 0';
|
| 135 |
tzset(); |
| 136 |
|
| 137 |
foreach $line (@leasefile) {
|
| 138 |
if ($line =~ /lease ([\d]+\.[\d]+\.[\d]+\.[\d]+)/) {
|
| 139 |
$lease = string2ip($1); |
| 140 |
defined($lease) || next; |
| 141 |
|
| 142 |
undef $startdate; |
| 143 |
undef $enddate; |
| 144 |
} |
| 145 |
elsif ($line =~ /starts \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/) {
|
| 146 |
$startdate = mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0); |
| 147 |
} |
| 148 |
elsif ($line =~ /ends \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/) {
|
| 149 |
$enddate = mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0); |
| 150 |
if (defined($enddate) && defined($startdate) && defined($lease)) {
|
| 151 |
if ($startdate < time() && $enddate > time()) {
|
| 152 |
push (@activeleases, $lease); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
# Set TZ back to its original setting |
| 160 |
if (defined($mytz)) {
|
| 161 |
$ENV{'TZ'} = $mytz;
|
| 162 |
} |
| 163 |
else {
|
| 164 |
delete $ENV{'TZ'};
|
| 165 |
} |
| 166 |
tzset(); |
| 167 |
|
| 168 |
# Sort the array, strip doubles, and return |
| 169 |
return grep(!$saw{$_}++, @activeleases);
|
| 170 |
} |
| 171 |
|
| 172 |
# |
| 173 |
# Helper routine to convert an IP address a.b.c.d into an integer |
| 174 |
# |
| 175 |
# Returns an integer representation of an IP address |
| 176 |
sub string2ip {
|
| 177 |
my $string = shift; |
| 178 |
defined($string) || return undef; |
| 179 |
if ($string =~ /([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)/) {
|
| 180 |
if ($1 < 0 || $1 > 255 || $2 < 0 || $2 > 255 || $3 < 0 || $3 > 255 || $4 < 0 || $4 > 255) {
|
| 181 |
return undef; |
| 182 |
} |
| 183 |
else {
|
| 184 |
return $1 << 24 | $2 << 16 | $3 << 8 | $4; |
| 185 |
} |
| 186 |
} |
| 187 |
return undef; |
| 188 |
} |
| 189 |
|
| 190 |
# |
| 191 |
# Returns a dotted quad notation of an |
| 192 |
# |
| 193 |
sub ip2string {
|
| 194 |
my $ip = shift; |
| 195 |
defined ($ip) || return undef; |
| 196 |
return sprintf ("%d.%d.%d.%d", ($ip >> 24) & 0xff, ($ip >> 16) & 0xff, ($ip >> 8) & 0xff, $ip & 0xff);
|
| 197 |
} |
