root / plugins / network / dhcp-pool @ dd4afac8
Historique | Voir | Annoter | Télécharger (5,35 ko)
| 1 | 4dee276e | Rien Broekstra | #!/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; |
||
| 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 | if ($line =~ /range[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)/) {
|
||
| 102 | $start = string2ip($1); |
||
| 103 | $end = string2ip($2); |
||
| 104 | $size = $end - $start; |
||
| 105 | defined($start) || next; |
||
| 106 | defined($end) || next; |
||
| 107 | |||
| 108 | $pools{$start} = $size;
|
||
| 109 | } |
||
| 110 | } |
||
| 111 | return %pools; |
||
| 112 | } |
||
| 113 | |||
| 114 | # Very simple parser for dhcpd.leases. This will break very easily if dhcpd decides to |
||
| 115 | # format the file differently. Ideally a simple recursive-descent parser should be used. |
||
| 116 | # |
||
| 117 | # Returns an array with currently leased IP's |
||
| 118 | sub determine_active_leases {
|
||
| 119 | my (@leasefile, $startdate, $enddate, $lease, @activeleases, $mytz, $line, %saw); |
||
| 120 | |||
| 121 | open(LEASEFILE, "<${LEASEFILE}") || exit -1;
|
||
| 122 | @leasefile = <LEASEFILE>; |
||
| 123 | close (LEASEFILE); |
||
| 124 | |||
| 125 | @activeleases = (); |
||
| 126 | |||
| 127 | # Portable way of converting a GMT date/time string to timestamp is setting TZ to UTC, and then calling mktime() |
||
| 128 | $mytz = $ENV{'TZ'};
|
||
| 129 | $ENV{'TZ'} = 'UTC 0';
|
||
| 130 | tzset(); |
||
| 131 | |||
| 132 | foreach $line (@leasefile) {
|
||
| 133 | if ($line =~ /lease ([\d]+\.[\d]+\.[\d]+\.[\d]+)/) {
|
||
| 134 | $lease = string2ip($1); |
||
| 135 | defined($lease) || next; |
||
| 136 | |||
| 137 | undef $startdate; |
||
| 138 | undef $enddate; |
||
| 139 | } |
||
| 140 | elsif ($line =~ /starts \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/) {
|
||
| 141 | $startdate = mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0); |
||
| 142 | } |
||
| 143 | elsif ($line =~ /ends \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/) {
|
||
| 144 | $enddate = mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0); |
||
| 145 | if (defined($enddate) && defined($startdate) && defined($lease)) {
|
||
| 146 | if ($startdate < time() && $enddate > time()) {
|
||
| 147 | push (@activeleases, $lease); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | } |
||
| 153 | |||
| 154 | # Set TZ back to its original setting |
||
| 155 | if (defined($mytz)) {
|
||
| 156 | $ENV{'TZ'} = $mytz;
|
||
| 157 | } |
||
| 158 | else {
|
||
| 159 | delete $ENV{'TZ'};
|
||
| 160 | } |
||
| 161 | tzset(); |
||
| 162 | |||
| 163 | # Sort the array, strip doubles, and return |
||
| 164 | return grep(!$saw{$_}++, @activeleases);
|
||
| 165 | } |
||
| 166 | |||
| 167 | # |
||
| 168 | # Helper routine to convert an IP address a.b.c.d into an integer |
||
| 169 | # |
||
| 170 | # Returns an integer representation of an IP address |
||
| 171 | sub string2ip {
|
||
| 172 | my $string = shift; |
||
| 173 | defined($string) || return undef; |
||
| 174 | if ($string =~ /([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)/) {
|
||
| 175 | if ($1 < 0 || $1 > 255 || $2 < 0 || $2 > 255 || $3 < 0 || $3 > 255 || $4 < 0 || $4 > 255) {
|
||
| 176 | return undef; |
||
| 177 | } |
||
| 178 | else {
|
||
| 179 | return $1 << 24 | $2 << 16 | $3 << 8 | $4; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | return undef; |
||
| 183 | } |
||
| 184 | |||
| 185 | # |
||
| 186 | # Returns a dotted quad notation of an |
||
| 187 | # |
||
| 188 | sub ip2string {
|
||
| 189 | my $ip = shift; |
||
| 190 | defined ($ip) || return undef; |
||
| 191 | return sprintf ("%d.%d.%d.%d", ($ip >> 24) & 0xff, ($ip >> 16) & 0xff, ($ip >> 8) & 0xff, $ip & 0xff);
|
||
| 192 | } |
