Project

General

Profile

Download (11.9 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-2019 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($_SERVER['REMOTE_ADDR'], "{$v['acl_network']}/{$v['mask']}")) {
42
			$allow_query = !isset($v['noquery']);
43
		}
44
	}
45
}
46

    
47
if ($allow_query) {
48
	if (isset($config['system']['ipv6allow'])) {
49
		$inet_version = "";
50
	} else {
51
		$inet_version = " -4";
52
	}
53

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

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

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

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

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

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

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

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

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

    
202
	exit;
203
}
204

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

    
208
	if (!$allow_query):
209

    
210
		print("<tr>\n");
211
		print('<td class="warning" colspan="11">');
212
		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>');
213
		print("</td>\n");
214
		print("</tr>\n");
215
	elseif (count($ntpq_servers) == 0):
216
		print("<tr>\n");
217
		print('<td class="warning" colspan="11">');
218
		printf(gettext('No peers found, %1$sis the ntp service running?%2$s'), '<a href="status_services.php">', '</a>');
219
		print("</td>\n");
220
		print("</tr>\n");
221
	else:
222

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

    
243
function print_gps() {
244
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_lat_dir, $gps_lon_dir,
245
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
246

    
247
	print("<tr>\n");
248
	print("<td>\n");
249
	printf("%.5f", $gps_lat);
250
	print(" (");
251
	printf("%d%s", $gps_lat_deg, "&deg;");
252
	printf("%.5f", $gps_lat_min);
253
	print($gps_lat_dir);
254
	print(")");
255
	print("</td>\n");
256
	print("<td>\n");
257
	printf("%.5f", $gps_lon);
258
	print(" (");
259
	printf("%d%s", $gps_lon_deg, "&deg;");
260
	printf("%.5f", $gps_lon_min);
261
	print($gps_lon_dir);
262
	print(")");
263
	print("</td>\n");
264

    
265
	if (isset($gps_alt)) {
266
		print("<td>\n");
267
		print($gps_alt . ' ' . $gps_alt_unit);
268
		print("</td>\n");
269
	}
270

    
271
	if (isset($gps_sat) || isset($gps_satview)) {
272
		print('<td>');
273

    
274
		if (isset($gps_satview)) {
275
			print(gettext('in view ') . intval($gps_satview));
276
		}
277

    
278
		if (isset($gps_sat) && isset($gps_satview)) {
279
			print(', ');
280
		}
281
		if (isset($gps_sat)) {
282
			print(gettext('in use ') . $gps_sat);
283
		}
284

    
285
		print("</td>\n");
286
	}
287

    
288
	print("</tr>\n");
289
	print("<tr>\n");
290
	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>');
291
	print("</tr>\n");
292
}
293

    
294
$pgtitle = array(gettext("Status"), gettext("NTP"));
295
$shortcut_section = "ntp";
296

    
297
include("head.inc");
298
?>
299

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

    
326

    
327
<?php
328

    
329
$showgps = 0;
330

    
331
// GPS satellite information (if available)
332
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
333
	$gps_goo_lnk = 2;
334
	$showgps = 1;
335
?>
336

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

    
353
	if (isset($gps_sat) || isset($gps_satview)) {
354
?>
355
					<th><?=gettext("Satellites")?></th>
356
<?php
357
		$gps_goo_lnk++;
358
	}
359
?>
360
				</tr>
361
			</thead>
362

    
363
			<tbody id="gpsbody">
364
				<?=print_gps()?>
365
			</tbody>
366
		</table>
367
	</div>
368
</div>
369

    
370
<?php
371
endif;
372
?>
373

    
374
<script type="text/javascript">
375
//<![CDATA[
376
events.push(function() {
377
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
378
	do_gps = "no";
379

    
380
	// Fetch the tbody contents from the server
381
	function update_tables() {
382

    
383
		if (ajax_lock) {
384
			return;
385
		}
386

    
387
		ajax_lock = true;
388

    
389
		ajaxRequest = $.ajax(
390
			{
391
				url: "/status_ntpd.php",
392
				type: "post",
393
				data: {
394
					ajax: 	"ajax",
395
					dogps:  do_gps
396
				}
397
			}
398
		);
399

    
400
		// Deal with the results of the above ajax call
401
		ajaxRequest.done(function (response, textStatus, jqXHR) {
402
			if (do_gps == "yes") {
403
				$('#gpsbody').html(response);
404
			} else {
405
				$('#ntpbody').html(response);
406
			}
407

    
408
			ajax_lock = false;
409

    
410
			// Alternate updating the status table and the gps table (if enabled)
411
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
412
				do_gps = "no";
413
			} else {
414
				do_gps = "yes";
415
			}
416

    
417
			// and do it again
418
			setTimeout(update_tables, 5000);
419
		});
420

    
421

    
422
	}
423

    
424
	// Populate the tbody on page load
425
	update_tables();
426
});
427
//]]>
428
</script>
429

    
430
<?php
431
include("foot.inc");
432
?>
(175-175/225)