Project

General

Profile

Download (10.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-2016 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
if (!isset($config['ntpd']['noquery'])) {
37
	if (isset($config['system']['ipv6allow'])) {
38
		$inet_version = "";
39
	} else {
40
		$inet_version = " -4";
41
	}
42

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

    
45
	$ntpq_servers = array();
46
	foreach ($ntpq_output as $line) {
47
		$server = array();
48
		$status_char = substr($line, 0, 1);
49
		$line = substr($line, 1);
50
		$peerinfo = preg_split("/[\s\t]+/", $line);
51

    
52
		$server['server'] = $peerinfo[0];
53
		$server['refid'] = $peerinfo[1];
54
		$server['stratum'] = $peerinfo[2];
55
		$server['type'] = $peerinfo[3];
56
		$server['when'] = $peerinfo[4];
57
		$server['poll'] = $peerinfo[5];
58
		$server['reach'] = $peerinfo[6];
59
		$server['delay'] = $peerinfo[7];
60
		$server['offset'] = $peerinfo[8];
61
		$server['jitter'] = $peerinfo[9];
62

    
63
		switch ($status_char) {
64
			case " ":
65
				if ($server['refid'] == ".POOL.") {
66
					$server['status'] = gettext("Pool Placeholder");
67
				} else {
68
					$server['status'] = gettext("Unreach/Pending");
69
				}
70
				break;
71
			case "*":
72
				$server['status'] = gettext("Active Peer");
73
				break;
74
			case "+":
75
				$server['status'] = gettext("Candidate");
76
				break;
77
			case "o":
78
				$server['status'] = gettext("PPS Peer");
79
				break;
80
			case "#":
81
				$server['status'] = gettext("Selected");
82
				break;
83
			case ".":
84
				$server['status'] = gettext("Excess Peer");
85
				break;
86
			case "x":
87
				$server['status'] = gettext("False Ticker");
88
				break;
89
			case "-":
90
				$server['status'] = gettext("Outlier");
91
				break;
92
		}
93

    
94
		$ntpq_servers[] = $server;
95
	}
96

    
97
	exec("/usr/local/sbin/ntpq -c clockvar $inet_version", $ntpq_clockvar_output);
98
	foreach ($ntpq_clockvar_output as $line) {
99
		if (substr($line, 0, 9) == "timecode=") {
100
			$tmp = explode('"', $line);
101
			$tmp = $tmp[1];
102
			if (substr($tmp, 0, 6) == '$GPRMC') {
103
				$gps_vars = explode(",", $tmp);
104
				$gps_ok = ($gps_vars[2] == "A");
105
				$gps_lat_deg = substr($gps_vars[3], 0, 2);
106
				$gps_lat_min = substr($gps_vars[3], 2) / 60.0;
107
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
108
				$gps_lon_min = substr($gps_vars[5], 3) / 60.0;
109
				$gps_lat = $gps_lat_deg + $gps_lat_min;
110
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
111
				$gps_lon = $gps_lon_deg + $gps_lon_min;
112
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
113
				$gps_la = $gps_vars[4];
114
				$gps_lo = $gps_vars[6];
115
			} elseif (substr($tmp, 0, 6) == '$GPGGA') {
116
				$gps_vars = explode(",", $tmp);
117
				$gps_ok = $gps_vars[6];
118
				$gps_lat_deg = substr($gps_vars[2], 0, 2);
119
				$gps_lat_min = substr($gps_vars[2], 2) / 60.0;
120
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
121
				$gps_lon_min = substr($gps_vars[4], 3) / 60.0;
122
				$gps_lat = $gps_lat_deg + $gps_lat_min;
123
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
124
				$gps_lon = $gps_lon_deg + $gps_lon_min;
125
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
126
				$gps_alt = $gps_vars[9];
127
				$gps_alt_unit = $gps_vars[10];
128
				$gps_sat = (int)$gps_vars[7];
129
				$gps_la = $gps_vars[3];
130
				$gps_lo = $gps_vars[5];
131
			} elseif (substr($tmp, 0, 6) == '$GPGLL') {
132
				$gps_vars = preg_split('/[,\*]+/', $tmp);
133
				$gps_ok = ($gps_vars[6] == "A");
134
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
135
				$gps_lat_min = substr($gps_vars[1], 2) / 60.0;
136
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
137
				$gps_lon_min = substr($gps_vars[3], 3) / 60.0;
138
				$gps_lat = $gps_lat_deg + $gps_lat_min;
139
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
140
				$gps_lon = $gps_lon_deg + $gps_lon_min;
141
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
142
				$gps_la = $gps_vars[2];
143
				$gps_lo = $gps_vars[4];
144
			}
145
		}
146
	}
147
}
148

    
149
if (isset($config['ntpd']['gps']['type']) && ($config['ntpd']['gps']['type'] == 'SureGPS') && (isset($gps_ok))) {
150
	//GSV message is only enabled by init commands in services_ntpd_gps.php for SureGPS board
151
	$gpsport = fopen("/dev/gps0", "r+");
152
	while ($gpsport) {
153
		$buffer = fgets($gpsport);
154
		if (substr($buffer, 0, 6) == '$GPGSV') {
155
			//echo $buffer."\n";
156
			$gpgsv = explode(',', $buffer);
157
			$gps_satview = $gpgsv[3];
158
			break;
159
		}
160
	}
161
}
162

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

    
166
	if ($_REQUEST['dogps'] == "yes") {
167
		print_gps();
168
	} else {
169
		print_status();
170
	}
171

    
172
	exit;
173
}
174

    
175
function print_status() {
176
	global $config, $ntpq_servers;
177

    
178
	if (isset($config['ntpd']['noquery'])):
179

    
180
		print("<tr>\n");
181
		print('<td class="warning" colspan="11">');
182
		printf(gettext("Statistics unavailable because ntpq and ntpdc queries are disabled in the %sNTP service settings%s"), '<a href="services_ntpd.php">', '</a>');
183
		print("</td>\n");
184
		print("</tr>\n");
185
	elseif (count($ntpq_servers) == 0):
186
		print("<tr>\n");
187
		print('<td class="warning" colspan="11">');
188
		printf(gettext("No peers found, %sis the ntp service running?%s"), '<a href="status_services.php">', '</a>');
189
		print("</td>\n");
190
		print("</tr>\n");
191
	else:
192

    
193
		$i = 0;
194
		foreach ($ntpq_servers as $server):
195
			print("<tr>\n");
196
			print("<td>" . $server['status'] . "</td>\n");
197
			print("<td>" . $server['server'] . "</td>\n");
198
			print("<td>" . $server['refid'] . "</td>\n");
199
			print("<td>" . $server['stratum'] . "</td>\n");
200
			print("<td>" . $server['type'] . "</td>\n");
201
			print("<td>" . $server['when'] . "</td>\n");
202
			print("<td>" . $server['poll'] . "</td>\n");
203
			print("<td>" . $server['reach'] . "</td>\n");
204
			print("<td>" . $server['delay'] . "</td>\n");
205
			print("<td>" . $server['offset'] . "</td>\n");
206
			print("<td>" . $server['jitter'] . "</td>\n");
207
			print("</tr>\n");
208
			$i++;
209
		endforeach;
210
	endif;
211
}
212

    
213
function print_gps() {
214
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_la, $gps_lo,
215
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
216

    
217
	print("<tr>\n");
218
	print("<td>\n");
219
	printf("%.5f", $gps_lat);
220
	print(" (");
221
	printf("%d%s", $gps_lat_deg, "&deg;");
222
	printf("%.5f", $gps_lat_min*60);
223
	print($gps_la);
224
	print(")");
225
	print("</td>\n");
226
	print("<td>\n");
227
	printf("%.5f", $gps_lon);
228
	print(" (");
229
	printf("%d%s", $gps_lon_deg, "&deg;");
230
	printf("%.5f", $gps_lon_min*60);
231
	print($gps_lo);
232
	print(")");
233
	print("</td>\n");
234

    
235
	if (isset($gps_alt)) {
236
		print("<td>\n");
237
		print($gps_alt . ' ' . $gps_alt_unit);
238
		print("</td>\n");
239
	}
240

    
241
	if (isset($gps_sat) || isset($gps_satview)) {
242
		print('<td>');
243

    
244
		if (isset($gps_satview)) {
245
			print(gettext('in view ') . intval($gps_satview));
246
		}
247

    
248
		if (isset($gps_sat) && isset($gps_satview)) {
249
			print(', ');
250
		}
251
		if (isset($gps_sat)) {
252
			print(gettext('in use ') . $gps_sat);
253
		}
254

    
255
		print("</td>\n");
256
	}
257

    
258
	print("</tr>\n");
259
	print("<tr>\n");
260
	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>');
261
	print("</tr>\n");
262
}
263

    
264
$pgtitle = array(gettext("Status"), gettext("NTP"));
265
$shortcut_section = "ntp";
266

    
267
include("head.inc");
268
?>
269

    
270
<div class="panel panel-default">
271
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Network Time Protocol Status");?></h2></div>
272
	<div class="panel-body">
