Project

General

Profile

Download (9.3 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['vardb_path']}/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]}| /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
		$i++;
168
}
169
foreach($config['interfaces'] as $ifname => $ifarr) {
170
	if (is_array($config['dhcpd'][$ifname]['staticmap'])) {
171
		foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
172
			$slease = array();
173
			$slease['ip'] = $static['ipaddr'];
174
			$slease['type'] = "static";
175
			$slease['mac'] = $static['mac'];
176
			$slease['start'] = gmdate("M d Y H:i:s", time());
177
			$slease['end'] = gmdate("M d Y H:i:s", time());
178
			$slease['end'] = gmdate("M d Y H:i:s", strtotime('+5 minutes'));
179
			$slease['hostname'] = $static['descr'];
180
			$slease['act'] = "static";
181
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$slease['mac']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
182
			if ($online == 1) {
183
				$slease['online'] = 'online';
184
			} else {
185
				$slease['online'] = 'offline';
186
			}
187
			$leases[] = $slease;
188
		}
189
	}
190
}
191

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

    
242
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
243
		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";
244
                echo "</tr>\n";
245
	}
246
}
247
?>
248
</table>
249
<p>
250
<form action="diag_dhcp_leases.php" method="GET">
251
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
252
<?php if ($_GET['all']): ?>
253
<input type="hidden" name="all" value="0">
254
<input type="submit" class="formbtn" value="Show active and static leases only">
255
<?php else: ?>
256
<input type="hidden" name="all" value="1">
257
<input type="submit" class="formbtn" value="Show all configured leases">
258
<?php endif; ?>
259
</form>
260
<?php else: ?>
261
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
262
<?php endif; ?>
263

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

    
266
<?php include("fend.inc"); ?>
267
</body>
268
</html>
(8-8/155)