status_ntpd.php 9.47 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php
/*
3
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
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
	Copyright (C) 2013 Dagorlad
	Copyright (C) 2012 Jim Pingle
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

require_once("guiconfig.inc");
31
require_once("services.inc");
32
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
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

if(!isset($config['ntpd']['noquery'])) {

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

	$ntpq_servers = array();
	foreach ($ntpq_output as $line) {
		$server = array();

		switch (substr($line, 0, 1)) {
			case " ":
				$server['status'] = "Unreach/Pending";
				break;
			case "*":
				$server['status'] = "Active Peer";
				break;
			case "+":
				$server['status'] = "Candidate";
				break;
			case "o":
				$server['status'] = "PPS Peer";
				break;
			case "#":
				$server['status'] = "Selected";
				break;
			case ".":
				$server['status'] = "Excess Peer";
				break;
			case "x":
				$server['status'] = "False Ticker";
				break;
			case "-":
				$server['status'] = "Outlier";
				break;
		}

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

		$server['server'] = $peerinfo[0];
		$server['refid'] = $peerinfo[1];
		$server['stratum'] = $peerinfo[2];
		$server['type'] = $peerinfo[3];
		$server['when'] = $peerinfo[4];
		$server['poll'] = $peerinfo[5];
		$server['reach'] = $peerinfo[6];
		$server['delay'] = $peerinfo[7];
		$server['offset'] = $peerinfo[8];
		$server['jitter'] = $peerinfo[9];

		$ntpq_servers[] = $server;
	}

	exec("/usr/local/sbin/ntpq -c clockvar", $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) / 60.0;
				$gps_lon_deg = substr($gps_vars[5], 0, 3);
				$gps_lon_min = substr($gps_vars[5], 3) / 60.0;
				$gps_lat = $gps_lat_deg + $gps_lat_min;
				$gps_lat = $gps_lat * (($gps_vars[4] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min;
				$gps_lon = $gps_lon * (($gps_vars[6] == "E") ? 1 : -1);
			}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) / 60.0;
				$gps_lon_deg = substr($gps_vars[4], 0, 3);
				$gps_lon_min = substr($gps_vars[4], 3) / 60.0;
				$gps_lat = $gps_lat_deg + $gps_lat_min;
				$gps_lat = $gps_lat * (($gps_vars[3] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min;
				$gps_lon = $gps_lon * (($gps_vars[5] == "E") ? 1 : -1);
				$gps_alt = $gps_vars[9];
				$gps_alt_unit = $gps_vars[10];
				$gps_sat = $gps_vars[7];
			}elseif (substr($tmp, 0, 6) == '$GPGLL') {
				$gps_vars = explode(",", $tmp);
				$gps_ok  = ($gps_vars[6] == "A");
				$gps_lat_deg = substr($gps_vars[1], 0, 2);
				$gps_lat_min = substr($gps_vars[1], 2) / 60.0;
				$gps_lon_deg = substr($gps_vars[3], 0, 3);
				$gps_lon_min = substr($gps_vars[3], 3) / 60.0;
				$gps_lat = $gps_lat_deg + $gps_lat_min;
				$gps_lat = $gps_lat * (($gps_vars[2] == "N") ? 1 : -1);
				$gps_lon = $gps_lon_deg + $gps_lon_min;
				$gps_lon = $gps_lon * (($gps_vars[4] == "E") ? 1 : -1);
			}
		}
	}

}

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

$pgtitle = array(gettext("Status"),gettext("NTP"));
$shortcut_section = "ntp";
include("head.inc");
?>
151 152 153


<body>
Ad Schellevis's avatar
Ad Schellevis committed
154
<?php include("fbegin.inc"); ?>
155 156


157

158 159
<section class="page-content-main">
	<div class="container-fluid">
160

161
        <div class="row">
162

163 164
            <section class="col-xs-12">
                <div class="content-box">
Ad Schellevis's avatar
Ad Schellevis committed
165
	                <header class="content-box-head container-fluid">
166 167
				        <h3>Network Time Protocol Status</h3>
				    </header>
168

Ad Schellevis's avatar
Ad Schellevis committed
169
				    <div class="content-box-main">
170 171 172
						<div class="table-responsive">
				<table class="table table-striped table-sort sortable __nomb">

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

								<thead>
								<tr>
									<th class="listhdrr"><?=gettext("Status"); ?></th>
									<th class="listhdrr"><?=gettext("Server"); ?></th>
									<th class="listhdrr"><?=gettext("Ref ID"); ?></th>
									<th class="listhdrr"><?=gettext("Stratum"); ?></th>
									<th class="listhdrr"><?=gettext("Type"); ?></th>
									<th class="listhdrr"><?=gettext("When"); ?></th>
									<th class="listhdrr"><?=gettext("Poll"); ?></th>
									<th class="listhdrr"><?=gettext("Reach"); ?></th>
									<th class="listhdrr"><?=gettext("Delay"); ?></th>
									<th class="listhdrr"><?=gettext("Offset"); ?></th>
									<th class="listhdr"><?=gettext("Jitter"); ?></th>
								</tr>
								</thead>
								<tbody>
								<?php if (isset($config['ntpd']['noquery'])): ?>
								<tr><td class="listlr" colspan="11" align="center">
									Statistics unavailable because ntpq and ntpdc queries are disabled in the <a href="services_ntpd.php">NTP service settings</a>.
								</td></tr>
								<?php elseif (count($ntpq_servers) == 0): ?>
								<tr><td class="listlr" colspan="11" align="center">
									No peers found, <a href="status_services.php">is the ntp service running?</a>.
								</td></tr>
								<?php else: ?>
								<?php $i = 0; foreach ($ntpq_servers as $server): ?>
								<tr>
								<td class="listlr nowrap">
									<?=$server['status'];?>
								</td>
								<td class="listr">
									<?=$server['server'];?>
								</td>
								<td class="listr">
									<?=$server['refid'];?>
								</td>
								<td class="listr">
									<?=$server['stratum'];?>
								</td>
								<td class="listr">
									<?=$server['type'];?>
								</td>
								<td class="listr">
									<?=$server['when'];?>
								</td>
								<td class="listr">
									<?=$server['poll'];?>
								</td>
								<td class="listr">
									<?=$server['reach'];?>
								</td>
								<td class="listr">
									<?=$server['delay'];?>
								</td>
								<td class="listr">
									<?=$server['offset'];?>
								</td>
								<td class="listr">
									<?=$server['jitter'];?>
								</td>
								</tr>
							<?php $i++; endforeach; endif; ?>
								</tbody>
								</table>
							<?php if (($gps_ok) && ($gps_lat) && ($gps_lon)): ?>
								<?php $gps_goo_lnk = 2; ?>
								<table class="table table-striped table-sort sortable">
								<thead>
								<tr>
									<th class="listhdrr"><?=gettext("Clock Latitude"); ?></th>
									<th class="listhdrr"><?=gettext("Clock Longitude"); ?></th>
									<?php if (isset($gps_alt)) { echo '<th class="listhdrr">' . gettext("Clock Altitude") . '</th>'; $gps_goo_lnk++;}?>
									<?php if (isset($gps_sat) || isset($gps_satview)) { echo '<th class="listhdrr">' . gettext("Satellites") . '</th>'; $gps_goo_lnk++;}?>
								</tr>
								</thead>
								<tbody>
									<tr>
										<td class="listlr" align="center"><?php echo sprintf("%.5f", $gps_lat); ?> (<?php echo sprintf("%d", $gps_lat_deg); ?>&deg; <?php echo sprintf("%.5f", $gps_lat_min*60); ?><?php echo $gps_vars[4]; ?>)</td>
										<td class="listlr" align="center"><?php echo sprintf("%.5f", $gps_lon); ?> (<?php echo sprintf("%d", $gps_lon_deg); ?>&deg; <?php echo sprintf("%.5f", $gps_lon_min*60); ?><?php echo $gps_vars[6]; ?>)</td>
										<?php if (isset($gps_alt)) { echo '<td class="listlr" align="center">' . $gps_alt . ' ' . $gps_alt_unit . '</td>';}?>
										<td class="listr" align="center">
255
										<?php
256 257
										if (isset($gps_satview)) {echo 'in view ' . intval($gps_satview);}
										if (isset($gps_sat) && isset($gps_satview)) {echo ', ';}
258
										if (isset($gps_sat)) {echo 'in use ' . $gps_sat;}
259 260 261 262 263 264 265 266 267
										?>
										</td>
									</tr>
									<tr>
										<td class="listlr" colspan="<?php echo $gps_goo_lnk; ?>" align="center"><a target="_gmaps" href="http://maps.google.com/?q=<?php echo $gps_lat; ?>,<?php echo $gps_lon; ?>">Google Maps Link</a></td>
									</tr>
								</tbody>
								</table>
							<?php endif; ?>
268
						</div>
269 270 271 272 273 274 275 276
				    </div>
                </div>
            </section>
        </div>
	</div>
</section>

<?php include("foot.inc"); ?>