Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * status_ntpd.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * Copyright (c) 2013 Dagorlad
8
 * All rights reserved.
9
 *
10
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 *    this list of conditions and the following disclaimer.
19
 *
20
 * 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
 *
25
 * 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
 *
30
 * 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
 *
35
 * 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
 *
39
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41
 *
42
 * "This product includes software developed by the pfSense Project
43
 * for use in the pfSense software distribution (http://www.pfsense.org/).
44
 *
45
 * 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
 */
58

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

    
66
require_once("guiconfig.inc");
67

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

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

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

    
81
		switch (substr($line, 0, 1)) {
82
			case " ":
83
				$server['status'] = gettext("Unreach/Pending");
84
				break;
85
			case "*":
86
				$server['status'] = gettext("Active Peer");
87
				break;
88
			case "+":
89
				$server['status'] = gettext("Candidate");
90
				break;
91
			case "o":
92
				$server['status'] = gettext("PPS Peer");
93
				break;
94
			case "#":
95
				$server['status'] = gettext("Selected");
96
				break;
97
			case ".":
98
				$server['status'] = gettext("Excess Peer");
99
				break;
100
			case "x":
101
				$server['status'] = gettext("False Ticker");
102
				break;
103
			case "-":
104
				$server['status'] = gettext("Outlier");
105
				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
	exec("/usr/local/sbin/ntpq -c clockvar $inet_version", $ntpq_clockvar_output);
126
	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
				$gps_ok = ($gps_vars[2] == "A");
133
				$gps_lat_deg = substr($gps_vars[3], 0, 2);
134
				$gps_lat_min = substr($gps_vars[3], 2) / 60.0;
135
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
136
				$gps_lon_min = substr($gps_vars[5], 3) / 60.0;
137
				$gps_lat = $gps_lat_deg + $gps_lat_min;
138
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
139
				$gps_lon = $gps_lon_deg + $gps_lon_min;
140
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
141
			} elseif (substr($tmp, 0, 6) == '$GPGGA') {
142
				$gps_vars = explode(",", $tmp);
143
				$gps_ok = $gps_vars[6];
144
				$gps_lat_deg = substr($gps_vars[2], 0, 2);
145
				$gps_lat_min = substr($gps_vars[2], 2) / 60.0;
146
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
147
				$gps_lon_min = substr($gps_vars[4], 3) / 60.0;
148
				$gps_lat = $gps_lat_deg + $gps_lat_min;
149
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
150
				$gps_lon = $gps_lon_deg + $gps_lon_min;
151
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
152
				$gps_alt = $gps_vars[9];
153
				$gps_alt_unit = $gps_vars[10];
154
				$gps_sat = $gps_vars[7];
155
			} elseif (substr($tmp, 0, 6) == '$GPGLL') {
156
				$gps_vars = explode(",", $tmp);
157
				$gps_ok = ($gps_vars[6] == "A");
158
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
159
				$gps_lat_min = substr($gps_vars[1], 2) / 60.0;
160
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
161
				$gps_lon_min = substr($gps_vars[3], 3) / 60.0;
162
				$gps_lat = $gps_lat_deg + $gps_lat_min;
163
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
164
				$gps_lon = $gps_lon_deg + $gps_lon_min;
165
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
166
			}
167
		}
168
	}
169
}
170

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

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

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

    
194
	exit;
195
}
196

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
318

    
319
<?php
320

    
321
$showgps = 0;
322

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

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

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

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

    
362
<?php
363
endif;
364
?>
365

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

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

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

    
379
		ajax_lock = true;
380

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

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

    
400
			ajax_lock = false;
401

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

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

    
413

    
414
	}
415

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

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