summaryrefslogtreecommitdiffstats
path: root/src/usr/local/www/widgets/widgets/ntp_status.widget.php
blob: 4be7074d3468a0a3b788ca9873b6468af1a8b80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
 * ntp_status.widget.php
 *
 * part of pfSense (https://www.pfsense.org)
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$nocsrf = true;

require_once("guiconfig.inc");
require_once("pfsense-utils.inc");
require_once("functions.inc");
require_once("/usr/local/www/widgets/include/ntp_status.inc");

// For this widget the update period is 6 x larger than most others. It typically defaults
// to once per 60 seconds, not once per 10 seconds
$widgetperiod = isset($config['widgets']['period']) ? $config['widgets']['period'] * 1000 * 6 : 60000;

if ($_REQUEST['updateme']) {
//this block displays only on ajax refresh
	if (isset($config['system']['ipv6allow'])) {
		$inet_version = "";
	} else {
		$inet_version = " -4";
	}

	exec("/usr/local/sbin/ntpq -pn -w $inet_version | /usr/bin/tail +3", $ntpq_output);
	$ntpq_counter = 0;
	$stratum_text = gettext("stratum");
	foreach ($ntpq_output as $line) {
		if (substr($line, 0, 1) == "*") {
			//Active NTP Peer
			$line = substr($line, 1);
			$peerinfo = preg_split("/[\s\t]+/", $line);
			if ($peerinfo[2] == "1") {
				$syncsource = $peerinfo[0] . " (" . $stratum_text . " " . $peerinfo[2] . ", " . $peerinfo[1] . ")";
			} else {
				$syncsource = $peerinfo[0] . " (" . $stratum_text . " " . $peerinfo[2] . ")";
			}
			$ntpq_counter++;
		} elseif (substr($line, 0, 1) == "o") {
			//Local PPS Peer
			$line = substr($line, 1);
			$peerinfo = preg_split("/[\s\t]+/", $line);
			$syncsource = $peerinfo[1] . " (" . $stratum_text . " " . $peerinfo[2] . ", PPS)";
			$ntpq_counter++;
		}
	}

	exec("/usr/local/sbin/ntpq -c clockvar $inet_version", $ntpq_clockvar_output);
	foreach ($ntpq_clockvar_output as $line) {
		if (substr($line, 0, 9) == "timecode=") {
			$tmp = explode('"', $line);
			$tmp = $tmp[1];
			if (substr($tmp, 0, 6) == '$GPRMC') {
				$gps_vars = explode(",", $tmp);
				$gps_ok	= ($gps_vars[2] == "A");
				$gps_lat_deg = substr($gps_vars[3], 0, 2);
				$gps_lat_min = substr($gps_vars[3], 2);
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
				$gps_lon_min = substr($gps_vars[5], 3);
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
				$gps_lat_dir = $gps_vars[4];
				$gps_lon_dir = $gps_vars[6];
			} elseif (substr($tmp, 0, 6) == '$GPGGA') {
				$gps_vars = explode(",", $tmp);
				$gps_ok	= $gps_vars[6];
				$gps_lat_deg = substr($gps_vars[2], 0, 2);
				$gps_lat_min = substr($gps_vars[2], 2);
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
				$gps_lon_min = substr($gps_vars[4], 3);
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
				$gps_alt = $gps_vars[9];
				$gps_alt_unit = $gps_vars[10];
				$gps_sat = (int)$gps_vars[7];
				$gps_lat_dir = $gps_vars[3];
				$gps_lon_dir = $gps_vars[5];
			} elseif (substr($tmp, 0, 6) == '$GPGLL') {
				$gps_vars = preg_split('/[,\*]+/', $tmp);
				$gps_ok	= ($gps_vars[6] == "A");
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
				$gps_lat_min = substr($gps_vars[1], 2);
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
				$gps_lon_min = substr($gps_vars[3], 3);
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
				$gps_lat_dir = $gps_vars[2];
				$gps_lon_dir = $gps_vars[4];
			} elseif (substr($tmp, 0, 6) == '$PGRMF') {
				$gps_vars = preg_split('/[,\*]+/', $tmp);
				$gps_ok = $gps_vars[11];
				$gps_lat_deg = substr($gps_vars[6], 0, 2);
				$gps_lat_min = substr($gps_vars[6], 2);
				$gps_lon_deg = substr($gps_vars[8], 0, 3);
				$gps_lon_min = substr($gps_vars[8], 3);
				$gps_lat = $gps_lat_deg + $gps_lat_min / 60.0;
				$gps_lat = $gps_lat * (($gps_vars[7] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min / 60.0;
				$gps_lon = $gps_lon * (($gps_vars[9] == "E") ? 1 : -1);
				$gps_lat_dir = $gps_vars[7];
				$gps_lon_dir = $gps_vars[9];
			}
		}
	}

	if (isset($gps_ok) && isset($config['ntpd']['gps']['extstatus']) && ($config['ntpd']['gps']['nmeaset']['gpgsv'] || $config['ntpd']['gps']['nmeaset']['gpgga'])) {
		$lookfor['GPGSV'] = $config['ntpd']['gps']['nmeaset']['gpgsv'];
		$lookfor['GPGGA'] = !isset($gps_sat) && $config['ntpd']['gps']['nmeaset']['gpgga'];
		$gpsport = fopen('/dev/gps0', 'r+');
		while ($gpsport && ($lookfor['GPGSV'] || $lookfor['GPGGA'])) {
			$buffer = fgets($gpsport);
			if ($lookfor['GPGSV'] && substr($buffer, 0, 6) == '$GPGSV') {
				$gpgsv = explode(',', $buffer);
				$gps_satview = (int)$gpgsv[3];
				$lookfor['GPGSV'] = 0;
			} elseif ($lookfor['GPGGA'] && substr($buffer, 0, 6) == '$GPGGA') {
				$gpgga = explode(',', $buffer);
				$gps_sat = (int)$gpgga[7];
				$gps_alt = $gpgga[9];
				$gps_alt_unit = $gpgga[10];
				$lookfor['GPGGA'] = 0;
			}
		}
	}
?>

<table id="ntp_status_widget" class="table table-striped table-hover">
	<tr>
		<th><?=gettext('Server Time')?></th>
		<td id="ClockTime">
			<!-- will be replaced by javascript -->
			<span id="ntpStatusClock"></span>
		</td>
	</tr>
	<tr>
		<th><?=gettext('Sync Source')?></th>
		<td>
		<?php if ($ntpq_counter == 0): ?>
			<i><?=gettext('No active peers available')?></i>
		<?php else: ?>
			<?=$syncsource;?>
		<?php endif; ?>
		</td>
	</tr>
	<?php if (($gps_ok) && ($gps_lat) && ($gps_lon)): ?>
		<tr>
			<th><?=gettext('Clock location')?></th>
			<td>
				<a target="_gmaps" href="http://maps.google.com/?q=<?=$gps_lat;?>,<?=$gps_lon;?>">
				<?php
				echo sprintf("%.5f", $gps_lat) . " " . $gps_lat_dir . ", " . sprintf("%.5f", $gps_lon) . " " . $gps_lon_dir; ?>
				</a>
				<?php if (isset($gps_alt)) {echo " (" . $gps_alt . " " . $gps_alt_unit . " alt.)";} ?>
			</td>
		</tr>
		<?php if (isset($gps_sat) || isset($gps_satview)): ?>
			<tr>
				<th><?=gettext('Satellites')?></th>
				<td>
				<?php
				if (isset($gps_satview)) {echo gettext('in view') . ' ' . intval($gps_satview);}
				if (isset($gps_sat) && isset($gps_satview)) {echo ', ';}
				if (isset($gps_sat)) {echo gettext('in use') . ' ' . $gps_sat;}
				?>
				</td>
			</tr>
		<?php endif; ?>
	<?php endif; ?>
</table>

<?php
	exit;
}
?>
<?php if ($widget_first_instance): ?>
<script type="text/javascript">
//<![CDATA[
var d = new Date('<?=date_format(date_create(), 'c')?>');
var tz = '<?=date('T');?>';

setInterval(function() {
	d.setSeconds(d.getSeconds() + 1);
	var thisSecond = d.getSeconds();
	var thisMinute = d.getMinutes();
	var thisHour = d.getHours();

	// Add leading zeros to minutes and seconds as required
	thisMinute = thisMinute < 10 ? "0" + thisMinute : thisMinute;
	thisSecond = thisSecond < 10 ? "0" + thisSecond : thisSecond;

	$('[id="ntpStatusClock"]').html(thisHour +':' + thisMinute + ':' + thisSecond + ' ' + tz);
}, 1000);
//]]>
</script>
<?php endif; ?>
<table id="ntpstatus" class="table table-striped table-hover">
	<tbody>
		<tr>
			<td>
				<?=gettext('Updating...')?>
			</td>
		</tr>
	</tbody>
</table>
<?php if ($widget_first_instance): ?>
<script type="text/javascript">
//<![CDATA[

events.push(function(){
	// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------

	// Callback function called by refresh system when data is retrieved
	function ntp_callback(s) {
		$('[id="ntpstatus"]').prop('innerHTML', s);
	}

	// POST data to send via AJAX
	var postdata = {
		ajax: "ajax",
	 	updateme : "yes"
	 };

	// Create an object defining the widget refresh AJAX call
	var ntpObject = new Object();
	ntpObject.name = "NTP";
	ntpObject.url = "/widgets/widgets/ntp_status.widget.php";
	ntpObject.callback = ntp_callback;
	ntpObject.parms = postdata;
	ntpObject.freq = 4;

	// Register the AJAX object
	register_ajax(ntpObject);

	// ---------------------------------------------------------------------------------------------------
});

//]]>
</script>
<?php endif; ?>
OpenPOWER on IntegriCloud