#!/usr/bin/env php
<?php

    ///////////////////////////////////////////////////////////////////////////////////////
    ///
    //      A small script to grab the NTPD Server statistics from a NTPD server
    //      Needed commands: php, ntpd, ntpq, ntpdc
    ///
    //      Version 1.3 By:
    //              All In One - Dennis de Houx <info@all-in-one.be>
    ///
    ///////////////////////////////////////////////////////////////////////////////////////


    // START SETTINGS ///

	$ntpq		= "ntpq";
	$ntpdc		= "ntpq";

    // END SETTINGS ///



    ///
    // DO NOT EDIT BENEATH THIS LINE
    ///
    ///////////////////////////////////////////////////////////////////////////////////////

	$cmd	= shell_exec($ntpq." -c rv");
	$cmd2	= shell_exec($ntpdc." -c iostats");
	$vars	= array();
	$vars2	= array();
	$vars	= explode(',', $cmd);
	//$vars2	= eregi_replace(' ', '', $cmd2);
	$vars2	= preg_replace('/ /', '', $cmd2);
	$vars2	= explode("\n", $vars2);

	function doUnixAgent($vars, $vars2) {
	    $ntpd	= array();
	    foreach ($vars as $item=>$value) {
		if (!empty($value)) {
		    $temp = explode('=', $value);
		    if (isset($temp[1])) {
			$ntpd[trim($temp[0])] = trim($temp[1]);
		    }
		}
	    }
	    foreach ($vars2 as $item=>$value) {
		if (!empty($value)) {
		    $temp = explode(':', $value);
		    if (isset($temp[1])) {
			$ntpd[trim($temp[0])] = trim($temp[1]);
		    }
		}
	    }
	    $var['version']			= (isset($ntpd['version']) ? $ntpd['version'] : "N/A");
	    $var['stratum']			= (isset($ntpd['stratum']) ? $ntpd['stratum'] : "U");
	    $var['offset']			= (isset($ntpd['offset']) ? $ntpd['offset'] : "U");
	    $var['frequency']			= (isset($ntpd['frequency']) ? $ntpd['frequency'] : "U");
	    if (isset($ntpd['clk_jitter'])) {
		$var['jitter']			= (isset($ntpd['clk_jitter']) ? $ntpd['clk_jitter'] : "U");
		$var['noise']			= (isset($ntpd['sys_jitter']) ? $ntpd['sys_jitter'] : "U");
		$var['stability']		= (isset($ntpd['clk_wander']) ? $ntpd['clk_wander'] : "U");
	    } else {
		$var['jitter']			= (isset($ntpd['jitter']) ? $ntpd['jitter'] : "U");
		$var['noise']			= (isset($ntpd['noise']) ? $ntpd['noise'] : "U");
		$var['stability']		= (isset($ntpd['stability']) ? $ntpd['stability'] : "U");
	    }
	    if (!empty($ntpdc) || $var['stratum'] < 16) {
		$var['server']			= true;
		$var['uptime']			= (isset($ntpd['timesincereset']) ? $ntpd['timesincereset'] : "U");
		$var['buffer_recv']		= (isset($ntpd['receivebuffers']) ? $ntpd['receivebuffers'] : "U");
		$var['buffer_free']		= (isset($ntpd['freereceivebuffers']) ? $ntpd['freereceivebuffers'] : "U");
		$var['buffer_used']		= (isset($ntpd['usedreceivebuffers']) ? $ntpd['usedreceivebuffers'] : "U");
		$var['packets_drop']		= (isset($ntpd['droppedpackets']) ? $ntpd['droppedpackets'] : "U");
		$var['packets_ignore']		= (isset($ntpd['ignoredpackets']) ? $ntpd['ignoredpackets'] : "U");
		$var['packets_recv']		= (isset($ntpd['receivedpackets']) ? $ntpd['receivedpackets'] : "U");
		$var['packets_sent']		= (isset($ntpd['packetssent']) ? $ntpd['packetssent'] : "U");
	    }
	    echo "<<<app-ntpd>>>\n";
	    foreach ($var as $item=>$count) {
		echo $item.":".$count."\n";
	    }
	}

	doUnixAgent($vars, $vars2);

?>
