Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_dhcp_leases.php
5
	Copyright (C) 2004 Scott Ullrich
6
	All rights reserved.
7

    
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
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	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

    
22
	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

    
36
$pgtitle = "Status: DHCP leases";
37
include("head.inc");
38

    
39
?>
40

    
41
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
42
<script src="/javascript/sorttable.js"></script>
43
<?php include("fbegin.inc"); ?>
44
<p class="pgtitle"><?=$pgtitle?></p>
45
<?php
46

    
47
function leasecmp($a, $b) {
48
        return strcmp($a[$_GET['order']], $b[$_GET['order']]);
49
}
50

    
51
function adjust_gmt($dt) {
52
        $ts = strtotime($dt . " GMT");
53
        return strftime("%Y/%m/%d %H:%M:%S", $ts);
54
}
55

    
56
function remove_duplicate($array, $field)
57
{
58
  foreach ($array as $sub)
59
   $cmp[] = $sub[$field];
60
  $unique = array_unique($cmp);
61
  foreach ($unique as $k => $rien)
62
   $new[] = $array[$k];
63
  return $new;
64
}
65

    
66
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
67
$awk = "/usr/bin/awk";
68
/* this pattern sticks comments into a single array item */
69
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
70
/* We then split the leases file by } */
71
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
72

    
73
/* stuff the leases file in a proper format into a array by line */
74
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
75
$leases_count = count($leases_content);
76

    
77
exec("/usr/sbin/arp -an", $rawdata);
78
$arpdata = array();
79
foreach ($rawdata as $line) {
80
	$elements = explode(' ',$line);
81
	if ($elements[3] != "(incomplete)") {
82
		$arpent = array();
83
		$arpent['ip'] = trim(str_replace(array('(',')'),'',$elements[1]));
84
		// $arpent['mac'] = trim($elements[3]);
85
		// $arpent['interface'] = trim($elements[5]);
86
	$arpdata[] = $arpent['ip'];
87
	}
88
}
89

    
90
$pools = array();
91
$leases = array();
92
$i = 0;
93
$l = 0;
94
$p = 0;
95

    
96
// Put everything together again
97
while($i < $leases_count) {
98
	/* split the line by space */
99
	$data = explode(" ", $leases_content[$i]);
100
	/* walk the fields */
101
	$f = 0;
102
	$fcount = count($data);
103
	/* with less then 20 fields there is nothing useful */
104
	if($fcount < 20) {
105
		$i++;
106
		continue;
107
	}
108
	while($f < $fcount) {
109
		switch($data[$f]) {
110
			case "failover":
111
				$pools[$p]['name'] = $data[$f+2];
112
				$pools[$p]['mystate'] = $data[$f+7];
113
				$pools[$p]['peerstate'] = $data[$f+14];
114
				$pools[$p]['mydate'] = $data[$f+10];
115
				$pools[$p]['mydate'] .= " " . $data[$f+11];
116
				$pools[$p]['peerdate'] = $data[$f+17];
117
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
118
				$p++;
119
				$i++;
120
				continue 3;
121
			case "lease":
122
				$leases[$l]['ip'] = $data[$f+1];
123
				$leases[$l]['type'] = "dynamic";
124
				$f = $f+2;
125
				break;
126
			case "starts":
127
				$leases[$l]['start'] = $data[$f+2];
128
				$leases[$l]['start'] .= " " . $data[$f+3];
129
				$f = $f+3;
130
				break;
131
			case "ends":
132
				$leases[$l]['end'] = $data[$f+2];
133
				$leases[$l]['end'] .= " " . $data[$f+3];
134
				$f = $f+3;
135
				break;
136
			case "tstp":
137
				$f = $f+3;
138
				break;
139
			case "tsfp":
140
				$f = $f+3;
141
				break;
142
			case "atsfp":
143
				$f = $f+3;
144
				break;
145
			case "cltt":
146
				$f = $f+3;
147
				break;
148
			case "binding":
149
				switch($data[$f+2]) {
150
					case "active":
151
						$leases[$l]['act'] = "active";
152
						break;
153
					case "free":
154
						$leases[$l]['act'] = "expired";
155
						$leases[$l]['online'] = "offline";
156
						break;
157
					case "backup":
158
						$leases[$l]['act'] = "reserved";
159
						$leases[$l]['online'] = "offline";
160
						break;
161
				}
162
				$f = $f+1;
163
				break;
164
			case "next":
165
				/* skip the next binding statement */
166
				$f = $f+3;
167
				break;
168
			case "hardware":
169
				$leases[$l]['mac'] = $data[$f+2];
170
				/* check if it's online and the lease is active */
171
				if (in_array($leases[$l]['ip'], $arpdata)) {
172
					$leases[$l]['online'] = 'online';
173
				} else {
174
					$leases[$l]['online'] = 'offline';
175
				}
176
				$f = $f+2;
177
				break;
178
			case "client-hostname":
179
				if($data[$f+1] <> "") {
180
					$leases[$l]['hostname'] = preg_replace('/"/','',$data[$f+1]);
181
				} else {
182
					$hostname = gethostbyaddr($leases[$l]['ip']);
183
					if($hostname <> "") {
184
						$leases[$l]['hostname'] = $hostname;
185
					}
186
				}
187
				$f = $f+1;
188
				break;
189
			case "uid":
190
				$f = $f+1;
191
				break;
192
		}
193
		$f++;
194
	}
195
	$l++;
196
	$i++;
197
}
198

    
199
/* remove duplicate items by mac address */
200
if(count($leases) > 0) {
201
	$leases = remove_duplicate($leases,"ip");
202
}
203

    
204
if(count($pools) > 0) {
205
	$pools = remove_duplicate($pools,"name");
206
	asort($pools);
207
}
208

    
209
foreach($config['interfaces'] as $ifname => $ifarr) {
210
	if (is_array($config['dhcpd'][$ifname]['staticmap'])) {
211
		foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
212
			$slease = array();
213
			$slease['ip'] = $static['ipaddr'];
214
			$slease['type'] = "static";
215
			$slease['mac'] = $static['mac'];
216
			$slease['start'] = gmdate("M d Y H:i:s", time());
217
			$slease['end'] = gmdate("M d Y H:i:s", time());
218
			$slease['end'] = gmdate("M d Y H:i:s", strtotime('+5 minutes'));
219
			$slease['hostname'] = $static['hostname'];
220
			$slease['act'] = "static";
221
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$slease['mac']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
222
			if ($online == 1) {
223
				$slease['online'] = 'online';
224
			} else {
225
				$slease['online'] = 'offline';
226
			}
227
			$leases[] = $slease;
228
		}
229
	}
