
尋找一個腳本來(1)檢查 ntp 的狀態和(2)如果它在給定時間伺服器的 +/- 1 秒內(例如123.456.789.10)。(3)此外,還應檢查系統時間,看看時區設定是否正確(例如太平洋標準時間)
這是我到目前為止檢查 ntp 狀態的內容:
#!/bin/bash
if [[ ! -x /usr/bin/ntpstat ]]
then
echo "ntpstat is NOT installed: please install it"
exit 3
res=$(/usr/bin/ntpstat)
rc=$?
case $rc in
0 )
echo "clocks are synchronized"
;;
1 )
echo "WARNING: clocks are NOT synchronized"
;;
2 )
echo "CRITICAL: NTPD IS DOWN -- NO STATUS"
;;
esac
答案1
我用ntpq
這個。
這是一些片段和偽代碼。
首先,計算偏移量並將其儲存在 var 中:
ntp_offset=$(ntpq -pn | \
/usr/bin/awk 'BEGIN { offset=1000 } $1 ~ /\*/ { offset=$9 } END { print offset }')
當 ntp_offset < 1000 時伺服器正常
當 ntp_offset >= 1000 時伺服器不同步
檢查 ntpd 是否關閉可以使用不同的方法來完成,這取決於您的作業系統。例如,
service ntpd status
在 red hat、centos 等上使用,然後檢查$?
變數的結果狀態。