Project

General

Profile

Download (12.9 KB) Statistics
| Branch: | Tag: | Revision:
1 e078c882 jim-p
<?php
2
/*
3 aaec5634 Renato Botelho
 * status_ntpd.php
4 191cb31d Stephen Beaver
 *
5 aaec5634 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 aaec5634 Renato Botelho
 * Copyright (c) 2013 Dagorlad
8
 * All rights reserved.
9 191cb31d Stephen Beaver
 *
10 aaec5634 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13 191cb31d Stephen Beaver
 *
14 aaec5634 Renato Botelho
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16 191cb31d Stephen Beaver
 *
17 aaec5634 Renato Botelho
 * 1. Redistributions of source code must retain the above copyright notice,
18
 *    this list of conditions and the following disclaimer.
19 191cb31d Stephen Beaver
 *
20 aaec5634 Renato Botelho
 * 2. Redistributions in binary form must reproduce the above copyright
21
 *    notice, this list of conditions and the following disclaimer in
22
 *    the documentation and/or other materials provided with the
23
 *    distribution.
24 191cb31d Stephen Beaver
 *
25 aaec5634 Renato Botelho
 * 3. All advertising materials mentioning features or use of this software
26
 *    must display the following acknowledgment:
27
 *    "This product includes software developed by the pfSense Project
28
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
29 191cb31d Stephen Beaver
 *
30 aaec5634 Renato Botelho
 * 4. The names "pfSense" and "pfSense Project" must not be used to
31
 *    endorse or promote products derived from this software without
32
 *    prior written permission. For written permission, please contact
33
 *    coreteam@pfsense.org.
34 191cb31d Stephen Beaver
 *
35 aaec5634 Renato Botelho
 * 5. Products derived from this software may not be called "pfSense"
36
 *    nor may "pfSense" appear in their names without prior written
37
 *    permission of the Electric Sheep Fencing, LLC.
38 191cb31d Stephen Beaver
 *
39 aaec5634 Renato Botelho
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41 191cb31d Stephen Beaver
 *
42 aaec5634 Renato Botelho
 * "This product includes software developed by the pfSense Project
43
 * for use in the pfSense software distribution (http://www.pfsense.org/).
44 191cb31d Stephen Beaver
 *
45 aaec5634 Renato Botelho
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
46
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
49
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56
 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 5d36f3c4 Stephen Beaver
 */