273
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
274
			<thead>
275
				<tr>
276
					<th><?=gettext("Status")?></th>
277
					<th><?=gettext("Server")?></th>
278
					<th><?=gettext("Ref ID")?></th>
279
					<th><?=gettext("Stratum")?></th>
280
					<th><?=gettext("Type")?></th>
281
					<th><?=gettext("When")?></th>
282
					<th><?=gettext("Poll")?></th>
283
					<th><?=gettext("Reach")?></th>
284
					<th><?=gettext("Delay")?></th>
285
					<th><?=gettext("Offset")?></th>
286
					<th><?=gettext("Jitter")?></th>
287
				</tr>
288
			</thead>
289
			<tbody id="ntpbody">
290
				<?=print_status()?>
291
			</tbody>
292
		</table>
293
	</div>
294
</div>
295

    
296

    
297
<?php
298

    
299
$showgps = 0;
300

    
301
// GPS satellite information (if available)
302
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
303
	$gps_goo_lnk = 2;
304
	$showgps = 1;
305
?>
306

    
307
<div class="panel panel-default">
308
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("GPS Information");?></h2></div>
309
	<div class="panel-body">
310
		<table class="table table-striped table-hover table-condensed">
311
			<thead>
312
				<tr>
313
					<th><?=gettext("Clock Latitude")?></th>
