监控linux各主机系统时间是否一致

Posted FlyBack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监控linux各主机系统时间是否一致相关的知识,希望对你有一定的参考价值。

#!/bin/bash
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
PASSWD=‘**************‘
print_help() {
echo "Usage:"
    echo "  -H)"
    echo "    主机地址"
echo "[-w] Warning"
echo "[-c] Critical"
exit  $STATE_OK
}
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit  $STATE_OK
;;
 --url|-H)
    HOSTADDRESS=$2
    shift
    ;;
-w)
warn_level=$2
shift
;;
-c)
critical_level=$2
shift
;;
*)
echo "Unknown Argument: $1"
print_help
exit  $STATE_UNKNOWN
;;
esac
shift
done
if [ "$warn_level" == "" ]; then
echo "No Warning Level Specified"
print_help
exit  $STATE_UNKNOWN;
fi
if [ "$critical_level" == "" ]; then
echo "No Critical Level Specified"
print_help
exit  $STATE_UNKNOWN;
fi
if [ "$HOSTADDRESS" == "" ]; then
echo "No HOSTADDRESS Specified"
print_help
exit  $STATE_UNKNOWN;
fi
echo "" > /usr/local/nagios/libexec/check_time_data/host_time_$HOSTADDRESS
/usr/local/bin/expect <<EOF
log_file /usr/local/nagios/libexec/check_time_data/host_time_$HOSTADDRESS
log_user 1
spawn  ssh -t -p 22 [email protected]$HOSTADDRESS "date +%s"
expect {
        "(yes/no)?"
                    {

                        send "yes\r"
                        expect  "*assword" { send "$PASSWD\r"}
                   
                    }
        "*assword"  {
                       
                        send "$PASSWD\r"
                    }

}
expect "100%"
EOF
###处理数据格式
sed -i ‘s/\r//g‘ /usr/local/nagios/libexec/check_time_data/host_time_$HOSTADDRESS

###处理时间


host_time=`cat /usr/local/nagios/libexec/check_time_data/host_time_$HOSTADDRESS|grep -E ‘^[0-9]‘`


cur_time=`date +%s`

value=`expr $cur_time - $host_time`

if [ $value -lt 0 ]; then
  let value=0-$value;
fi


if [[ "$value" -le "$warn_level" ]]; then
echo "$HOSTADDRESS time is OK. $HOSTADDRESS_time-cur_time:$value|value=$value;$warn_level;$critical_level;0"
exit  $STATE_OK;
elif [[ "$value" -gt "$warn_level" ]] && [[ "$value" -lt "$critical_level" ]]; then
echo "$HOSTADDRESS time is WARNING. $HOSTADDRESS_time-cur_time:$value|value=$value;$warn_level;$critical_level;0"
exit  $STATE_WARNING;
elif [[ "$value" -ge "$critical_level" ]]; then
echo "$HOSTADDRESS time is CRITICAL. $HOSTADDRESS_time-cur_time:$value|value=$value;$warn_level;$critical_level;0"
exit  $STATE_CRITICAL;
fi
























































































以上是关于监控linux各主机系统时间是否一致的主要内容,如果未能解决你的问题,请参考以下文章

Linux网络流量监控与分析工具Ntop和Ntopng

Linux—查看远程Linux系统运行时间

nagios监控系统

Nagios监控系统

Nagios监控系统

Nagios监控系统