码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2017-07-10 14:28:01      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:local   sage   unknown   help   ssh   监控linux   data   int   case   

#!/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 supope@$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各主机系统时间是否一致

标签:local   sage   unknown   help   ssh   监控linux   data   int   case   

原文地址:http://www.cnblogs.com/flyback/p/7145345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!