File: dhcpd3.in

package info (click to toggle)
munin 1.2.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,940 kB
  • ctags: 98
  • sloc: sh: 4,215; makefile: 452; perl: 135
file content (197 lines) | stat: -rw-r--r-- 4,073 bytes parent folder | download | duplicates (3)
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
#!@@PERL@@ -w
#
# Plugin to monitor dhcp3 leases 
#
# Usage: copy or link into /etc/munin/plugins
#
# Parameters:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#
# Config variables:
#
#       user         - run by user that can read lease files [root]
#       leasefile    - What file to read leases from
#       configfile   - What file to read configuration from
#
# Requires:
#       Net::Netmask
#       HTTP::Date
#       
# $Log$
# Revision 1.1  2004/12/10 13:16:19  jimmyo
# Added plugin generic/dhcpd3, created by Rune N. Skillingstad.
#
#
#
# Magic markers (optinal - used by munin-config and some installation
# scripts):
#
#%# family=contrib
#%# capabilities=autoconf


my $ret = undef;

if(! eval "require Net::Netmask") {
    $ret = "Net::Netmask not found";
}
if(! eval "require HTTP::Date") {
    $ret = "HTTP::Date not found";
}

use strict;

my %leases   = ();
my %networks = ();
my %ips      = ();
my $DEBUG    = 0;

my $LEASEFILE  = $ENV{leasefile}  || "/var/lib/dhcp/dhcpd.leases";
my $CONFIGFILE = $ENV{configfile} || "/etc/dhcpd.conf";

if($ARGV[0] and $ARGV[0] eq "autoconf" ) {
    if($ret) {
	print "no ($ret)\n";
	exit 1;
    }
    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 gile not found)\n";
	    }
	} else {
	    print "no (leasefile not readable)\n";
	}
    } else {
	print "no (leasefile not found)\n";
    }
    exit 1;
}

if (! -f $LEASEFILE and ! -f $CONFIGFILE) {
    print "net.value U\n";
    exit 0;
}

if($DEBUG) {
    print "CONFIGFILE == $CONFIGFILE\nLEASEFILE == $LEASEFILE\n";
}

Net::Netmask->import();
HTTP::Date->import();

parseconfig();

if($ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title dhcp leases\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.label $name\n";
    }
    exit 0;
} 

parseleases();

foreach my $network (sort(keys %leases)) {
    print "$network.value ".$leases{$network}."\n";
}

sub parseconfig {
    open(IN, "<$CONFIGFILE") or exit 4;
    while(<IN>) {
	if(/subnet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask\s+(\d+\.\d+\.\d+\.\d+)/ && ! /^\s*#/) {
	    initnet($1,$2);
	}
    }
    close(IN);
}

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 "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 "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 "old $end $time:(\n" if $DEBUG;
		$abandon = 1;
	    }
	}
	if($ip && /^\s*abandoned;$/) {
	    print "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 "out\n\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;
}

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 "$ip already counted\n" if $DEBUG;
	return 1;
    }
    $ips{$ip} = $ip;
    print "Counted $ip once!\n" if $DEBUG;
    return 0;
}

exit();