From b6bdb9ea45d579becc8f858090e8b7d3e4c809ea Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 15 Aug 2016 22:56:31 -0500 Subject: [PATCH] Copy ntp scripts from the main repo. --- snmp/ntp-client.php | 79 +++++++++++++++++++++++++++++++++++++++++ snmp/ntpd-server.php | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100755 snmp/ntp-client.php create mode 100755 snmp/ntpd-server.php diff --git a/snmp/ntp-client.php b/snmp/ntp-client.php new file mode 100755 index 0000000..9332791 --- /dev/null +++ b/snmp/ntp-client.php @@ -0,0 +1,79 @@ +#!/usr/bin/env php + +// + +// START SETTINGS /// +$ntpq = '/usr/sbin/ntpq'; +// Change this to true if you have clk_jitter, sys_jitter in the ntpq -c rv output +$newstats_style = false; +// END SETTINGS /// + +// DO NOT EDIT UNDER THIS LINE + +$cmd = shell_exec($ntpq." -c rv | grep '^offset'"); +if ($newstats_style) { + $cmd2 = shell_exec($ntpq." -c rv | grep '^clk_wander'"); +} +else { + $cmd2 = shell_exec($ntpq." -c rv | grep '^stability'"); +} + +$vars = array(); +$vars2 = array(); +$vars = explode(',', $cmd); +$vars2 = explode(',', $cmd2); + + +function doSNMPv2($vars, $vars2, $newstats_style) +{ + $ntp = array(); + foreach ($vars as $item => $value) { + if (!empty($value)) { + $temp = explode('=', $value); + if (isset($temp[1])) { + $ntp[trim($temp[0])] = trim($temp[1]); + } + } + } + + foreach ($vars2 as $item => $value) { + if (!empty($value)) { + $temp = explode('=', $value); + if (isset($temp[1])) { + $ntp[trim($temp[0])] = trim($temp[1]); + } + } + } + + $var = array(); + $var['offset'] = (isset($ntp['offset']) ? $ntp['offset'] : 'U'); + $var['frequency'] = (isset($ntp['frequency']) ? $ntp['frequency'] : 'U'); + if ($newstats_style) { + $var['jitter'] = (isset($ntp['clk_jitter']) ? $ntp['clk_jitter'] : 'U'); + $var['noise'] = (isset($ntp['sys_jitter']) ? $ntp['sys_jitter'] : 'U'); + $var['stability'] = (isset($ntp['clk_wander']) ? $ntp['clk_wander'] : 'U'); + } + else { + $var['jitter'] = (isset($ntp['jitter']) ? $ntp['jitter'] : 'U'); + $var['noise'] = (isset($ntp['noise']) ? $ntp['noise'] : 'U'); + $var['stability'] = (isset($ntp['stability']) ? $ntp['stability'] : 'U'); + } + + foreach ($var as $item => $count) { + echo $count."\n"; + } + +}//end doSNMPv2() + + +doSNMPv2($vars, $vars2, $newstats_style); diff --git a/snmp/ntpd-server.php b/snmp/ntpd-server.php new file mode 100755 index 0000000..aba7b1f --- /dev/null +++ b/snmp/ntpd-server.php @@ -0,0 +1,83 @@ +#!/usr/bin/env php + + +// START SETTINGS /// +$ntpq = '/usr/sbin/ntpq'; +$ntpdc = '/usr/sbin/ntpdc'; +// Change this to true if you have clk_jitter, sys_jitter in the ntpq -c rv output +$newstats_style = false; +// END SETTINGS /// + +// DO NOT EDIT UNDER THIS LINE + +$cmd = shell_exec($ntpq.' -c rv'); +$cmd2 = shell_exec($ntpdc.' -c iostats'); +$vars = array(); +$vars2 = array(); +$vars = explode(',', $cmd); +$vars2 = str_replace(' ', '', $cmd2); +$vars2 = explode("\n", $vars2); + + +function doSNMPv2($vars, $vars2, $newstats_style) +{ + $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 = array(); + $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 ($newstats_style) { + $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'); + } + + $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'); + foreach ($var as $item => $count) { + echo $count."\n"; + } + +}//end doSNMPv2() + + +doSNMPv2($vars, $vars2, $newstats_style);