Last active
December 26, 2015 10:39
-
-
Save jhrcz/7138803 to your computer and use it in GitHub Desktop.
kvm-clock-hardsync.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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