Project

General

Profile

Download (12.4 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-2024 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('config.inc');
37
require_once('config.lib.inc');
38
require_once('guiconfig.inc');
39

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

    
48
if ($allow_query && (config_get_path('ntpd/enable') != 'disabled')) {
49
	if (config_path_enabled('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 + (float) $gps_lat_min / 60.0;
122
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
123
				$gps_lon = (float) $gps_lon_deg + (float) $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 + (float) $gps_lat_min / 60.0;
135
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
136
				$gps_lon = (float) $gps_lon_deg + (float) $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 + (float) $gps_lat_min / 60.0;
151
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
152
				$gps_lon = (float) $gps_lon_deg + (float) $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 + (float) $gps_lat_min / 60.0;
164
				$gps_lat = $gps_lat * (($gps_vars[7] == "N") ? 1 : -1);
165
				$gps_lon = (float) $gps_lon_deg + (float) $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
global $showgps;
175
$showgps = 0;
176

    
177
global $gps_goo_lnk;
178
$gps_goo_lnk = 1;
179

    
180
// GPS satellite information (if available)
181
if (($gps_ok) && ($gps_lat) && ($gps_lon)) {
182
	$gps_goo_lnk = 2;
183
	$showgps = 1;
184
}
185

    
186
if (isset($gps_ok) && config_path_enabled('ntpd/gps','extstatus')) {
187
	$lookfor['GPGSV'] = config_path_enabled('ntpd/gps/nmeaset','gpgsv');
188
	$lookfor['GPGGA'] = !isset($gps_sat) && config_path_enabled('ntpd/gps/nmeaset','gpgga');
189
	$gpsport = fopen('/dev/gps0', 'r+');
190
	while ($gpsport && ($lookfor['GPGSV'] || $lookfor['GPGGA'])) {
191
		$buffer = fgets($gpsport);
192
		if ($lookfor['GPGSV'] && substr($buffer, 0, 6) == '$GPGSV') {
193
			$gpgsv = explode(',', $buffer);
194
			$gps_satview = (int)$gpgsv[3];
195
			$lookfor['GPGSV'] = false;
196
		} elseif ($lookfor['GPGGA'] && substr($buffer, 0, 6) == '$GPGGA') {
197
			$gpgga = explode(',', $buffer);
198
			$gps_sat = (int)$gpgga[7];
199
			$gps_alt = $gpgga[9];
200
			$gps_alt_unit = $gpgga[10];
201
			$lookfor['GPGGA'] = false;
202
		}
203
	}
204
}
205

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

    
209
	if ($_REQUEST['dogps'] == "yes") {
210
		print_gps();
211
	} else {
212
		print_status();
213
	}
214

    
215
	exit;
216
}
217

    
218
function print_status() {
219
	global $ntpq_servers, $allow_query;
220

    
221
	if (config_get_path('ntpd/enable') == 'disabled'):
222
		print("<tr>\n");
223
		print('<td class="warning" colspan="11">');
224
		printf(gettext('NTP Server is disabled'));
225
		print("</td>\n");
226
		print("</tr>\n");
227
	elseif (!$allow_query):
228
		print("<tr>\n");
229
		print('<td class="warning" colspan="11">');
230
		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>');
231
		print("</td>\n");
232
		print("</tr>\n");
233
	elseif (count($ntpq_servers) == 0):
234
		print("<tr>\n");
235
		print('<td class="warning" colspan="11">');
236
		printf(gettext('No peers found, %1$sis the ntp service running?%2$s'), '<a href="status_services.php">', '</a>');
237
		print("</td>\n");
238
		print("</tr>\n");
239
	else:
240

    
241
		$i = 0;
242
		foreach ($ntpq_servers as $server):
243
			print("<tr>\n");
244
			print("<td>" . $server['status'] . "</td>\n");
245
			print("<td>" . $server['server'] . "</td>\n");
246
			print("<td>" . $server['refid'] . "</td>\n");
247
			print("<td>" . $server['stratum'] . "</td>\n");
248
			print("<td>" . $server['type'] . "</td>\n");
249
			print("<td>" . $server['when'] . "</td>\n");
250
			print("<td>" . $server['poll'] . "</td>\n");
251
			print("<td>" . $server['reach'] . "</td>\n");
252
			print("<td>" . $server['delay'] . "</td>\n");
253
			print("<td>" . $server['offset'] . "</td>\n");
254
			print("<td>" . $server['jitter'] . "</td>\n");
255
			print("</tr>\n");
256
			$i++;
257
		endforeach;
258
	endif;
259
}
260

    
261
function print_gps() {
262
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_lat_dir, $gps_lon_dir,
263
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
264

    
265
	print("<tr>\n");
266
	print("<td>\n");
267
	printf("%.5f", $gps_lat);
268
	print(" (");
269
	printf("%d%s", $gps_lat_deg, "&deg;");
270
	printf("%.5f", $gps_lat_min);
271
	print($gps_lat_dir);
272
	print(")");
273
	print("</td>\n");
274
	print("<td>\n");
275
	printf("%.5f", $gps_lon);
276
	print(" (");
277
	printf("%d%s", $gps_lon_deg, "&deg;");
278
	printf("%.5f", $gps_lon_min);
279
	print($gps_lon_dir);
280
	print(")");
281
	print("</td>\n");
282

    
283
	if (isset($gps_alt)) {
284
		print("<td>\n");
285
		print($gps_alt . ' ' . $gps_alt_unit);
286
		print("</td>\n");
287
	}
288

    
289
	if (isset($gps_sat) || isset($gps_satview)) {
290
		print('<td>');
291

    
292
		if (isset($gps_satview)) {
293
			print(gettext('in view ') . intval($gps_satview));
294
		}
295

    
296
		if (isset($gps_sat) && isset($gps_satview)) {
297
			print(', ');
298
		}
299
		if (isset($gps_sat)) {
300
			print(gettext('in use ') . $gps_sat);
301
		}
302

    
303
		print("</td>\n");
304
	}
305

    
306
	print("</tr>\n");
307
	print("<tr>\n");
308
	print('<td colspan="' . $gps_goo_lnk . '"><a target="_gmaps" href="https://maps.google.com/?q=' . $gps_lat . ',' . $gps_lon . '">' . gettext("Google Maps Link") . '</a></td>');
309
	print("</tr>\n");
310
}
311

    
312
$pgtitle = array(gettext("Status"), gettext("NTP"));
313
$shortcut_section = "ntp";
314

    
315
include("head.inc");
316
?>
317

    
318
<div class="panel panel-default">
319
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Network Time Protocol Status");?></h2></div>
320
	<div class="panel-body">
321
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
322
			<thead>
323
				<tr>
324
					<th><?=gettext("Status")?></th>
325
					<th><?=gettext("Server")?></th>
326
					<th><?=gettext("Ref ID")?></th>
327
					<th><?=gettext("Stratum")?></th>
328
					<th><?=gettext("Type")?></th>
329
					<th><?=gettext("When")?></th>
330
					<th><?=gettext("Poll (s)")?></th>
331
					<th><?=gettext("Reach")?></th>
332
					<th><?=gettext("Delay (ms)")?></th>
333
					<th><?=gettext("Offset (ms)")?></th>
334
					<th><?=gettext("Jitter (ms)")?></th>
335
				</tr>
336
			</thead>
337
			<tbody id="ntpbody">
338
				<?=print_status()?>
339
			</tbody>
340
		</table>
341
	</div>
342
</div>
343

    
344

    
345
<?php
346

    
347
// GPS satellite information (if available)
348
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
349
	$gps_goo_lnk = 2;
350
	$showgps = 1;
351
?>
352

    
353
<div class="panel panel-default">
354
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("GPS Information");?></h2></div>
355
	<div class="panel-body">
356
		<table class="table table-striped table-hover table-condensed">
357
			<thead>
358
				<tr>
359
					<th><?=gettext("Clock Latitude")?></th>
360
					<th><?=gettext("Clock Longitude")?></th>
361
<?php
362
	if (isset($gps_alt)) {
363
?>
364
					<th><?=gettext("Clock Altitude")?></th>
365
<?php
366
		$gps_goo_lnk++;
367
	}
368

    
369
	if (isset($gps_sat) || isset($gps_satview)) {
370
?>
371
					<th><?=gettext("Satellites")?></th>
372
<?php
373
		$gps_goo_lnk++;
374
	}
375
?>
376
				</tr>
377
			</thead>
378

    
379
			<tbody id="gpsbody">
380
				<?=print_gps()?>
381
			</tbody>
382
		</table>
383
	</div>
384
</div>
385

    
386
<?php
387
endif;
388
?>
389

    
390
<script type="text/javascript">
391
//<![CDATA[
392
events.push(function() {
393
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
394
	do_gps = "no";
395

    
396
	// Fetch the tbody contents from the server
397
	function update_tables() {
398

    
399
		if (ajax_lock) {
400
			return;
401
		}
402

    
403
		ajax_lock = true;
404

    
405
		ajaxRequest = $.ajax(
406
			{
407
				url: "/status_ntpd.php",
408
				type: "post",
409
				data: {
410
					ajax: 	"ajax",
411
					dogps:  do_gps
412
				}
413
			}
414
		);
415

    
416
		// Deal with the results of the above ajax call
417
		ajaxRequest.done(function (response, textStatus, jqXHR) {
418
			if (do_gps == "yes") {
419
				$('#gpsbody').html(response);
420
			} else {
421
				$('#ntpbody').html(response);
422
			}
423

    
424
			ajax_lock = false;
425

    
426
			// Alternate updating the status table and the gps table (if enabled)
427
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
428
				do_gps = "no";
429
			} else {
430
				do_gps = "yes";
431
			}
432

    
433
			// and do it again
434
			setTimeout(update_tables, 5000);
435
		});
436

    
437

    
438
	}
439

    
440
	// Populate the tbody on page load
441
	update_tables();
442
});
443
//]]>
444
</script>
445

    
446
<?php
447
include("foot.inc");
448
?>
(177-177/228)