Project

General

Profile

Download (12.3 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2013 Dagorlad
10
 * All rights reserved.
11
 *
12
 * originally based on m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * Licensed under the Apache License, Version 2.0 (the "License");
17
 * you may not use this file except in compliance with the License.
18
 * You may obtain a copy of the License at
19
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * Unless required by applicable law or agreed to in writing, software
23
 * distributed under the License is distributed on an "AS IS" BASIS,
24
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
 * See the License for the specific language governing permissions and
26
 * limitations under the License.
27
 */
28

    
29
##|+PRIV
30
##|*IDENT=page-status-ntp
31
##|*NAME=Status: NTP
32
##|*DESCR=Allow access to the 'Status: NTP' page.
33
##|*MATCH=status_ntpd.php*
34
##|-PRIV
35

    
36
require_once("guiconfig.inc");
37

    
38
$allow_query = !isset($config['ntpd']['noquery']);
39
if (!empty($config['ntpd']['restrictions']['row']) && is_array($config['ntpd']['restrictions']['row'])) {
40
	foreach ($config['ntpd']['restrictions']['row'] as $v) {
41
		if (ip_in_subnet('127.0.0.1', "{$v['acl_network']}/{$v['mask']}") || 
42
		    ip_in_subnet('::1', "{$v['acl_network']}/{$v['mask']}")) {
43
			$allow_query = !isset($v['noquery']);
44
		}
45
	}
46
}
47

    
48
if ($allow_query && ($config['ntpd']['enable'] != 'disabled')) {
49
	if (isset($config['system']['ipv6allow'])) {
50
		$inet_version = "";
51
	} else {
52
		$inet_version = " -4";
53
	}
54

    
55
	exec('/usr/local/sbin/ntpq -pnw ' . $inet_version . ' | /usr/bin/tail +3 | /usr/bin/awk -v RS= \'{gsub(/\n[[:space:]][[:space:]]+/," ")}1\'', $ntpq_output);
56

    
57
	$ntpq_servers = array();
58
	foreach ($ntpq_output as $line) {
59
		$server = array();
60
		$status_char = substr($line, 0, 1);
61
		$line = substr($line, 1);
62
		$peerinfo = preg_split("/[\s\t]+/", $line);
63

    
64
		$server['server'] = $peerinfo[0];
65
		$server['refid'] = $peerinfo[1];
66
		$server['stratum'] = $peerinfo[2];
67
		$server['type'] = $peerinfo[3];
68
		$server['when'] = $peerinfo[4];
69
		$server['poll'] = $peerinfo[5];
70
		$server['reach'] = $peerinfo[6];
71
		$server['delay'] = $peerinfo[7];
72
		$server['offset'] = $peerinfo[8];
73
		$server['jitter'] = $peerinfo[9];
74

    
75
		switch ($status_char) {
76
			case " ":
77
				if ($server['refid'] == ".POOL.") {
78
					$server['status'] = gettext("Pool Placeholder");
79
				} else {
80
					$server['status'] = gettext("Unreach/Pending");
81
				}
82
				break;
83
			case "*":
84
				$server['status'] = gettext("Active Peer");
85
				break;
86
			case "+":
87
				$server['status'] = gettext("Candidate");
88
				break;
89
			case "o":
90
				$server['status'] = gettext("PPS Peer");
91
				break;
92
			case "#":
93
				$server['status'] = gettext("Selected");
94
				break;
95
			case ".":
96
				$server['status'] = gettext("Excess Peer");
97
				break;
98
			case "x":
99
				$server['status'] = gettext("False Ticker");
100
				break;
101
			case "-":
102
				$server['status'] = gettext("Outlier");
103
				break;
104
		}
105

    
106
		$ntpq_servers[] = $server;
107
	}
108

    
109
	exec("/usr/local/sbin/ntpq -c clockvar $inet_version", $ntpq_clockvar_output);
110
	foreach ($ntpq_clockvar_output as $line) {
111
		if (substr($line, 0, 9) == "timecode=") {
112
			$tmp = explode('"', $line);
113
			$tmp = $tmp[1];
114
			if (substr($tmp, 0, 6) == '$GPRMC') {
115
				$gps_vars = explode(",", $tmp);
116
				$gps_ok = ($gps_vars[2] == "A");
117
				$gps_lat_deg = substr($gps_vars[3], 0, 2);
118
				$gps_lat_min = substr($gps_vars[3], 2);
119
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
120
				$gps_lon_min = substr($gps_vars[5], 3);
121
				$gps_lat = (float) $gps_lat_deg + $gps_lat_min / 60.0;
122
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
123
				$gps_lon = (float) $gps_lon_deg + $gps_lon_min / 60.0;
124
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
125
				$gps_lat_dir = $gps_vars[4];
126
				$gps_lon_dir = $gps_vars[6];
127
			} elseif (substr($tmp, 0, 6) == '$GPGGA') {
128
				$gps_vars = explode(",", $tmp);
129
				$gps_ok = $gps_vars[6];
130
				$gps_lat_deg = substr($gps_vars[2], 0, 2);
131
				$gps_lat_min = substr($gps_vars[2], 2);
132
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
133
				$gps_lon_min = substr($gps_vars[4], 3);
134
				$gps_lat = (float) $gps_lat_deg + $gps_lat_min / 60.0;
135
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
136
				$gps_lon = (float) $gps_lon_deg + $gps_lon_min / 60.0;
137
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
138
				$gps_alt = $gps_vars[9];
139
				$gps_alt_unit = $gps_vars[10];
140
				$gps_sat = (int)$gps_vars[7];
141
				$gps_lat_dir = $gps_vars[3];
142
				$gps_lon_dir = $gps_vars[5];
143
			} elseif (substr($tmp, 0, 6) == '$GPGLL') {
144
				$gps_vars = preg_split('/[,\*]+/', $tmp);
145
				$gps_ok = ($gps_vars[6] == "A");
146
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
147
				$gps_lat_min = substr($gps_vars[1], 2);
148
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
149
				$gps_lon_min = substr($gps_vars[3], 3);
150
				$gps_lat = (float) $gps_lat_deg + $gps_lat_min / 60.0;
151
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
152
				$gps_lon = (float) $gps_lon_deg + $gps_lon_min / 60.0;
153
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
154
				$gps_lat_dir = $gps_vars[2];
155
				$gps_lon_dir = $gps_vars[4];
156
			} elseif (substr($tmp, 0, 6) == '$PGRMF') {
157
				$gps_vars = preg_split('/[,\*]+/', $tmp);
158
				$gps_ok = $gps_vars[11];
159
				$gps_lat_deg = substr($gps_vars[6], 0, 2);
160
				$gps_lat_min = substr($gps_vars[6], 2);
161
				$gps_lon_deg = substr($gps_vars[8], 0, 3);
162
				$gps_lon_min = substr($gps_vars[8], 3);
163
				$gps_lat = (float) $gps_lat_deg + $gps_lat_min / 60.0;
164
				$gps_lat = $gps_lat * (($gps_vars[7] == "N") ? 1 : -1);
165
				$gps_lon = (float) $gps_lon_deg + $gps_lon_min / 60.0;
166
				$gps_lon = $gps_lon * (($gps_vars[9] == "E") ? 1 : -1);
167
				$gps_lat_dir = $gps_vars[7];
168
				$gps_lon_dir = $gps_vars[9];
169
			}
170
		}
171
	}
172
}
173

    
174
if (isset($gps_ok) && isset($config['ntpd']['gps']['extstatus']) && ($config['ntpd']['gps']['nmeaset']['gpgsv'] || $config['ntpd']['gps']['nmeaset']['gpgga'])) {
175
	$lookfor['GPGSV'] = $config['ntpd']['gps']['nmeaset']['gpgsv'];
176
	$lookfor['GPGGA'] = !isset($gps_sat) && $config['ntpd']['gps']['nmeaset']['gpgga'];
177
	$gpsport = fopen('/dev/gps0', 'r+');
178
	while ($gpsport && ($lookfor['GPGSV'] || $lookfor['GPGGA'])) {
179
		$buffer = fgets($gpsport);
180
		if ($lookfor['GPGSV'] && substr($buffer, 0, 6) == '$GPGSV') {
181
			$gpgsv = explode(',', $buffer);
182
			$gps_satview = (int)$gpgsv[3];
183
			$lookfor['GPGSV'] = 0;
184
		} elseif ($lookfor['GPGGA'] && substr($buffer, 0, 6) == '$GPGGA') {
185
			$gpgga = explode(',', $buffer);
186
			$gps_sat = (int)$gpgga[7];
187
			$gps_alt = $gpgga[9];
188
			$gps_alt_unit = $gpgga[10];
189
			$lookfor['GPGGA'] = 0;
190
		}
191
	}
192
}
193

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

    
197
	if ($_REQUEST['dogps'] == "yes") {
198
		print_gps();
199
	} else {
200
		print_status();
201
	}
202

    
203
	exit;
204
}
205

    
206
function print_status() {
207
	global $config, $ntpq_servers, $allow_query;
208

    
209
	if ($config['ntpd']['enable'] == 'disabled'):
210
		print("<tr>\n");
211
		print('<td class="warning" colspan="11">');
212
		printf(gettext('NTP Server is disabled'));
213
		print("</td>\n");
214
		print("</tr>\n");
215
	elseif (!$allow_query):
216
		print("<tr>\n");
217
		print('<td class="warning" colspan="11">');
218
		printf(gettext('Statistics unavailable because ntpq and ntpdc queries are disabled in the %1$sNTP service settings%2$s'), '<a href="services_ntpd.php">', '</a>');
219
		print("</td>\n");
220
		print("</tr>\n");
221
	elseif (count($ntpq_servers) == 0):
222
		print("<tr>\n");
223
		print('<td class="warning" colspan="11">');
224
		printf(gettext('No peers found, %1$sis the ntp service running?%2$s'), '<a href="status_services.php">', '</a>');
225
		print("</td>\n");
226
		print("</tr>\n");
227
	else:
228

    
229
		$i = 0;
230
		foreach ($ntpq_servers as $server):
231
			print("<tr>\n");
232
			print("<td>" . $server['status'] . "</td>\n");
233
			print("<td>" . $server['server'] . "</td>\n");
234
			print("<td>" . $server['refid'] . "</td>\n");
235
			print("<td>" . $server['stratum'] . "</td>\n");
236
			print("<td>" . $server['type'] . "</td>\n");
237
			print("<td>" . $server['when'] . "</td>\n");
238
			print("<td>" . $server['poll'] . "</td>\n");
239
			print("<td>" . $server['reach'] . "</td>\n");
240
			print("<td>" . $server['delay'] . "</td>\n");
241
			print("<td>" . $server['offset'] . "</td>\n");
242
			print("<td>" . $server['jitter'] . "</td>\n");
243
			print("</tr>\n");
244
			$i++;
245
		endforeach;
246
	endif;
247
}
248

    
249
function print_gps() {
250
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_lat_dir, $gps_lon_dir,
251
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
252

    
253
	print("<tr>\n");
254
	print("<td>\n");
255
	printf("%.5f", $gps_lat);
256
	print(" (");
257
	printf("%d%s", $gps_lat_deg, "&deg;");
258
	printf("%.5f", $gps_lat_min);
259
	print($gps_lat_dir);
260
	print(")");
261
	print("</td>\n");
262
	print("<td>\n");
263
	printf("%.5f", $gps_lon);
264
	print(" (");
265
	printf("%d%s", $gps_lon_deg, "&deg;");
266
	printf("%.5f", $gps_lon_min);
267
	print($gps_lon_dir);
268
	print(")");
269
	print("</td>\n");
270

    
271
	if (isset($gps_alt)) {
272
		print("<td>\n");
273
		print($gps_alt . ' ' . $gps_alt_unit);
274
		print("</td>\n");
275
	}
276

    
277
	if (isset($gps_sat) || isset($gps_satview)) {
278
		print('<td>');
279

    
280
		if (isset($gps_satview)) {
281
			print(gettext('in view ') . intval($gps_satview));
282
		}
283

    
284
		if (isset($gps_sat) && isset($gps_satview)) {
285
			print(', ');
286
		}
287
		if (isset($gps_sat)) {
288
			print(gettext('in use ') . $gps_sat);
289
		}
290

    
291
		print("</td>\n");
292
	}
293

    
294
	print("</tr>\n");
295
	print("<tr>\n");
296
	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>');
297
	print("</tr>\n");
298
}
299

    
300
$pgtitle = array(gettext("Status"), gettext("NTP"));
301
$shortcut_section = "ntp";
302

    
303
include("head.inc");
304
?>
305

    
306
<div class="panel panel-default">
307
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Network Time Protocol Status");?></h2></div>
308
	<div class="panel-body">
