From d950f9292c7cd91b02b791fe4c545911deb7f3b2 Mon Sep 17 00:00:00 2001 From: nagyrobi Date: Tue, 18 Feb 2014 15:32:11 +0100 Subject: Create ntp_status.widget.php Dedicated widget which has a javascript clock showing the server time accurately, and based on NTP's running state displays information about sync source, GPS state etc. It refreshes contents every minute, without reloading the entire page. --- .../www/widgets/widgets/ntp_status.widget.php | 503 +++++++++++++++++++++ 1 file changed, 503 insertions(+) create mode 100644 usr/local/www/widgets/widgets/ntp_status.widget.php (limited to 'usr/local/www/widgets') diff --git a/usr/local/www/widgets/widgets/ntp_status.widget.php b/usr/local/www/widgets/widgets/ntp_status.widget.php new file mode 100644 index 0000000..405af92 --- /dev/null +++ b/usr/local/www/widgets/widgets/ntp_status.widget.php @@ -0,0 +1,503 @@ +, Manuel Kasper + and Jonathan Watt . + 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. +*/ + +$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"); + +if($_REQUEST['updateme']) { +//this block displays only on ajax refresh + exec("/usr/local/bin/ntpq -pn | /usr/bin/tail +3", $ntpq_output); + $ntpq_counter = 0; + 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 " . $peerinfo[2] . ", " . $peerinfo[1] . ")"; + } else { + $syncsource = $peerinfo[0] . " (stratum " . $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 " . $peerinfo[2] . ", PPS)"; + $ntpq_counter++; + } + } + + exec("/usr/local/bin/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); + $gps_la = $gps_vars[4]; + $gps_lo = $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) / 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]; + $gps_la = $gps_vars[3]; + $gps_lo = $gps_vars[5]; + }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); + $gps_la = $gps_vars[2]; + $gps_lo = $gps_vars[4]; + } + } + } + + 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; + } + } + } +?> + + + + + + + + + + + + + + + + + + + + +
Sync Source + + No active peers available + + + +
Clock location + + + + +
Satellites + +
+. ***/ + +/* Prevent this page from being cached (though some browsers still + cache the page anyway, which is why we use cookies). This is + only important if the cookie is deleted while the page is still + cached (and for ancient browsers that don't know about Cache-Control). + If that's not an issue, you may be able to get away with + "Cache-Control: private" instead. */ + +/* Grab the current server time. */ +$gDate = time(); +/* Are the seconds shown by default? When changing this, also change the + JavaScript client code's definition of clockShowsSeconds below to match. */ +$gClockShowsSeconds = true; + +function getServerDateItems($inDate) { + return date('Y,n,j,G,',$inDate).intval(date('i',$inDate)).','.intval(date('s',$inDate)); + // year (4-digit),month,day,hours (0-23),minutes,seconds + // use intval to strip leading zero from minutes and seconds + // so JavaScript won't try to interpret them in octal + // (use intval instead of ltrim, which translates '00' to '') +} + +function clockDateString($inDate) { + return date('Y. F j l',$inDate); // eg "Monday, January 1, 2002" +} + +function clockTimeString($inDate, $showSeconds) { + return date($showSeconds ? 'G:i:s' : 'g:i',$inDate).' '; +} +/*** Clock -- end of server-side support code ***/ +?> + + + + + + + + + + + + + + + + +
Server Time +
+ +
+
+ +
+ + + + + + +
+ Updating... +
+
+ + -- cgit v1.1 From de3ba9958594eac590e2e39a92fb5eabf8ffa22e Mon Sep 17 00:00:00 2001 From: nagyrobi Date: Tue, 18 Feb 2014 16:01:47 +0100 Subject: Update ntp_status.widget.php Updated errorneous paths. Sorry. --- usr/local/www/widgets/widgets/ntp_status.widget.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'usr/local/www/widgets') diff --git a/usr/local/www/widgets/widgets/ntp_status.widget.php b/usr/local/www/widgets/widgets/ntp_status.widget.php index 405af92..504815c 100644 --- a/usr/local/www/widgets/widgets/ntp_status.widget.php +++ b/usr/local/www/widgets/widgets/ntp_status.widget.php @@ -41,7 +41,7 @@ require_once("/usr/local/www/widgets/include/ntp_status.inc"); if($_REQUEST['updateme']) { //this block displays only on ajax refresh - exec("/usr/local/bin/ntpq -pn | /usr/bin/tail +3", $ntpq_output); + exec("/usr/local/sbin/ntpq -pn | /usr/sbin/tail +3", $ntpq_output); $ntpq_counter = 0; foreach ($ntpq_output as $line) { if (substr($line, 0, 1) == "*") { @@ -63,7 +63,7 @@ if($_REQUEST['updateme']) { } } - exec("/usr/local/bin/ntpq -c clockvar", $ntpq_clockvar_output); + 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); -- cgit v1.1 From fe5ad8afe07d1f654e12f7d2edbbc92c5e3473d0 Mon Sep 17 00:00:00 2001 From: nagyrobi Date: Tue, 18 Feb 2014 18:22:25 +0100 Subject: Update ntp_status.widget.php fixed tail path --- usr/local/www/widgets/widgets/ntp_status.widget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'usr/local/www/widgets') diff --git a/usr/local/www/widgets/widgets/ntp_status.widget.php b/usr/local/www/widgets/widgets/ntp_status.widget.php index 504815c..51ca44f 100644 --- a/usr/local/www/widgets/widgets/ntp_status.widget.php +++ b/usr/local/www/widgets/widgets/ntp_status.widget.php @@ -41,7 +41,7 @@ require_once("/usr/local/www/widgets/include/ntp_status.inc"); if($_REQUEST['updateme']) { //this block displays only on ajax refresh - exec("/usr/local/sbin/ntpq -pn | /usr/sbin/tail +3", $ntpq_output); + exec("/usr/local/sbin/ntpq -pn | /usr/bin/tail +3", $ntpq_output); $ntpq_counter = 0; foreach ($ntpq_output as $line) { if (substr($line, 0, 1) == "*") { -- cgit v1.1