58 e078c882 jim-p
59
##|+PRIV
60
##|*IDENT=page-status-ntp
61 5230f468 jim-p
##|*NAME=Status: NTP
62 e078c882 jim-p
##|*DESCR=Allow access to the 'Status: NTP' page.
63
##|*MATCH=status_ntpd.php*
64
##|-PRIV
65
66
require_once("guiconfig.inc");
67
68 42b0c921 Phil Davis
if (!isset($config['ntpd']['noquery'])) {
69
	if (isset($config['system']['ipv6allow'])) {
70 e4a496ae Phil Davis
		$inet_version = "";
71 42b0c921 Phil Davis
	} else {
72 e4a496ae Phil Davis
		$inet_version = " -4";
73 42b0c921 Phil Davis
	}
74 5c8843d5 jim-p
75 e4a496ae Phil Davis
	exec("/usr/local/sbin/ntpq -pn $inet_version | /usr/bin/tail +3", $ntpq_output);
76 25890c50 jim-p
77 c56d07dc nagyrobi
	$ntpq_servers = array();
78
	foreach ($ntpq_output as $line) {
79
		$server = array();
80
81
		switch (substr($line, 0, 1)) {
82
			case " ":
83 3bd74348 bruno
				$server['status'] = gettext("Unreach/Pending");
84 c56d07dc nagyrobi
				break;
85
			case "*":
86 3bd74348 bruno
				$server['status'] = gettext("Active Peer");
87 c56d07dc nagyrobi
				break;
88
			case "+":
89 3bd74348 bruno
				$server['status'] = gettext("Candidate");
90 c56d07dc nagyrobi
				break;
91
			case "o":
92 3bd74348 bruno
				$server['status'] = gettext("PPS Peer");
93 c56d07dc nagyrobi
				break;
94
			case "#":
95 3bd74348 bruno
				$server['status'] = gettext("Selected");
96 c56d07dc nagyrobi
				break;
97
			case ".":
98 3bd74348 bruno
				$server['status'] = gettext("Excess Peer");
99 c56d07dc nagyrobi
				break;
100
			case "x":
101 3bd74348 bruno
				$server['status'] = gettext("False Ticker");
102 c56d07dc nagyrobi
				break;
103
			case "-":
104 3bd74348 bruno
				$server['status'] = gettext("Outlier");
105 c56d07dc nagyrobi
				break;
106
		}
107
108
		$line = substr($line, 1);
109
		$peerinfo = preg_split("/[\s\t]+/", $line);
110
111
		$server['server'] = $peerinfo[0];
112
		$server['refid'] = $peerinfo[1];
113
		$server['stratum'] = $peerinfo[2];
114
		$server['type'] = $peerinfo[3];
115
		$server['when'] = $peerinfo[4];
116
		$server['poll'] = $peerinfo[5];
117
		$server['reach'] = $peerinfo[6];
118
		$server['delay'] = $peerinfo[7];
119
		$server['offset'] = $peerinfo[8];
120
		$server['jitter'] = $peerinfo[9];
121
122
		$ntpq_servers[] = $server;
123
	}
124
125 62a407da Phil Davis
	exec("/usr/local/sbin/ntpq -c clockvar $inet_version", $ntpq_clockvar_output);
126 c56d07dc nagyrobi
	foreach ($ntpq_clockvar_output as $line) {
127
		if (substr($line, 0, 9) == "timecode=") {
128
			$tmp = explode('"', $line);
129
			$tmp = $tmp[1];
130
			if (substr($tmp, 0, 6) == '$GPRMC') {
131
				$gps_vars = explode(",", $tmp);
132 6c07db48 Phil Davis
				$gps_ok = ($gps_vars[2] == "A");
133 c56d07dc nagyrobi
				$gps_lat_deg = substr($gps_vars[3], 0, 2);
134 43916e63 jskyboo
				$gps_lat_min = substr($gps_vars[3], 2);
135 c56d07dc nagyrobi
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
136 43916e63 jskyboo
				$gps_lon_min = substr($gps_vars[5], 3);
137
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
138 c56d07dc nagyrobi
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
139 43916e63 jskyboo
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
140 c56d07dc nagyrobi
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
141 43916e63 jskyboo
				$gps_lat_dir = $gps_vars[4];
142
				$gps_lon_dir = $gps_vars[6];
143 42b0c921 Phil Davis
			} elseif (substr($tmp, 0, 6) == '$GPGGA') {
144 c56d07dc nagyrobi
				$gps_vars = explode(",", $tmp);
145 6c07db48 Phil Davis
				$gps_ok = $gps_vars[6];
146 c56d07dc nagyrobi
				$gps_lat_deg = substr($gps_vars[2], 0, 2);
147 43916e63 jskyboo
				$gps_lat_min = substr($gps_vars[2], 2);
148 c56d07dc nagyrobi
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
149 43916e63 jskyboo
				$gps_lon_min = substr($gps_vars[4], 3);
150
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
151 c56d07dc nagyrobi
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
152 43916e63 jskyboo
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
153 c56d07dc nagyrobi
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
154
				$gps_alt = $gps_vars[9];
155
				$gps_alt_unit = $gps_vars[10];
156 d6c4d9a0 jskyboo
				$gps_sat = (int)$gps_vars[7];
157 43916e63 jskyboo
				$gps_lat_dir = $gps_vars[3];
158
				$gps_lon_dir = $gps_vars[5];
159 42b0c921 Phil Davis
			} elseif (substr($tmp, 0, 6) == '$GPGLL') {
160 d6c4d9a0 jskyboo
				$gps_vars = preg_split('/[,\*]+/', $tmp);
161 6c07db48 Phil Davis
				$gps_ok = ($gps_vars[6] == "A");
162 c56d07dc nagyrobi
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
163 43916e63 jskyboo
				$gps_lat_min = substr($gps_vars[1], 2);
164 c56d07dc nagyrobi
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
165 43916e63 jskyboo
				$gps_lon_min = substr($gps_vars[3], 3);
166
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
167 c56d07dc nagyrobi
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
168 43916e63 jskyboo
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
169 c56d07dc nagyrobi
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
170 43916e63 jskyboo
				$gps_lat_dir = $gps_vars[2];
171
				$gps_lon_dir = $gps_vars[4];
172 5476b118 jskyboo
			} elseif (substr($tmp, 0, 6) == '$PGRMF') {
173
				$gps_vars = preg_split('/[,\*]+/', $tmp);
174
				$gps_ok = $gps_vars[11];
175
				$gps_lat_deg = substr($gps_vars[6], 0, 2);
176
				$gps_lat_min = substr($gps_vars[6], 2);
177
				$gps_lon_deg = substr($gps_vars[8], 0, 3);
178
				$gps_lon_min = substr($gps_vars[8], 3);
179
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
180
				$gps_lat = $gps_lat * (($gps_vars[7] == "N") ? 1 : -1);
181
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
182
				$gps_lon = $gps_lon * (($gps_vars[9] == "E") ? 1 : -1);
183
				$gps_lat_dir = $gps_vars[7];
184
				$gps_lon_dir = $gps_vars[9];
185 c56d07dc nagyrobi
			}
186
		}
187 25890c50 jim-p
	}
188 e078c882 jim-p
}
189
190 43916e63 jskyboo
if (isset($gps_ok) && isset($config['ntpd']['gps']['extstatus']) && ($config['ntpd']['gps']['nmeaset']['gpgsv'] || $config['ntpd']['gps']['nmeaset']['gpgga'])) {
191
	$lookfor['GPGSV'] = $config['ntpd']['gps']['nmeaset']['gpgsv'];
192
	$lookfor['GPGGA'] = !isset($gps_sat) && $config['ntpd']['gps']['nmeaset']['gpgga'];
193
	$gpsport = fopen('/dev/gps0', 'r+');
194
	while ($gpsport && ($lookfor['GPGSV'] || $lookfor['GPGGA'])) {
195 c56d07dc nagyrobi
		$buffer = fgets($gpsport);
196 43916e63 jskyboo
		if ($lookfor['GPGSV'] && substr($buffer, 0, 6) == '$GPGSV') {
197 6c07db48 Phil Davis
			$gpgsv = explode(',', $buffer);
198 43916e63 jskyboo
			$gps_satview = (int)$gpgsv[3];
199
			$lookfor['GPGSV'] = 0;
200
		} elseif ($lookfor['GPGGA'] && substr($buffer, 0, 6) == '$GPGGA') {
201
			$gpgga = explode(',', $buffer);
202
			$gps_sat = (int)$gpgga[7];
203
			$gps_alt = $gpgga[9];
204
			$gps_alt_unit = $gpgga[10];
205
			$lookfor['GPGGA'] = 0;
206 5c8843d5 jim-p
		}
207
	}
208
}
209
210 ae74c26d Stephen Beaver
// Responding to an AJAX call, we return the GPS data or the status data depending on $_REQUEST['dogps']
211
if ($_REQUEST['ajax']) {
212
213
	if ($_REQUEST['dogps'] == "yes") {
214
		print_gps();
215
	} else {
216
		print_status();
217
	}
218
219
	exit;
220
}
221
222
function print_status() {
223
	global $config, $ntpq_servers;
224
225
	if (isset($config['ntpd']['noquery'])):
226
227
		print("<tr>\n");
228
		print('<td class="warning" colspan="11">');
229
		printf(gettext("Statistics unavailable because ntpq and ntpdc queries are disabled in the %sNTP service settings%s"), '<a href="services_ntpd.php">', '</a>');
230
		print("</td>\n");
231
		print("</tr>\n");
232
	elseif (count($ntpq_servers) == 0):
233
		print("<tr>\n");
234
		print('<td class="warning" colspan="11">');
235 ca459215 Stephen Beaver
		printf(gettext("No peers found, %sis the ntp service running?%s"), '<a href="status_services.php">', '</a>');
236 ae74c26d Stephen Beaver
		print("</td>\n");
237
		print("</tr>\n");
238
	else:
239
240
		$i = 0;
241
		foreach ($ntpq_servers as $server):
242
			print("<tr>\n");
243
			print("<td>" . $server['status'] . "</td>\n");
244
			print("<td>" . $server['server'] . "</td>\n");
245
			print("<td>" . $server['refid'] . "</td>\n");
246
			print("<td>" . $server['stratum'] . "</td>\n");
247
			print("<td>" . $server['type'] . "</td>\n");
248
			print("<td>" . $server['when'] . "</td>\n");
249
			print("<td>" . $server['poll'] . "</td>\n");
250
			print("<td>" . $server['reach'] . "</td>\n");
251
			print("<td>" . $server['delay'] . "</td>\n");
252
			print("<td>" . $server['offset'] . "</td>\n");
253
			print("<td>" . $server['jitter'] . "</td>\n");
254
			print("</tr>\n");
255
			$i++;
256
		endforeach;
257
	endif;
258
}
259
260
function print_gps() {
261 43916e63 jskyboo
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_lat_dir, $gps_lon_dir,
262 ae74c26d Stephen Beaver
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
263
264
	print("<tr>\n");
265
	print("<td>\n");
266 ca459215 Stephen Beaver
	printf("%.5f", $gps_lat);
267 ae74c26d Stephen Beaver
	print(" (");
268 ca459215 Stephen Beaver
	printf("%d%s", $gps_lat_deg, "&deg;");
269 43916e63 jskyboo
	printf("%.5f", $gps_lat_min);
270
	print($gps_lat_dir);
271 ae74c26d Stephen Beaver
	print(")");
272
	print("</td>\n");
273
	print("<td>\n");
274 ca459215 Stephen Beaver
	printf("%.5f", $gps_lon);
275 ae74c26d Stephen Beaver
	print(" (");
276 ca459215 Stephen Beaver
	printf("%d%s", $gps_lon_deg, "&deg;");
277 43916e63 jskyboo
	printf("%.5f", $gps_lon_min);
278
	print($gps_lon_dir);
279 ae74c26d Stephen Beaver
	print(")");
280
	print("</td>\n");
281
282
	if (isset($gps_alt)) {
283 ca459215 Stephen Beaver
		print("<td>\n");
284
		print($gps_alt . ' ' . $gps_alt_unit);
285
		print("</td>\n");
286 ae74c26d Stephen Beaver
	}
287
288
	if (isset($gps_sat) || isset($gps_satview)) {
289 d6c4d9a0 jskyboo
		print('<td>');
290 ae74c26d Stephen Beaver
291
		if (isset($gps_satview)) {
292
			print(gettext('in view ') . intval($gps_satview));
293
		}
294
295
		if (isset($gps_sat) && isset($gps_satview)) {
296
			print(', ');
297
		}
298
		if (isset($gps_sat)) {
299
			print(gettext('in use ') . $gps_sat);
300
		}
301
302
		print("</td>\n");
303
	}
304
305
	print("</tr>\n");
306
	print("<tr>\n");
307
	print('<td colspan="' . $gps_goo_lnk . '"><a target="_gmaps" href="http://maps.google.com/?q=' . $gps_lat . ',' . $gps_lon . '">' . gettext("Google Maps Link") . '</a></td>');
308
	print("</tr>\n");
309
}
310
311 6c07db48 Phil Davis
$pgtitle = array(gettext("Status"), gettext("NTP"));
312 b32dd0a6 jim-p
$shortcut_section = "ntp";
313 8dff26ce sbeaver
314 e078c882 jim-p
include("head.inc");
315
?>
316 8dff26ce sbeaver
317
<div class="panel panel-default">
318 3bd74348 bruno
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Network Time Protocol Status");?></h2></div>
319 8dff26ce sbeaver
	<div class="panel-body">
