Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	status_ntpd.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2013 Dagorlad
8
 *
9
 *	Some or all of this file is based on the m0n0wall project which is
10
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
11
 *
12
 *	Redistribution and use in source and binary forms, with or without modification,
13
 *	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
20
 *		the documentation and/or other materials provided with the
21
 *		distribution.
22
 *
23
 *	3. All advertising materials mentioning features or use of this software
24
 *		must display the following acknowledgment:
25
 *		"This product includes software developed by the pfSense Project
26
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
27
 *
28
 *	4. The names "pfSense" and "pfSense Project" must not be used to
29
 *		 endorse or promote products derived from this software without
30
 *		 prior written permission. For written permission, please contact
31
 *		 coreteam@pfsense.org.
32
 *
33
 *	5. Products derived from this software may not be called "pfSense"
34
 *		nor may "pfSense" appear in their names without prior written
35
 *		permission of the Electric Sheep Fencing, LLC.
36
 *
37
 *	6. Redistributions of any form whatsoever must retain the following
38
 *		acknowledgment:
39
 *
40
 *	"This product includes software developed by the pfSense Project
41
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
42
 *
43
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
44
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
47
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
55
 *
56
 *	====================================================================
57
 *
58
 */
59

    
60
##|+PRIV
61
##|*IDENT=page-status-ntp
62
##|*NAME=Status: NTP
63
##|*DESCR=Allow access to the 'Status: NTP' page.
64
##|*MATCH=status_ntpd.php*
65
##|-PRIV
66

    
67
require_once("guiconfig.inc");
68

    
69
if (!isset($config['ntpd']['noquery'])) {
70
	if (isset($config['system']['ipv6allow'])) {
71
		$inet_version = "";
72
	} else {
73
		$inet_version = " -4";
74
	}
75

    
76
	exec("/usr/local/sbin/ntpq -pn $inet_version | /usr/bin/tail +3", $ntpq_output);
77

    
78
	$ntpq_servers = array();
79
	foreach ($ntpq_output as $line) {
80
		$server = array();
81

    
82
		switch (substr($line, 0, 1)) {
83
			case " ":
84
				$server['status'] = gettext("Unreach/Pending");
85
				break;
86
			case "*":
87
				$server['status'] = gettext("Active Peer");
88
				break;
89
			case "+":
90
				$server['status'] = gettext("Candidate");
91
				break;
92
			case "o":
93
				$server['status'] = gettext("PPS Peer");
94
				break;
95
			case "#":
96
				$server['status'] = gettext("Selected");
97
				break;
98
			case ".":
99
				$server['status'] = gettext("Excess Peer");
100
				break;
101
			case "x":
102
				$server['status'] = gettext("False Ticker");
103
				break;
104
			case "-":
105
				$server['status'] = gettext("Outlier");
106
				break;
107
		}
108

    
109
		$line = substr($line, 1);
110
		$peerinfo = preg_split("/[\s\t]+/", $line);
111

    
112
		$server['server'] = $peerinfo[0];
113
		$server['refid'] = $peerinfo[1];
114
		$server['stratum'] = $peerinfo[2];
115
		$server['type'] = $peerinfo[3];
116
		$server['when'] = $peerinfo[4];
117
		$server['poll'] = $peerinfo[5];
118
		$server['reach'] = $peerinfo[6];
119
		$server['delay'] = $peerinfo[7];
120
		$server['offset'] = $peerinfo[8];
121
		$server['jitter'] = $peerinfo[9];
122

    
123
		$ntpq_servers[] = $server;
124
	}
125

    
126
	exec("/usr/local/sbin/ntpq -c clockvar $inet_version", $ntpq_clockvar_output);
127
	foreach ($ntpq_clockvar_output as $line) {
128
		if (substr($line, 0, 9) == "timecode=") {
129
			$tmp = explode('"', $line);
130
			$tmp = $tmp[1];
131
			if (substr($tmp, 0, 6) == '$GPRMC') {
132
				$gps_vars = explode(",", $tmp);
133
				$gps_ok = ($gps_vars[2] == "A");
134
				$gps_lat_deg = substr($gps_vars[3], 0, 2);
135
				$gps_lat_min = substr($gps_vars[3], 2) / 60.0;
136
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
137
				$gps_lon_min = substr($gps_vars[5], 3) / 60.0;
138
				$gps_lat = $gps_lat_deg + $gps_lat_min;
139
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
140
				$gps_lon = $gps_lon_deg + $gps_lon_min;
141
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
142
			} elseif (substr($tmp, 0, 6) == '$GPGGA') {
143
				$gps_vars = explode(",", $tmp);
144
				$gps_ok = $gps_vars[6];
145
				$gps_lat_deg = substr($gps_vars[2], 0, 2);
146
				$gps_lat_min = substr($gps_vars[2], 2) / 60.0;
147
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
148
				$gps_lon_min = substr($gps_vars[4], 3) / 60.0;
149
				$gps_lat = $gps_lat_deg + $gps_lat_min;
150
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
151
				$gps_lon = $gps_lon_deg + $gps_lon_min;
152
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
153
				$gps_alt = $gps_vars[9];
154
				$gps_alt_unit = $gps_vars[10];
155
				$gps_sat = $gps_vars[7];
156
			} elseif (substr($tmp, 0, 6) == '$GPGLL') {
157
				$gps_vars = explode(",", $tmp);
158
				$gps_ok = ($gps_vars[6] == "A");
159
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
160
				$gps_lat_min = substr($gps_vars[1], 2) / 60.0;
161
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
162
				$gps_lon_min = substr($gps_vars[3], 3) / 60.0;
163
				$gps_lat = $gps_lat_deg + $gps_lat_min;
164
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
165
				$gps_lon = $gps_lon_deg + $gps_lon_min;
166
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
167
			}
168
		}
169
	}
170
}
171

    
172
if (isset($config['ntpd']['gps']['type']) && ($config['ntpd']['gps']['type'] == 'SureGPS') && (isset($gps_ok))) {
173
	//GSV message is only enabled by init commands in services_ntpd_gps.php for SureGPS board
174
	$gpsport = fopen("/dev/gps0", "r+");
175
	while ($gpsport) {
176
		$buffer = fgets($gpsport);
177
		if (substr($buffer, 0, 6) == '$GPGSV') {
178
			//echo $buffer."\n";
179
			$gpgsv = explode(',', $buffer);
180
			$gps_satview = $gpgsv[3];
181
			break;
182
		}
183
	}
184
}
185

    
186
// Responding to an AJAX call, we return the GPS data or the status data depending on $_REQUEST['dogps']
187
if ($_REQUEST['ajax']) {
188

    
189
	if ($_REQUEST['dogps'] == "yes") {
190
		print_gps();
191
	} else {
192
		print_status();
193
	}
194

    
195
	exit;
196
}
197

    
198
function print_status() {
199
	global $config, $ntpq_servers;
200

    
201
	if (isset($config['ntpd']['noquery'])):
202

    
203
		print("<tr>\n");
204
		print('<td class="warning" colspan="11">');
205
		printf(gettext("Statistics unavailable because ntpq and ntpdc queries are disabled in the %sNTP service settings%s"), '<a href="services_ntpd.php">', '</a>');
206
		print("</td>\n");
207
		print("</tr>\n");
208
	elseif (count($ntpq_servers) == 0):
209
		print("<tr>\n");
210
		print('<td class="warning" colspan="11">');
211
		printf(gettext("No peers found, %sis the ntp service running?%s"), '<a href="status_services.php">', '</a>');
212
		print("</td>\n");
213
		print("</tr>\n");
214
	else:
215

    
216
		$i = 0;
217
		foreach ($ntpq_servers as $server):
218
			print("<tr>\n");
219
			print("<td>" . $server['status'] . "</td>\n");
220
			print("<td>" . $server['server'] . "</td>\n");
221
			print("<td>" . $server['refid'] . "</td>\n");
222
			print("<td>" . $server['stratum'] . "</td>\n");
223
			print("<td>" . $server['type'] . "</td>\n");
224
			print("<td>" . $server['when'] . "</td>\n");
225
			print("<td>" . $server['poll'] . "</td>\n");
226
			print("<td>" . $server['reach'] . "</td>\n");
227
			print("<td>" . $server['delay'] . "</td>\n");
228
			print("<td>" . $server['offset'] . "</td>\n");
229
			print("<td>" . $server['jitter'] . "</td>\n");
230
			print("</tr>\n");
231
			$i++;
232
		endforeach;
233
	endif;
234
}
235

    
236
function print_gps() {
237
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_vars,
238
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
239

    
240
	print("<tr>\n");
241
	print("<td>\n");
242
	printf("%.5f", $gps_lat);
243
	print(" (");
244
	printf("%d%s", $gps_lat_deg, "&deg;");
245
	printf("%.5f", $gps_lat_min*60);
246
	print($gps_vars[4]);
247
	print(")");
248
	print("</td>\n");
249
	print("<td>\n");
250
	printf("%.5f", $gps_lon);
251
	print(" (");
252
	printf("%d%s", $gps_lon_deg, "&deg;");
253
	printf("%.5f", $gps_lon_min*60);
254
	print($gps_vars[6]);
255
	print(")");
256
	print("</td>\n");
257

    
258
	if (isset($gps_alt)) {
259
		print("<td>\n");
260
		print($gps_alt . ' ' . $gps_alt_unit);
261
		print("</td>\n");
262
	}
263

    
264
	if (isset($gps_sat) || isset($gps_satview)) {
265
		print('<td class="text-center">');
266

    
267
		if (isset($gps_satview)) {
268
			print(gettext('in view ') . intval($gps_satview));
269
		}
270

    
271
		if (isset($gps_sat) && isset($gps_satview)) {
272
			print(', ');
273
		}
274
		if (isset($gps_sat)) {
275
			print(gettext('in use ') . $gps_sat);
276
		}
277

    
278
		print("</td>\n");
279
	}
280

    
281
	print("</tr>\n");
282
	print("<tr>\n");
283
	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>');
284
	print("</tr>\n");
285
}
286

    
287
$pgtitle = array(gettext("Status"), gettext("NTP"));
288
$shortcut_section = "ntp";
289

    
290
include("head.inc");
291
?>
292

    
293
<div class="panel panel-default">
294
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Network Time Protocol Status");?></h2></div>
295
	<div class="panel-body">
