Script to Collection NTP information in CentOS/RHEL Server
This script collects information about the configuration and the status of a Linux NTP daemon and all system information needed for troubleshooting an NTP issue and other time-related problems. This script will need a running NTP service to collect the status information. A temporary directory /tmp/ntpd will be created and deleted after finishing the execution of the script. The output file can be found in /tmp and is named ntpd.tar.gz.
1. This script should be run as user root.
2. On CentOS/OL6/RHEL6, /usr/sbin/ntptrace command is provided by rpm package ntp-perl, so need to install rpm ntp-perl for this script, otherwise it will report warning below:
./ntp.sh: line 53: ntptrace: command not found
You can install rpm package ntp-perl from ULN by command ‘yum install ntp-perl’
# yum install ntp-perl
3. To run the script execute it as shown below:
# sh ntp-conf.sh
The script
The script is as follows:
#!/bin/sh
targetdir=/tmp/ntpd
#creating temporary directory
if [ -d $targetdir ]
then
echo "Temp directory $targetdir exists already."
else
mkdir $targetdir
fi
collect_file () {
if [ -f "$1" ]; then
cp -p "$1" "$targetdir"\/"$(echo "$1" | sed -e 's/^\/[^/]*\///' -e 's/\//-/g')"
fi
}
echo "Collecting NTP configuration files "
#collect ntp configurations
conf=/etc/ntp.conf
collect_file $conf
ntpstep=/etc/ntp/step-tickers
collect_file $ntpstep
drift=/var/lib/ntp/drift
collect_file $drift
log=/var/log/ntp.log
collect_file $log
sconf=/etc/sysconfig/ntpd
collect_file $sconf
sclock=/etc/sysconfig/clock
collect_file $sclock
localt=/etc/localtime
collect_file $localt
adjt=/etc/adjtime
collect_file $adjt
stscr=/etc/init.d/ntpd
collect_file $stscr
echo "Collecting clock settings/status "
#hwclock
hwclock --show > $targetdir/hwclock.txt
#date
date > $targetdir/date.txt
## checking time sources
cat /sys/devices/system/clocksource/clocksource0/available_clocksource > $targetdir/av_clocksource
cat /sys/devices/system/clocksource/clocksource0/current_clocksource > $targetdir/cu_clocksource
##checking timezone file
echo "Dumping and collecting timezone file "
YEAR=`date +"%Y"`
ZN=`cat /etc/sysconfig/clock | grep ZONE | cut -d = -f 2 | tr -d '"'`
zdump -v $ZN | grep $YEAR > $targetdir/timzone-check.txt
#tars the complete /etc/ntp directory
#NOTE: this will collect the key file as well
echo "Collecting /etc/ntp directory files "
tar cvzf $targetdir/etc-ntp-dir.tar.gz /etc/ntp 2>/dev/null
echo "Collecting system info files "
##collect sysinfo
cat /etc/*-release > $targetdir/release.txt
chkconfig --list > $targetdir/chkconfig.txt
top -b -n1 > $targetdir/top.txt
ps aux > $targetdir/psaux.txt
dmesg > $targetdir/dmesg.txt
rpm -qa > $targetdir/rpm.txt
uname -a > $targetdir/uname.txt
cat /proc/cmdline > $targetdir/kernel-cmdl.txt
# collecting syslogs
echo "Collecting messages files, depending on the size it can take a few mintues "
tar cvzf $targetdir/messages.tar.gz /var/log/messages* 2>/dev/null
##ntpq commands to gather status information
echo "Collecting ntp status information "
ntpq -p > $targetdir/ntpq-p.txt
ntpq -c as > $targetdir/ntpq-cas.txt
ntpq -c version > $targetdir/ntpq-cversion.txt
ntpq -c rv > $targetdir/ntpq-crv.txt
##other ntp status information and configurations
if [ -f /usr/sbin/ntptrace ]
then
echo "Collecting ntptrace information, you may get a timeout message here"
ntptrace -n > $targetdir/ntptrace.txt
else
echo "ntptrace not found, please install ntp-perl, 'yum install ntp-perl', and re-run this scrip"
fi
ntpstat > $targetdir/ntpstat.txt
##assID
##getting the ass id to run ntpq -c "rv assid" for every configured server
assID=`ntpq -cas | awk '{ print $2 }'`
count=`echo $assID | awk -F ' ' '{ print NF}'`
i=2
while ! [ $i -gt $count ]
do
ARR[$i]=`echo $assID | awk -F ' ' '{print $'"$i"'}'`
id=${ARR[$i]}
ntpq -c "rv $id" >> $targetdir/ntpq-assID$id.txt
i=$(expr $i + 1)
done
sleep 3
#create archive
echo "Creating tar file"
tar cvzf /tmp/ntpd.tar.gz $targetdir 2>/dev/null
echo "Please upload /tmp/ntpd.tar.gz to your SR"
#delete $targetdir
rm -f $targetdir/*
rmdir $targetdir
Sample Output
The output gzipped file will be genrated as follows:
# ls -la /tmp/ntpd.tar.gz
-rw-r--r-- 1 root root 17229 Feb 7 03:16 /tmp/ntpd.tar.gz
To extract the archive use the following command:
# tar xvzf ntpd.tar.gz
The Files are extracted in the current directory (tmp/ntpd was created):
# cd tmp/ntpd
# ls -la
total 5500
drwxr-xr-x 2 root root 4096 Mar 2 07:52 .
drwxr-xr-x 3 root root 4096 Mar 2 07:49 ..
-rw-r--r-- 1 root root 45 Jan 15 17:06 adjtime
-rw-r--r-- 1 root root 18 Mar 2 07:52 av_clocksource
-rw-r--r-- 1 root root 5037 Mar 2 07:52 chkconfig.txt
-rw-r--r-- 1 root root 4 Mar 2 07:52 cu_clocksource
-rw-r--r-- 1 root root 29 Mar 2 07:52 date.txt
-rw-r--r-- 1 root root 51628 Mar 2 07:52 dmesg.txt
-rw-r--r-- 1 root root 489 Mar 2 07:52 etc-ntp-dir.tar.gz
-rw-r--r-- 1 root root 51 Mar 2 07:52 hwclock.txt
-rwxr-xr-x 1 root root 1923 Jan 25 14:24 init.d-ntpd
-rw-r--r-- 1 root root 249 Mar 2 07:52 kernel-cmdl.txt
-rw-r--r-- 1 ntp ntp 7 Mar 2 07:32 lib-ntp-drift
-rw-r--r-- 1 root root 3661 Oct 16 2013 localtime
-rw-r--r-- 1 root root 5332960 Mar 2 07:52 messages.tar.gz
-rw-r--r-- 1 root root 1881 Oct 7 16:33 ntp.conf
-rw-r--r-- 1 root root 3070 Mar 2 07:52 ntpq-assID.txt
-rw-r--r-- 1 root root 357 Mar 2 07:52 ntpq-cas.txt
-rw-r--r-- 1 root root 508 Mar 2 07:52 ntpq-crv.txt
-rw-r--r-- 1 root root 55 Mar 2 07:52 ntpq-cversion.txt
-rw-r--r-- 1 root root 474 Mar 2 07:52 ntpq-p.txt
-rw-r--r-- 1 root root 117 Mar 2 07:52 ntpstat.txt
-rw-r--r-- 1 root root 72 Mar 21 2014 ntp-step-tickers
-rw-r--r-- 1 root root 64 Mar 2 07:52 ntptrace.txt
-rw-r--r-- 1 root root 23811 Mar 2 07:52 psaux.txt
-rw-r--r-- 1 root root 271 Mar 2 07:52 release.txt
-rw-r--r-- 1 root root 53913 Mar 2 07:52 rpm.txt
-rw-r--r-- 1 root root 196 Dec 12 2013 sysconfig-clock
-rw-r--r-- 1 root root 85 Jan 25 14:24 sysconfig-ntpd
-rw-r--r-- 1 root root 374 Mar 2 07:52 timzone-check.txt
-rw-r--r-- 1 root root 47169 Mar 2 07:52 top.txt
-rw-r--r-- 1 root root 130 Mar 2 07:52 uname.txt