首页 > 代码库 > 监控linux各主机系统时间是否一致

监控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 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=http://www.mamicode.com/`expr $cur_time - $host_time`

if [ $value -lt 0 ]; then
  let value=http://www.mamicode.com/0-$value;
fi


if [[ "$value" -le "$warn_level" ]]; then
echo "$HOSTADDRESS time is OK. $HOSTADDRESS_time-cur_time:$value|value=http://www.mamicode.com/$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=http://www.mamicode.com/$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=http://www.mamicode.com/$value;$warn_level;$critical_level;0"
exit  $STATE_CRITICAL;
fi

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