296
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
297
			<thead>
298
				<tr>
299
					<th><?=gettext("Status")?></th>
300
					<th><?=gettext("Server")?></th>
301
					<th><?=gettext("Ref ID")?></th>
302
					<th><?=gettext("Stratum")?></th>
303
					<th><?=gettext("Type")?></th>
304
					<th><?=gettext("When")?></th>
305
					<th><?=gettext("Poll")?></th>
306
					<th><?=gettext("Reach")?></th>
307
					<th><?=gettext("Delay")?></th>
308
					<th><?=gettext("Offset")?></th>
309
					<th><?=gettext("Jitter")?></th>
310
				</tr>
311
			</thead>
312
			<tbody id="ntpbody">
313
				<?=print_status()?>
314
			</tbody>
315
		</table>
316
	</div>
317
</div>
318

    
319

    
320
<?php
321

    
322
$showgps = 0;
323

    
324
// GPS satellite information (if available)
325
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
326
	$gps_goo_lnk = 2;
327
	$showgps = 1;
328
?>
329

    
330
<div class="panel panel-default">
331
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("GPS Information");?></h2></div>
332
	<div class="panel-body">
333
		<table class="table table-striped table-hover table-condensed">
334
			<thead>
335
				<tr>
336
					<th><?=gettext("Clock Latitude")?></th>
