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-2018 Rubicon Communications, LLC (Netgate)
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
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

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

    
34
require_once("guiconfig.inc");
35

    
36
$allow_query = !isset($config['ntpd']['noquery']);
37

    
38
if (is_ipaddr($_SERVER['REMOTE_ADDR']) && !empty($config['ntpd']['restrictions']['row']) && is_array($config['ntpd']['restrictions']['row'])) {
39
	foreach ($config['ntpd']['restrictions']['row'] as $v) {
40
		if (isset($v['noquery'])) {
41
			continue;
42
		}
43
		if (ip_in_subnet($_SERVER['REMOTE_ADDR'], $v['acl_network'].'/'.$v['mask'])) {
44
			$allow_query = true;
45
		}
46
	}
47
}
48

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

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

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

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

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

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

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

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

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

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

    
204
	exit;
205
}
206

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

    
210
	if (!$allow_query):
211

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

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

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

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

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

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

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

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

    
287
		print("</td>\n");
288
	}
289

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

    
296
$pgtitle = array(gettext("Status"), gettext("NTP"));
297
$shortcut_section = "ntp";
298

    
299
include("head.inc");
300
?>
301

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

    
328

    
329
<?php
330

    
331
$showgps = 0;
332

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

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

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

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

    
372
<?php
373
endif;
374
?>
375

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

    
382
	// Fetch the tbody contents from the server
383
	function update_tables() {
384

    
385
		if (ajax_lock) {
386
			return;
387
		}
388

    
389
		ajax_lock = true;
390

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

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

    
410
			ajax_lock = false;
411

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

    
419
			// and do it again
420
			setTimeout(update_tables, 5000);
421
		});
422

    
423

    
424
	}
425

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

    
432
<?php
433
include("foot.inc");
434
?>
(184-184/234)