320 10fe1eb5 Stephen Beaver
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
321 8dff26ce sbeaver
			<thead>
322
				<tr>
323 ae74c26d Stephen Beaver
					<th><?=gettext("Status")?></th>
324
					<th><?=gettext("Server")?></th>
325
					<th><?=gettext("Ref ID")?></th>
326
					<th><?=gettext("Stratum")?></th>
327
					<th><?=gettext("Type")?></th>
328
					<th><?=gettext("When")?></th>
329
					<th><?=gettext("Poll")?></th>
330
					<th><?=gettext("Reach")?></th>
331
					<th><?=gettext("Delay")?></th>
332
					<th><?=gettext("Offset")?></th>
333
					<th><?=gettext("Jitter")?></th>
334 8dff26ce sbeaver
				</tr>
335
			</thead>
336 ae74c26d Stephen Beaver
			<tbody id="ntpbody">
337
				<?=print_status()?>
338 8dff26ce sbeaver
			</tbody>
339
		</table>
340
	</div>
341
</div>
342
343
344
<?php
345
346 ae74c26d Stephen Beaver
$showgps = 0;
347
348 8dff26ce sbeaver
// GPS satellite information (if available)
349
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
350 c33f2391 Phil Davis
	$gps_goo_lnk = 2;
351 ae74c26d Stephen Beaver
	$showgps = 1;