314
					<th><?=gettext("Clock Longitude")?></th>
315
<?php
316
	if (isset($gps_alt)) {
317
?>
318
					<th><?=gettext("Clock Altitude")?></th>
319
<?php
320
		$gps_goo_lnk++;
321
	}
322

    
323
	if (isset($gps_sat) || isset($gps_satview)) {
324
?>
325
					<th><?=gettext("Satellites")?></th>
326
<?php
327
		$gps_goo_lnk++;
328
	}
329
?>
330
				</tr>
331
			</thead>
332

    
333
			<tbody id="gpsbody">
334
				<?=print_gps()?>
335
			</tbody>
336
		</table>
337
	</div>
338
</div>
339

    
340
<?php
341
endif;
342
?>
343

    
344
<script type="text/javascript">
345
//<![CDATA[
346
events.push(function() {
347
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
348
	do_gps = "no";
349

    
350
	// Fetch the tbody contents from the server
351
	function update_tables() {
352

    
353
		if (ajax_lock) {
354
			return;
355
		}
356

    
357
		ajax_lock = true;
358

    
359
		ajaxRequest = $.ajax(
360
			{
361
				url: "/status_ntpd.php",
362
				type: "post",
363
				data: {
364
					ajax: 	"ajax",
365
					dogps:  do_gps
366
				}
367
			}
368
		);
369

    
370
		// Deal with the results of the above ajax call
371
		ajaxRequest.done(function (response, textStatus, jqXHR) {
372
			if (do_gps == "yes") {
373
				$('#gpsbody').html(response);
374
			} else {
375
				$('#ntpbody').html(response);
376
			}
377

    
378
			ajax_lock = false;
379

    
380
			// Alternate updating the status table and the gps table (if enabled)
381
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
382
				do_gps = "no";
383
			} else {
384
				do_gps = "yes";
385
			}
386

    
387
			// and do it again
388
			setTimeout(update_tables, 5000);
389
		});
390

    
391

    
392
	}
393

    
394
	// Populate the tbody on page load
395
	update_tables();
396
});
397
//]]>
398
</script>
399

    
400
<?php
401
include("foot.inc");
402
?>
(177-177/225)