230
}
231

    
232
if ($_GET['order'])
233
	usort($leases, "leasecmp");
234

    
235
?>
236

    
237
<?php
238
/* only print pool status when we have one */
239
if(count($pools) > 0) {
240
?>
241
<table class="sortable" id="sortabletable" name="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
242
  <tr>
243
    <td class="listhdrr">Failover Group</a></td>
244
    <td class="listhdrr">My State</a></td>
245
    <td class="listhdrr">Since</a></td>
246
    <td class="listhdrr">Peer State</a></td>
247
    <td class="listhdrr">Since</a></td>
248
  </tr>
249
<?php
250
foreach ($pools as $data) {
251
	echo "<tr>\n";
252
	echo "<td class=\"listlr\">{$fspans}{$data['name']}{$fspane}&nbsp;</td>\n";
253
	echo "<td class=\"listr\">{$fspans}{$data['mystate']}{$fspane}&nbsp;</td>\n";
254
	echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['mydate']) . "{$fspane}&nbsp;</td>\n";
255
	echo "<td class=\"listr\">{$fspans}{$data['peerstate']}{$fspane}&nbsp;</td>\n";
256
	echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['peerdate']) . "{$fspane}&nbsp;</td>\n";
257
	echo "<td class=\"list\" valign=\"middle\" width=\"17\">&nbsp;</td>\n";
258
	echo "<td class=\"list\" valign=\"middle\" width=\"17\">&nbsp;</td>\n";
259
	echo "</tr>\n";
260
}
261

    
262
?>
263
</table>
264

    
265
<?php
266
/* only print pool status when we have one */
267
}
268
?>
269

    
270
<p>
271

    
272
<table class="sortable" id="sortabletable" name="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
273
  <tr>
274
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP address</a></td>
275
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC address</a></td>
276
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
277
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
278
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
279
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=online">Online</a></td>
280
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=act">Lease Type</a></td>
281
	</tr>
282
<?php
283
foreach ($leases as $data) {
284
	if (($data['act'] == "active") || ($data['act'] == "static") || ($_GET['all'] == 1)) {
285
		if ($data['act'] != "active" && $data['act'] != "static") {
286
			$fspans = "<span class=\"gray\">";
287
			$fspane = "</span>";
288
		} else {
289
			$fspans = $fspane = "";
290
		}
291
                $lip = ip2long($data['ip']);
292
		if ($data['act'] == "static") {
293
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
294
				if(is_array($dhcpifconf['staticmap'])) {
295
					foreach ($dhcpifconf['staticmap'] as $staticent) {
296
						if ($data['ip'] == $staticent['ipaddr']) {
297
							$data['if'] = $dhcpif;
298
							break;
299
						}
300
					}
301
				}
302
				/* exit as soon as we have an interface */
303
				if ($data['if'] != "")
304
					break;
305
			}
306
		} else {
307
                	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
308
                        	if (($lip >= ip2long($dhcpifconf['range']['from'])) && ($lip <= ip2long($dhcpifconf['range']['to']))) {
309
                                	$data['if'] = $dhcpif;
310
                                	break;
311
                        	}
312
			}
313
                }		
314
		echo "<tr>\n";
315
                echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
316
                if ($data['online'] != "online") {
317
                        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";
318
                } else {
319
                	echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
320
                }
321
                echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
322
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['start']) . "{$fspane}&nbsp;</td>\n";
323
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) . "{$fspane}&nbsp;</td>\n";
324
                echo "<td class=\"listr\">{$fspans}{$data['online']}{$fspane}&nbsp;</td>\n";
325
                echo "<td class=\"listr\">{$fspans}{$data['act']}{$fspane}&nbsp;</td>\n";
326
		
327
		if ($data['type'] == "dynamic") {
328
                	echo "<td class=\"list\" valign=\"middle\"><a href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}&hostname={$data['hostname']}\">";
329
			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";
330
		} else {
331
                	echo "<td class=\"list\" valign=\"middle\">";
332
			echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus_mo.gif\" width=\"17\" height=\"17\" border=\"0\"></td>\n";
333
		}
334

    
335
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
336
		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";
337
                echo "</tr>\n";
338
	}
339
}
340

    
341
?>
342
</table>
343
<p>
344
<form action="diag_dhcp_leases.php" method="GET">
345
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
346
<?php if ($_GET['all']): ?>
347
<input type="hidden" name="all" value="0">
348
<input type="submit" class="formbtn" value="Show active and static leases only">
349
<?php else: ?>
350
<input type="hidden" name="all" value="1">
351
<input type="submit" class="formbtn" value="Show all configured leases">
352
<?php endif; ?>
353
</form>
354
<?php if($leases == 0): ?>
355
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
356
<?php endif; ?>
357

    
358
<meta http-equiv="refresh" content="120;url=<?php print $_SERVER['SCRIPT_NAME']; ?>">
359

    
360
<?php include("fend.inc"); ?>
361
</body>
362
</html>
(8-8/184)