Project

General

Profile

Download (9.67 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
<?php include("fbegin.inc"); ?>
43
<p class="pgtitle"><?=$pgtitle?></p>
44
<?php
45

    
46
flush();
47

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

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

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

    
59
if ($fp):
60

    
61
$return = array();
62

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

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

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

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

    
95
	} while($line);
96

    
97
	$lines++;
98
}
99

    
100
fclose($fp);
101

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

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

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

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

    
256
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
257
		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";
258
                echo "</tr>\n";
259
	}
260
}
261
?>
262
</table>
263
<p>
264
<form action="diag_dhcp_leases.php" method="GET">
265
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
266
<?php if ($_GET['all']): ?>
267
<input type="hidden" name="all" value="0">
268
<input type="submit" class="formbtn" value="Show active and static leases only">
269
<?php else: ?>
270
<input type="hidden" name="all" value="1">
271
<input type="submit" class="formbtn" value="Show all configured leases">
272
<?php endif; ?>
273
</form>
274
<?php else: ?>
275
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
276
<?php endif; ?>
277

    
278
<meta http-equiv="refresh" content="120;url=<?php print $_SERVER['PHP_SELF']; ?>">
279

    
280
<?php include("fend.inc"); ?>
281
</body>
282
</html>
(8-8/160)