309
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
310
			<thead>
311
				<tr>
312
					<th><?=gettext("Status")?></th>
313
					<th><?=gettext("Server")?></th>
314
					<th><?=gettext("Ref ID")?></th>
315
					<th><?=gettext("Stratum")?></th>
316
					<th><?=gettext("Type")?></th>
317
					<th><?=gettext("When")?></th>
318
					<th><?=gettext("Poll (s)")?></th>
319
					<th><?=gettext("Reach")?></th>
320
					<th><?=gettext("Delay (ms)")?></th>
321
					<th><?=gettext("Offset (ms)")?></th>
322
					<th><?=gettext("Jitter (ms)")?></th>
323
				</tr>
324
			</thead>
325
			<tbody id="ntpbody">
326
				<?=print_status()?>
327
			</tbody>
328
		</table>
329
	</div>
330
</div>
331

    
332

    
333
<?php
334

    
335
$showgps = 0;
336

    
337
// GPS satellite information (if available)
338
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
339
	$gps_goo_lnk = 2;
340
	$showgps = 1;
341
?>
342

    
343
<div class="panel panel-default">
344
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("GPS Information");?></h2></div>
345
	<div class="panel-body">
346
		<table class="table table-striped table-hover table-condensed">
347
			<thead>
348
				<tr>
