Project

General

Profile

Download (10.1 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 = "Diagnostics: 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
flush();
48

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

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

    
58
$fp = @fopen("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases","r");
59

    
60
if ($fp):
61

    
62
$return = array();
63

    
64
while ($line = fgets($fp)) {
65
	$matches = "";
66

    
67
	// Sort out comments
68
	// C-style comments not supported!
69
	if (preg_match("/^\s*[\r|\n]/", $line, $matches[0]) ||
70
				preg_match("/^([^\"#]*)#.*$/", $line, $matches[1]) ||
71
				preg_match("/^([^\"]*)\/\/.*$/", $line, $matches[2]) ||
72
				preg_match("/\s*#(.*)/", $line, $matches[3]) ||
73
				preg_match("/\\\"\176/", $line, $matches[4])
74
		) {
75
		$line = "";
76
		continue;
77
	}
78

    
79
	if (preg_match("/(.*)#(.*)/", $line, $matches))
80
		$line = $matches[0];
81

    
82
	// Tokenize lines
83
	do {
84
		if (preg_match("/^\s*\"([^\"]*)\"(.*)$/", $line, $matches)) {
85
			$line = $matches[2];
86
			$return[] = array($matches[1], 0);
87
		} else if (preg_match("/^\s*([{};])(.*)$/", $line, $matches)) {
88
			$line = $matches[2];
89
			$return[] = array($matches[0], 1);
90
		} else if (preg_match("/^\s*([^{}; \t]+)(.*)$/", $line, $matches)) {
91
			$line = $matches[2];
92
			$return[] = array($matches[1], 0);
93
		} else
94
			break;
95

    
96
	} while($line);
97

    
98
	$lines++;
99
}
100

    
101
fclose($fp);
102

    
103
$leases = array();
104
$i = 0;
105

    
106
// Put everything together again
107
while ($data = array_shift($return)) {
108
	if ($data[0] == "next") {
109
		$d = array_shift($return);
110
	}
111
	if ($data[0] == "lease") {
112
		$d = array_shift($return);
113
		$leases[$i]['ip'] = $d[0];
114
		$leases[$i]['type'] = "dynamic";
115
	}
116
	if ($data[0] == "client-hostname") {
117
		$d = array_shift($return);
118
		if($d[0] <> "") {
119
			$leases[$i]['hostname'] = $d[0];
120
		} else {
121
			if(gethostbyaddr($leases[$i]['ip']) <> "") {
122
				$leases[$i]['hostname'] = gethostbyaddr($leases[$i]['ip']);
123
			}
124
		}
125
	}	
126
	if ($data[0] == "hardware") {
127
		$d = array_shift($return);
128
		if ($d[0] == "ethernet") {
129
			$d = array_shift($return);
130
			$leases[$i]['mac'] = $d[0];
131
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$d[0]}| grep {$leases[$i]['ip']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
132
			if ($online == 1) {
133
				$leases[$i]['online'] = 'online';
134
			} else {
135
				$leases[$i]['online'] = 'offline';
136
			}
137
		}
138
	} else if ($data[0] == "starts") {
139
		$d = array_shift($return);
140
		$d = array_shift($return);
141
		$leases[$i]['start'] = $d[0];
142
		$d = array_shift($return);
143
		$leases[$i]['start'] .= " " . $d[0];
144
	} else if ($data[0] == "ends") {
145
		$d = array_shift($return);
146
		$d = array_shift($return);
147
		$leases[$i]['end'] = $d[0];
148
		$d = array_shift($return);
149
		$leases[$i]['end'] .= " " . $d[0];
150
	} else if ($data[0] == "binding") {
151
		$d = array_shift($return);
152
		if ($d[0] == "state") {
153
			$d = array_shift($return);
154
			switch($d[0]) {
155
				case 'active':
156
					$leases[$i]['act'] = 'active';
157
					break;
158
				case 'free':
159
					$leases[$i]['act'] = 'expired';
160
					break;
161
				case 'backup':
162
					$leases[$i]['act'] = 'reserved';	
163
					$leases[$i]['online'] = 'offline';
164
					break;
165
			}
166
		}
167
	} else if (($data[0] == "}") && ($data[1] == 1)) {		// End of group
168

    
169
		//updated leases are appended to the end of the lease file. if we aren't
170
		//showing everything just show the most current lease in the list
171
		if(!$_GET['all']) {
172
			$l = $leases[$i]['ip'];
173
			for($k = 0; $k < $i; $k++) {
174
				if($leases[$k]['ip'] == $l) {
175
					array_splice($leases, $k, 1);
176
					--$i;
177
					break;
178
				}
179
			}
180
		}
181
		$i++;
182
	}
183
}
184
foreach($config['interfaces'] as $ifname => $ifarr) {
185
	if (is_array($config['dhcpd'][$ifname]['staticmap'])) {
186
		foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
187
			$slease = array();
188
			$slease['ip'] = $static['ipaddr'];
189
			$slease['type'] = "static";
190
			$slease['mac'] = $static['mac'];
191
			$slease['start'] = gmdate("M d Y H:i:s", time());
192
			$slease['end'] = gmdate("M d Y H:i:s", time());
193
			$slease['end'] = gmdate("M d Y H:i:s", strtotime('+5 minutes'));
194
			$slease['hostname'] = $static['descr'];
195
			$slease['act'] = "static";
196
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$slease['mac']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
197
			if ($online == 1) {
198
				$slease['online'] = 'online';
199
			} else {
200
				$slease['online'] = 'offline';
201
			}
202
			$leases[] = $slease;
203
		}
204
	}
205
}
206

    
207
if ($_GET['order'])
208
	usort($leases, "leasecmp");
