Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jhrcz
Last active December 26, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhrcz/7138803 to your computer and use it in GitHub Desktop.
Save jhrcz/7138803 to your computer and use it in GitHub Desktop.
kvm-clock-hardsync.sh
#!/bin/bash
# /usr/local/sbin/kvm-clock-hardsync.sh
# cron:
# * * * * * /usr/bin/cronic /usr/local/sbin/kvm-clock-hardsync.sh
#
[ "$DEBUG" = "YES" ] \
&& set -x
# required for running in cron
PATH="/sbin:/usr/sbin:$PATH"
export PATH
####
ntpserver=$( grep ^server /etc/ntp.conf | awk '{print $2}' )
if [ -n "$ntpserver" ]
then
ntpserverdiff=$(/usr/lib64/nagios/plugins/check_ntp -H "$ntpserver" -v | grep 'overall average offset:' | cut -d : -f 2 | tr -d ' ' | cut -d . -f 1)
fi
hwclockdiff="$(( $(date +%s) - $(date +%s -d "$(hwclock -r)" ) ))"
#####
echo "hwclock:"
hwclock -r
echo "diff: $hwclockdiff"
echo "system:"
date
echo "diff: $ntpserverdiff"
####
echo
if [ \
-n "$hwclockdiff" \
-a "$hwclockdiff" -lt "-50" \
-a -n "$ntpserverdiff" \
-a "$ntpserverdiff" -gt "50" \
]
then
echo hard-sync
hwclock -s
service ntpd try-restart
else
echo sync-not-required
fi
echo "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment