Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
1 4d875b4f Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	diag_dhcp_leases.php
5 4d875b4f Scott Ullrich
	Copyright (C) 2004 Scott Ullrich
6 5b237745 Scott Ullrich
	All rights reserved.
7 4d875b4f Scott Ullrich
8
	originially part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14 4d875b4f Scott Ullrich
15 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17 4d875b4f Scott Ullrich
18 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21 4d875b4f Scott Ullrich
22 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
require("guiconfig.inc");
35 b63695db Scott Ullrich
36 d88c6a9f Scott Ullrich
$pgtitle = array("Status","DHCP leases");
37 b63695db Scott Ullrich
include("head.inc");
38
39 5b237745 Scott Ullrich
?>
40
41
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
42 5fd0b016 Scott Ullrich
<script src="/javascript/sorttable.js"></script>
43 5b237745 Scott Ullrich
<?php include("fbegin.inc"); ?>
44
<?php
45
46
function leasecmp($a, $b) {
47 5742ca5e Scott Ullrich
        return strcmp($a[$_GET['order']], $b[$_GET['order']]);
48
}
49
50
function adjust_gmt($dt) {
51
        $ts = strtotime($dt . " GMT");
52
        return strftime("%Y/%m/%d %H:%M:%S", $ts);
53 5b237745 Scott Ullrich
}
54
55 428d20c4 Seth Mos
function remove_duplicate($array, $field)
56
{
57
  foreach ($array as $sub)
58
   $cmp[] = $sub[$field];
59
  $unique = array_unique($cmp);
60
  foreach ($unique as $k => $rien)
61
   $new[] = $array[$k];
62
  return $new;
63 5b237745 Scott Ullrich
}
64
65 428d20c4 Seth Mos
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
66
$awk = "/usr/bin/awk";
67
/* this pattern sticks comments into a single array item */
68
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
69
/* We then split the leases file by } */
70 b4f7282c Seth Mos
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
71 5b237745 Scott Ullrich
72 428d20c4 Seth Mos
/* stuff the leases file in a proper format into a array by line */
73
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
74
$leases_count = count($leases_content);
75
76 468b0d65 Seth Mos
exec("/usr/sbin/arp -an", $rawdata);
77
$arpdata = array();
78
foreach ($rawdata as $line) {
79
	$elements = explode(' ',$line);
80
	if ($elements[3] != "(incomplete)") {
81
		$arpent = array();
82
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
83
		// $arpent['mac'] = trim($elements[3]);
84
		// $arpent['interface'] = trim($elements[5]);
85
	$arpdata[] = $arpent['ip'];
86
	}
87
}
88
89 428d20c4 Seth Mos
$pools = array();
90 5b237745 Scott Ullrich
$leases = array();
91
$i = 0;
92 b4f7282c Seth Mos
$l = 0;
93
$p = 0;
94 5b237745 Scott Ullrich
95
// Put everything together again
96 428d20c4 Seth Mos
while($i < $leases_count) {
97
	/* split the line by space */
98
	$data = explode(" ", $leases_content[$i]);
99
	/* walk the fields */
100
	$f = 0;
101
	$fcount = count($data);
102 b4f7282c Seth Mos
	/* with less then 20 fields there is nothing useful */
103
	if($fcount < 20) {
104
		$i++;
105
		continue;
106
	}
107 428d20c4 Seth Mos
	while($f < $fcount) {
108 b4f7282c Seth Mos
		switch($data[$f]) {
109
			case "failover":
110
				$pools[$p]['name'] = $data[$f+2];
111
				$pools[$p]['mystate'] = $data[$f+7];
112
				$pools[$p]['peerstate'] = $data[$f+14];
113
				$pools[$p]['mydate'] = $data[$f+10];
114
				$pools[$p]['mydate'] .= " " . $data[$f+11];
115
				$pools[$p]['peerdate'] = $data[$f+17];
116
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
117
				$p++;
118
				$i++;
119
				continue 3;
120
			case "lease":
121
				$leases[$l]['ip'] = $data[$f+1];
122
				$leases[$l]['type'] = "dynamic";
123
				$f = $f+2;
124
				break;
125
			case "starts":
126
				$leases[$l]['start'] = $data[$f+2];
127
				$leases[$l]['start'] .= " " . $data[$f+3];
128
				$f = $f+3;
129
				break;
130
			case "ends":
131
				$leases[$l]['end'] = $data[$f+2];
132
				$leases[$l]['end'] .= " " . $data[$f+3];
133
				$f = $f+3;
134
				break;
135
			case "tstp":
136
				$f = $f+3;
137
				break;
138
			case "tsfp":
139
				$f = $f+3;
140
				break;
141
			case "atsfp":
142
				$f = $f+3;
143
				break;
144
			case "cltt":
145
				$f = $f+3;
146
				break;
147
			case "binding":
148 428d20c4 Seth Mos
				switch($data[$f+2]) {
149 b4f7282c Seth Mos
					case "active":
150
						$leases[$l]['act'] = "active";
151 428d20c4 Seth Mos
						break;
152 b4f7282c Seth Mos
					case "free":
153
						$leases[$l]['act'] = "expired";
154
						$leases[$l]['online'] = "offline";
155 428d20c4 Seth Mos
						break;
156 b4f7282c Seth Mos
					case "backup":
157
						$leases[$l]['act'] = "reserved";
158
						$leases[$l]['online'] = "offline";
159 428d20c4 Seth Mos
						break;
160
				}
161 b4f7282c Seth Mos
				$f = $f+1;
162
				break;
163
			case "next":
164
				/* skip the next binding statement */
165
				$f = $f+3;
166
				break;
167
			case "hardware":
168
				$leases[$l]['mac'] = $data[$f+2];
169
				/* check if it's online and the lease is active */
170 468b0d65 Seth Mos
				if (in_array($leases[$l]['ip'], $arpdata)) {
171
					$leases[$l]['online'] = 'online';
172
				} else {
173
					$leases[$l]['online'] = 'offline';
174 14cf741d Scott Ullrich
				}
175 b4f7282c Seth Mos
				$f = $f+2;
176
				break;
177
			case "client-hostname":
178
				if($data[$f+1] <> "") {
179
					$leases[$l]['hostname'] = preg_replace('/"/','',$data[$f+1]);
180
				} else {
181
					$hostname = gethostbyaddr($leases[$l]['ip']);
182
					if($hostname <> "") {
183
						$leases[$l]['hostname'] = $hostname;
184
					}
185
				}
186
				$f = $f+1;
187
				break;
188
			case "uid":
189
				$f = $f+1;
190
				break;
191 14cf741d Scott Ullrich
		}
192 428d20c4 Seth Mos
		$f++;
193 14cf741d Scott Ullrich
	}
194 b4f7282c Seth Mos
	$l++;
195 428d20c4 Seth Mos
	$i++;
196 5b237745 Scott Ullrich
}
197 428d20c4 Seth Mos
198
/* remove duplicate items by mac address */
199 9255cbc5 Seth Mos
if(count($leases) > 0) {
200 b4f7282c Seth Mos
	$leases = remove_duplicate($leases,"ip");
201 9255cbc5 Seth Mos
}
202 b4f7282c Seth Mos
203 9255cbc5 Seth Mos
if(count($pools) > 0) {
204
	$pools = remove_duplicate($pools,"name");
205
	asort($pools);
206
}
207 428d20c4 Seth Mos
208 9252431c Scott Ullrich
foreach($config['interfaces'] as $ifname => $ifarr) {
209
	if (is_array($config['dhcpd'][$ifname]['staticmap'])) {
210
		foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
211
			$slease = array();
212
			$slease['ip'] = $static['ipaddr'];
213
			$slease['type'] = "static";
214
			$slease['mac'] = $static['mac'];
215
			$slease['start'] = gmdate("M d Y H:i:s", time());
216
			$slease['end'] = gmdate("M d Y H:i:s", time());
217
			$slease['end'] = gmdate("M d Y H:i:s", strtotime('+5 minutes'));
218 6a01ea44 Bill Marquette
			$slease['hostname'] = $static['hostname'];
219 9252431c Scott Ullrich
			$slease['act'] = "static";
220
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$slease['mac']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
221
			if ($online == 1) {
222
				$slease['online'] = 'online';
223
			} else {
224
				$slease['online'] = 'offline';
225
			}
226
			$leases[] = $slease;
227
		}
228
	}
229
}
230 5b237745 Scott Ullrich
231
if ($_GET['order'])
232
	usort($leases, "leasecmp");
233 428d20c4 Seth Mos
234
?>
235
236
<?php
237
/* only print pool status when we have one */
238
if(count($pools) > 0) {
239 5b237745 Scott Ullrich
?>
240 428d20c4 Seth Mos
<table class="sortable" id="sortabletable" name="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
241
  <tr>
242
    <td class="listhdrr">Failover Group</a></td>
243
    <td class="listhdrr">My State</a></td>
244
    <td class="listhdrr">Since</a></td>
245
    <td class="listhdrr">Peer State</a></td>
246
    <td class="listhdrr">Since</a></td>
247
  </tr>
248
<?php
249
foreach ($pools as $data) {
250
	echo "<tr>\n";
251
	echo "<td class=\"listlr\">{$fspans}{$data['name']}{$fspane}&nbsp;</td>\n";
252
	echo "<td class=\"listr\">{$fspans}{$data['mystate']}{$fspane}&nbsp;</td>\n";
253
	echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['mydate']) . "{$fspane}&nbsp;</td>\n";
254
	echo "<td class=\"listr\">{$fspans}{$data['peerstate']}{$fspane}&nbsp;</td>\n";
255
	echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['peerdate']) . "{$fspane}&nbsp;</td>\n";
256
	echo "<td class=\"list\" valign=\"middle\" width=\"17\">&nbsp;</td>\n";
257
	echo "<td class=\"list\" valign=\"middle\" width=\"17\">&nbsp;</td>\n";
258
	echo "</tr>\n";
259
}
260
261
?>
262
</table>
263
264
<?php
265
/* only print pool status when we have one */
266
}
267
?>
268
269
<p>
270
271 5fd0b016 Scott Ullrich
<table class="sortable" id="sortabletable" name="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
272 5b237745 Scott Ullrich
  <tr>
273
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP address</a></td>
274
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC address</a></td>
275
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
276
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
277 9252431c Scott Ullrich
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
278
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=online">Online</a></td>
279
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=act">Lease Type</a></td>
280 5b237745 Scott Ullrich
	</tr>
281
<?php
282
foreach ($leases as $data) {
283 9252431c Scott Ullrich
	if (($data['act'] == "active") || ($data['act'] == "static") || ($_GET['all'] == 1)) {
284
		if ($data['act'] != "active" && $data['act'] != "static") {
285 5b237745 Scott Ullrich
			$fspans = "<span class=\"gray\">";
286
			$fspane = "</span>";
287
		} else {
288
			$fspans = $fspane = "";
289
		}
290 69981800 Scott Ullrich
                $lip = ip2long($data['ip']);
291 5b2177e4 Scott Ullrich
		if ($data['act'] == "static") {
292
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
293 0e616855 Scott Ullrich
				if(is_array($dhcpifconf['staticmap'])) {
294
					foreach ($dhcpifconf['staticmap'] as $staticent) {
295
						if ($data['ip'] == $staticent['ipaddr']) {
296
							$data['if'] = $dhcpif;
297
							break;
298
						}
299 5b2177e4 Scott Ullrich
					}
300
				}
301
				/* exit as soon as we have an interface */
302
				if ($data['if'] != "")
303
					break;
304
			}
305
		} else {
306
                	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
307
                        	if (($lip >= ip2long($dhcpifconf['range']['from'])) && ($lip <= ip2long($dhcpifconf['range']['to']))) {
308
                                	$data['if'] = $dhcpif;
309
                                	break;
310
                        	}
311
			}
312 69981800 Scott Ullrich
                }		
313 5b237745 Scott Ullrich
		echo "<tr>\n";
314 5742ca5e Scott Ullrich
                echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
315 9252431c Scott Ullrich
                if ($data['online'] != "online") {
316 37c0c49b Scott Ullrich
                        echo "<td class=\"listr\">{$fspans}<a href=\"services_wol.php?if={$data['if']}&mac={$data['mac']}\" title=\"send Wake on Lan packet to mac\">{$data['mac']}</a>{$fspane}&nbsp;</td>\n";
317
                } else {
318 9252431c Scott Ullrich
                	echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
319 37c0c49b Scott Ullrich
                }
320 5742ca5e Scott Ullrich
                echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
321
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['start']) . "{$fspane}&nbsp;</td>\n";
322
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) . "{$fspane}&nbsp;</td>\n";
323 9252431c Scott Ullrich
                echo "<td class=\"listr\">{$fspans}{$data['online']}{$fspane}&nbsp;</td>\n";
324
                echo "<td class=\"listr\">{$fspans}{$data['act']}{$fspane}&nbsp;</td>\n";
325
		
326
		if ($data['type'] == "dynamic") {
327 6a01ea44 Bill Marquette
                	echo "<td class=\"list\" valign=\"middle\"><a href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}&hostname={$data['hostname']}\">";
328 9252431c Scott Ullrich
			echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a static mapping for this MAC address\"></a></td>\n";
329
		} else {
330
                	echo "<td class=\"list\" valign=\"middle\">";
331 b86e64c1 Scott Ullrich
			echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus_mo.gif\" width=\"17\" height=\"17\" border=\"0\"></td>\n";
332 9252431c Scott Ullrich
		}
333
334 7477df4f Scott Ullrich
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
335 37c0c49b Scott Ullrich
		echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_wol_all.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a Wake on Lan mapping for this MAC address\"></a></td>\n";
336 5742ca5e Scott Ullrich
                echo "</tr>\n";
337 5b237745 Scott Ullrich
	}
338
}
339 428d20c4 Seth Mos
340 5b237745 Scott Ullrich
?>
341
</table>
342
<p>
343
<form action="diag_dhcp_leases.php" method="GET">
344
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
345
<?php if ($_GET['all']): ?>
346
<input type="hidden" name="all" value="0">
347 9252431c Scott Ullrich
<input type="submit" class="formbtn" value="Show active and static leases only">
348 5b237745 Scott Ullrich
<?php else: ?>
349
<input type="hidden" name="all" value="1">
350 9252431c Scott Ullrich
<input type="submit" class="formbtn" value="Show all configured leases">
351 5b237745 Scott Ullrich
<?php endif; ?>
352
</form>
353 428d20c4 Seth Mos
<?php if($leases == 0): ?>
354 5b237745 Scott Ullrich
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
355
<?php endif; ?>
356 fda1fdae Scott Ullrich
357 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
358
</body>
359
</html>