352 c33f2391 Phil Davis
?>
353 8dff26ce sbeaver
354 c33f2391 Phil Davis
<div class="panel panel-default">
355 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("GPS Information");?></h2></div>
356 c33f2391 Phil Davis
	<div class="panel-body">
357
		<table class="table table-striped table-hover table-condensed">
358
			<thead>
359 8dff26ce sbeaver
				<tr>
360 ae74c26d Stephen Beaver
					<th><?=gettext("Clock Latitude")?></th>
361
					<th><?=gettext("Clock Longitude")?></th>
362 c33f2391 Phil Davis
<?php
363
	if (isset($gps_alt)) {
364
?>
365 ae74c26d Stephen Beaver
					<th><?=gettext("Clock Altitude")?></th>
366 c33f2391 Phil Davis
<?php
367
		$gps_goo_lnk++;
368
	}
369 8dff26ce sbeaver
370 c33f2391 Phil Davis
	if (isset($gps_sat) || isset($gps_satview)) {
371
?>
372 ae74c26d Stephen Beaver
					<th><?=gettext("Satellites")?></th>
373 c33f2391 Phil Davis
<?php
374
		$gps_goo_lnk++;
375
	}
376
?>
377
				</tr>
378
			</thead>
379 8dff26ce sbeaver
380 ae74c26d Stephen Beaver
			<tbody id="gpsbody">