209
?>
210
<table class="sortable" id="sortabletable" name="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
211
  <tr>
212
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP address</a></td>
213
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC address</a></td>
214
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
215
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
216
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
217
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=online">Online</a></td>
218
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=act">Lease Type</a></td>
219
	</tr>
220
<?php
221
foreach ($leases as $data) {
222
	if (($data['act'] == "active") || ($data['act'] == "static") || ($_GET['all'] == 1)) {
223
		if ($data['act'] != "active" && $data['act'] != "static") {
224
			$fspans = "<span class=\"gray\">";
225
			$fspane = "</span>";
226
		} else {
227
			$fspans = $fspane = "";
228
		}
229
                $lip = ip2long($data['ip']);
230
		if ($data['act'] == "static") {
231
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
232
				foreach ($dhcpifconf['staticmap'] as $staticent) {
233
					if ($data['ip'] == $staticent['ipaddr']) {
234
						$data['if'] = $dhcpif;
235
						break;
236
					}
237
				}
238
				/* exit as soon as we have an interface */
239
				if ($data['if'] != "")
240
					break;
241
			}
242
		} else {
243
                	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
244
                        	if (($lip >= ip2long($dhcpifconf['range']['from'])) && ($lip <= ip2long($dhcpifconf['range']['to']))) {
245
                                	$data['if'] = $dhcpif;
246
                                	break;
247
                        	}
248
			}
249
                }		
250
		echo "<tr>\n";
251
                echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
252
                if ($data['online'] != "online") {
253
                        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";
254
                } else {
255
                	echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
256
                }
257
                echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
258
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['start']) . "{$fspane}&nbsp;</td>\n";
259
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) . "{$fspane}&nbsp;</td>\n";
260
                echo "<td class=\"listr\">{$fspans}{$data['online']}{$fspane}&nbsp;</td>\n";
261
                echo "<td class=\"listr\">{$fspans}{$data['act']}{$fspane}&nbsp;</td>\n";
262
		
263
		if ($data['type'] == "dynamic") {
264
                	echo "<td class=\"list\" valign=\"middle\"><a href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
265
			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";
266
		} else {
267
                	echo "<td class=\"list\" valign=\"middle\">";
268
			echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus_mo.gif\" width=\"17\" height=\"17\" border=\"0\"></td>\n";
269
		}
270

    
271
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
272
		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";
273
                echo "</tr>\n";
274
	}
275
}
276
?>
277
</table>
278
<p>
279
<form action="diag_dhcp_leases.php" method="GET">
280
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
281
<?php if ($_GET['all']): ?>
282
<input type="hidden" name="all" value="0">
283
<input type="submit" class="formbtn" value="Show active and static leases only">
284
<?php else: ?>
285
<input type="hidden" name="all" value="1">
286
<input type="submit" class="formbtn" value="Show all configured leases">
287
<?php endif; ?>
288
</form>
289
<?php else: ?>
290
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
291
<?php endif; ?>
292

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

    
295
<?php include("fend.inc"); ?>
296
</body>
297
</html>
(8-8/164)