+-
linux – 检查ntp状态和系统时间同步的脚本
寻找一个脚本(1)检查ntp的状态,以及(2)是否在给定时间服务器的/ – 1秒内(例如123.456.789.10). (3)此外,应检查系统时间以查看时区是否设置正确(例如PST)

这是我到目前为止,检查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
最佳答案
我使用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是否已关闭.例如,在red hat,centos等上使用service ntpd status,然后在$?上查看结果状态.变量.

点击查看更多相关文章

转载注明原文:linux – 检查ntp状态和系统时间同步的脚本 - 乐贴网