349
					<th><?=gettext("Clock Latitude")?></th>
350
					<th><?=gettext("Clock Longitude")?></th>
351
<?php
352
	if (isset($gps_alt)) {
353
?>
354
					<th><?=gettext("Clock Altitude")?></th>
355
<?php
356
		$gps_goo_lnk++;
357
	}
358

    
359
	if (isset($gps_sat) || isset($gps_satview)) {
360
?>
361
					<th><?=gettext("Satellites")?></th>
362
<?php
363
		$gps_goo_lnk++;
364
	}
365
?>
366
				</tr>
367
			</thead>
368

    
369
			<tbody id="gpsbody">
370
				<?=print_gps()?>
371
			</tbody>
372
		</table>
373
	</div>
374
</div>
375

    
376
<?php
377
endif;
378
?>
379

    
380
<script type="text/javascript">
381
//<![CDATA[
382
events.push(function() {
383
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
384
	do_gps = "no";
385

    
386
	// Fetch the tbody contents from the server
387
	function update_tables() {
388

    
389
		if (ajax_lock) {
390
			return;
391
		}
392

    
393
		ajax_lock = true;
394

    
395
		ajaxRequest = $.ajax(
396
			{
397
				url: "/status_ntpd.php",
398
				type: "post",
399
				data: {
400
					ajax: 	"ajax",
401
					dogps:  do_gps
402
				}
403
			}
404
		);
405

    
406
		// Deal with the results of the above ajax call
407
		ajaxRequest.done(function (response, textStatus, jqXHR) {
408
			if (do_gps == "yes") {
409
				$('#gpsbody').html(response);
410
			} else {
411
				$('#ntpbody').html(response);
412
			}
413

    
414
			ajax_lock = false;
415

    
416
			// Alternate updating the status table and the gps table (if enabled)
417
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
418
				do_gps = "no";
419
			} else {
420
				do_gps = "yes";
421
			}
422

    
423
			// and do it again
424
			setTimeout(update_tables, 5000);
425
		});
426

    
427

    
428
	}
429

    
430
	// Populate the tbody on page load
431
	update_tables();
432
});
433
//]]>
434
</script>
435

    
436
<?php
437
include("foot.inc");
438
?>
(177-177/229)