Project

General

Profile

Download (10.1 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 Electric Sheep Fencing, LLC
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

    
49
		switch (substr($line, 0, 1)) {
50
			case " ":
51
				$server['status'] = gettext("Unreach/Pending");
52
				break;
53
			case "*":
54
				$server['status'] = gettext("Active Peer");
55
				break;
56
			case "+":
57
				$server['status'] = gettext("Candidate");
58
				break;
59
			case "o":
60
				$server['status'] = gettext("PPS Peer");
61
				break;
62
			case "#":
63
				$server['status'] = gettext("Selected");
64
				break;
65
			case ".":
66
				$server['status'] = gettext("Excess Peer");
67
				break;
68
			case "x":
69
				$server['status'] = gettext("False Ticker");
70
				break;
71
			case "-":
72
				$server['status'] = gettext("Outlier");
73
				break;
74
		}
75

    
76
		$line = substr($line, 1);
77
		$peerinfo = preg_split("/[\s\t]+/", $line);
78

    
79
		$server['server'] = $peerinfo[0];
80
		$server['refid'] = $peerinfo[1];
81
		$server['stratum'] = $peerinfo[2];
82
		$server['type'] = $peerinfo[3];
83
		$server['when'] = $peerinfo[4];
84
		$server['poll'] = $peerinfo[5];
85
		$server['reach'] = $peerinfo[6];
86
		$server['delay'] = $peerinfo[7];
87
		$server['offset'] = $peerinfo[8];
88
		$server['jitter'] = $peerinfo[9];
89

    
90
		$ntpq_servers[] = $server;
91
	}
92

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

    
139
if (isset($config['ntpd']['gps']['type']) && ($config['ntpd']['gps']['type'] == 'SureGPS') && (isset($gps_ok))) {
140
	//GSV message is only enabled by init commands in services_ntpd_gps.php for SureGPS board
141
	$gpsport = fopen("/dev/gps0", "r+");
142
	while ($gpsport) {
143
		$buffer = fgets($gpsport);
144
		if (substr($buffer, 0, 6) == '$GPGSV') {
145
			//echo $buffer."\n";
146
			$gpgsv = explode(',', $buffer);
147
			$gps_satview = $gpgsv[3];
148
			break;
149
		}
150
	}
151
}
152

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

    
156
	if ($_REQUEST['dogps'] == "yes") {
157
		print_gps();
158
	} else {
159
		print_status();
160
	}
161

    
162
	exit;
163
}
164

    
165
function print_status() {
166
	global $config, $ntpq_servers;
167

    
168
	if (isset($config['ntpd']['noquery'])):
169

    
170
		print("<tr>\n");
171
		print('<td class="warning" colspan="11">');
172
		printf(gettext("Statistics unavailable because ntpq and ntpdc queries are disabled in the %sNTP service settings%s"), '<a href="services_ntpd.php">', '</a>');
173
		print("</td>\n");
174
		print("</tr>\n");
175
	elseif (count($ntpq_servers) == 0):
176
		print("<tr>\n");
177
		print('<td class="warning" colspan="11">');
178
		printf(gettext("No peers found, %sis the ntp service running?%s"), '<a href="status_services.php">', '</a>');
179
		print("</td>\n");
180
		print("</tr>\n");
181
	else:
182

    
183
		$i = 0;
184
		foreach ($ntpq_servers as $server):
185
			print("<tr>\n");
186
			print("<td>" . $server['status'] . "</td>\n");
187
			print("<td>" . $server['server'] . "</td>\n");
188
			print("<td>" . $server['refid'] . "</td>\n");
189
			print("<td>" . $server['stratum'] . "</td>\n");
190
			print("<td>" . $server['type'] . "</td>\n");
191
			print("<td>" . $server['when'] . "</td>\n");
192
			print("<td>" . $server['poll'] . "</td>\n");
193
			print("<td>" . $server['reach'] . "</td>\n");
194
			print("<td>" . $server['delay'] . "</td>\n");
195
			print("<td>" . $server['offset'] . "</td>\n");
196
			print("<td>" . $server['jitter'] . "</td>\n");
197
			print("</tr>\n");
198
			$i++;
199
		endforeach;
200
	endif;
201
}
202

    
203
function print_gps() {
204
	global 	$gps_lat, $gps_lon, $gps_lat_deg, $gps_lon_deg, $gps_lat_min, $gps_lon_min, $gps_vars,
205
			$gps_alt, $gps_alt_unit, $gps_sat, $gps_satview, $gps_goo_lnk;
206

    
207
	print("<tr>\n");
208
	print("<td>\n");
209
	printf("%.5f", $gps_lat);
210
	print(" (");
211
	printf("%d%s", $gps_lat_deg, "&deg;");
212
	printf("%.5f", $gps_lat_min*60);
213
	print($gps_vars[4]);
214
	print(")");
215
	print("</td>\n");
216
	print("<td>\n");
217
	printf("%.5f", $gps_lon);
218
	print(" (");
219
	printf("%d%s", $gps_lon_deg, "&deg;");
220
	printf("%.5f", $gps_lon_min*60);
221
	print($gps_vars[6]);
222
	print(")");
223
	print("</td>\n");
224

    
225
	if (isset($gps_alt)) {
226
		print("<td>\n");
227
		print($gps_alt . ' ' . $gps_alt_unit);
228
		print("</td>\n");
229
	}
230

    
231
	if (isset($gps_sat) || isset($gps_satview)) {
232
		print('<td class="text-center">');
233

    
234
		if (isset($gps_satview)) {
235
			print(gettext('in view ') . intval($gps_satview));
236
		}
237

    
238
		if (isset($gps_sat) && isset($gps_satview)) {
239
			print(', ');
240
		}
241
		if (isset($gps_sat)) {
242
			print(gettext('in use ') . $gps_sat);
243
		}
244

    
245
		print("</td>\n");
246
	}
247

    
248
	print("</tr>\n");
249
	print("<tr>\n");
250
	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>');
251
	print("</tr>\n");
252
}
253

    
254
$pgtitle = array(gettext("Status"), gettext("NTP"));
255
$shortcut_section = "ntp";
256

    
257
include("head.inc");
258
?>
259

    
260
<div class="panel panel-default">
261
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Network Time Protocol Status");?></h2></div>
262
	<div class="panel-body">