381
				<?=print_gps()?>
382
			</tbody>
383
		</table>
384
	</div>
385
</div>
386 8dff26ce sbeaver
387 c33f2391 Phil Davis
<?php
388 ae74c26d Stephen Beaver
endif;
389 c33f2391 Phil Davis
?>
390 8dff26ce sbeaver
391 ae74c26d Stephen Beaver
<script type="text/javascript">
392
//<![CDATA[
393
events.push(function() {
394
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
395
	do_gps = "no";
396 8dff26ce sbeaver
397 ae74c26d Stephen Beaver
	// Fetch the tbody contents from the server
398
	function update_tables() {
399
400
		if (ajax_lock) {
401
			return;
402 c33f2391 Phil Davis
		}
403 ae74c26d Stephen Beaver
404
		ajax_lock = true;
405
406
		ajaxRequest = $.ajax(
407
			{
408
				url: "/status_ntpd.php",
409
				type: "post",
410
				data: {
411
					ajax: 	"ajax",
412
					dogps:  do_gps
413
				}
414
			}
415
		);
416
417
		// Deal with the results of the above ajax call
418
		ajaxRequest.done(function (response, textStatus, jqXHR) {
419
			if (do_gps == "yes") {
420
				$('#gpsbody').html(response);
421
			} else {
422
				$('#ntpbody').html(response);
423
			}
424
425
			ajax_lock = false;
426
427 4c84c124 Stephen Beaver
			// Alternate updating the status table and the gps table (if enabled)
428 ae74c26d Stephen Beaver
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
429
				do_gps = "no";
430
			} else {
431 4c84c124 Stephen Beaver
				do_gps = "yes";
432 ae74c26d Stephen Beaver
			}
433
434
			// and do it again
435
			setTimeout(update_tables, 5000);
436
		});
437
438
439 c33f2391 Phil Davis
	}
440 8dff26ce sbeaver
441 ae74c26d Stephen Beaver
	// Populate the tbody on page load
442
	update_tables();
443
});
444
//]]>
445
</script>
446 8dff26ce sbeaver
447 ae74c26d Stephen Beaver
<?php
448 c33f2391 Phil Davis
include("foot.inc");
449
?>