337
					<th><?=gettext("Clock Longitude")?></th>
338
<?php
339
	if (isset($gps_alt)) {
340
?>
341
					<th><?=gettext("Clock Altitude")?></th>
342
<?php
343
		$gps_goo_lnk++;
344
	}
345

    
346
	if (isset($gps_sat) || isset($gps_satview)) {
347
?>
348
					<th><?=gettext("Satellites")?></th>
349
<?php
350
		$gps_goo_lnk++;
351
	}
352
?>
353
				</tr>
354
			</thead>
355

    
356
			<tbody id="gpsbody">
357
				<?=print_gps()?>
358
			</tbody>
359
		</table>
360
	</div>
361
</div>
362

    
363
<?php
364
endif;
365
?>
366

    
367
<script type="text/javascript">
368
//<![CDATA[
369
events.push(function() {
370
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
371
	do_gps = "no";
372

    
373
	// Fetch the tbody contents from the server
374
	function update_tables() {
375

    
376
		if (ajax_lock) {
377
			return;
378
		}
379

    
380
		ajax_lock = true;
381

    
382
		ajaxRequest = $.ajax(
383
			{
384
				url: "/status_ntpd.php",
385
				type: "post",
386
				data: {
387
					ajax: 	"ajax",
388
					dogps:  do_gps
389
				}
390
			}
391
		);
392

    
393
		// Deal with the results of the above ajax call
394
		ajaxRequest.done(function (response, textStatus, jqXHR) {
395
			if (do_gps == "yes") {
396
				$('#gpsbody').html(response);
397
			} else {
398
				$('#ntpbody').html(response);
399
			}
400

    
401
			ajax_lock = false;
402

    
403
			// Alternate updating the status table and the gps table (if enabled)
404
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
405
				do_gps = "no";
406
			} else {
407
				do_gps = "yes";
408
			}
409

    
410
			// and do it again
411
			setTimeout(update_tables, 5000);
412
		});
413

    
414

    
415
	}
416

    
417
	// Populate the tbody on page load
418
	update_tables();
419
});
420
//]]>
421
</script>
422

    
423
<?php
424
include("foot.inc");
425
?>
(178-178/225)