263
		<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
264
			<thead>
265
				<tr>
266
					<th><?=gettext("Status")?></th>
267
					<th><?=gettext("Server")?></th>
268
					<th><?=gettext("Ref ID")?></th>
269
					<th><?=gettext("Stratum")?></th>
270
					<th><?=gettext("Type")?></th>
271
					<th><?=gettext("When")?></th>
272
					<th><?=gettext("Poll")?></th>
273
					<th><?=gettext("Reach")?></th>
274
					<th><?=gettext("Delay")?></th>
275
					<th><?=gettext("Offset")?></th>
276
					<th><?=gettext("Jitter")?></th>
277
				</tr>
278
			</thead>
279
			<tbody id="ntpbody">
280
				<?=print_status()?>
281
			</tbody>
282
		</table>
283
	</div>
284
</div>
285

    
286

    
287
<?php
288

    
289
$showgps = 0;
290

    
291
// GPS satellite information (if available)
292
if (($gps_ok) && ($gps_lat) && ($gps_lon)):
293
	$gps_goo_lnk = 2;
294
	$showgps = 1;
295
?>
296

    
297
<div class="panel panel-default">
298
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("GPS Information");?></h2></div>
299
	<div class="panel-body">
300
		<table class="table table-striped table-hover table-condensed">
301
			<thead>
302
				<tr>
303
					<th><?=gettext("Clock Latitude")?></th>
304
					<th><?=gettext("Clock Longitude")?></th>
305
<?php
306
	if (isset($gps_alt)) {
307
?>
308
					<th><?=gettext("Clock Altitude")?></th>
309
<?php
310
		$gps_goo_lnk++;
311
	}
312

    
313
	if (isset($gps_sat) || isset($gps_satview)) {
314
?>
315
					<th><?=gettext("Satellites")?></th>
316
<?php
317
		$gps_goo_lnk++;
318
	}
319
?>
320
				</tr>
321
			</thead>
322

    
323
			<tbody id="gpsbody">
324
				<?=print_gps()?>
325
			</tbody>
326
		</table>
327
	</div>
328
</div>
329

    
330
<?php
331
endif;
332
?>
333

    
334
<script type="text/javascript">
335
//<![CDATA[
336
events.push(function() {
337
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
338
	do_gps = "no";
339

    
340
	// Fetch the tbody contents from the server
341
	function update_tables() {
342

    
343
		if (ajax_lock) {
344
			return;
345
		}
346

    
347
		ajax_lock = true;
348

    
349
		ajaxRequest = $.ajax(
350
			{
351
				url: "/status_ntpd.php",
352
				type: "post",
353
				data: {
354
					ajax: 	"ajax",
355
					dogps:  do_gps
356
				}
357
			}
358
		);
359

    
360
		// Deal with the results of the above ajax call
361
		ajaxRequest.done(function (response, textStatus, jqXHR) {
362
			if (do_gps == "yes") {
363
				$('#gpsbody').html(response);
364
			} else {
365
				$('#ntpbody').html(response);
366
			}
367

    
368
			ajax_lock = false;
369

    
370
			// Alternate updating the status table and the gps table (if enabled)
371
			if ((do_gps == "yes") || ("<?=$showgps?>" != 1)) {
372
				do_gps = "no";
373
			} else {
374
				do_gps = "yes";
375
			}
376

    
377
			// and do it again
378
			setTimeout(update_tables, 5000);
379
		});
380

    
381

    
382
	}
383

    
384
	// Populate the tbody on page load
385
	update_tables();
386
});
387
//]]>
388
</script>
389

    
390
<?php
391
include("foot.inc");
392
?>
(179-179/227)