- #!/bin/bash
- PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- BASE_DIR="/opt/monitor"
- SYSCHECK_DIR="$BASE_DIR"/syscheck
- SERCHECK_DIR="$BASE_DIR"/sercheck
- CONTACT_DIR="$BASE_DIR"/contact
- SEND_MAIL=/bin/mail
- HOST_NAME=$(/bin/hostname)
- IP_ADD=$(/sbin/ifconfig |awk -F: '{print $2}'|grep Bcast|awk '{printf "%s:",$1}')
- cd "$BASE_DIR"
-
- #取出联系人邮件或手机号
- GET_ALERT_DST()
- {
- #group=$1 #The alert group
- Type=$1 #Only "mail" and "sms"
- DST=$(awk -v Type="$Type" '$1==Type' "$CONTACT_DIR"/sysop.list|awk '{$1=""}{print $0}') #取出邮件或者电话的列表
- echo "$DST"
- }
-
- #发送邮件函数
- SEND_MAIL()
- {
- TIME=$(/bin/date "+%F %R:%S")
- #"$SEND_MAIL" -s $HOST_NAME $1 -u "ALERT!!!:"$HOST_NAME"@"$TIME""
- echo -ne "$2" | $SEND_MAIL -s "$HOST_NAME@$TIME" $1
- }
-
- #发送短信函数
- SEND_SMS()
- {
- phone="$1"
- message=$(echo "$2" |/usr/bin/python "$BASE_DIR"/encode.py)
- curl "$phone"\&sms_text="$message"
- }
-
- #服务状态的检测以及发送报警
- MONITOR()
- {
- CHECK_SCRIPT_DIR=$1
- cd $CHECK_SCRIPT_DIR
- for script in $(ls *.sh)
- do
- CHECK_RESULT=$(source "$script")
- ALERTGROUP=$(grep '^ALERTGROUP' "$script" |cut -d= -f2)
- ALERT_MAIL=$(echo "$CHECK_RESULT" |awk '{if ($3!=0){$3="";print $0}}') #如果有状态不等与0的就不为空
- ALERT_SMS=$(echo "$CHECK_RESULT" |awk '{if ($3>1){$3="";print $0}}') #如果有状态大于1的就不为空
- SERVICES_STATUS=$(echo "$CHECK_RESULT" |awk -F "\t" '{print $3}') #取出服务状态值
- SERVICES_NAME=$(echo "$CHECK_RESULT" |awk -F "\t" '{print $2}').log #取service名称
- #检测服务器状态是否恢复正常,然后进行恢复正常的报警
- echo "$script" > /opt/txt.txt
- cd $SYSCHECK_DIR
- if [ -e "$SERVICES_NAME" ];then
- LAST_STATUS=$(cat $SYSCHECK_DIR/$SERVICES_NAME)
- if [ $LAST_STATUS -ge 1 -a $SERVICES_STATUS -eq 0 ];then
- for maildst in $(GET_ALERT_DST mail)
- do
- SEND_MAIL "$maildst" "$HOST_NAME : $IP_ADD\n$ALERT_MAIL ststus is ok"
- done
- fi
-
- if [ $LAST_STATUS -ge 2 -a $SERVICES_STATUS -eq 0 ];then
- for smsdst in $(GET_ALERT_DST sms)
- do
- SEND_SMS "$smsdst" "$HOST_NAME : $IP_ADD $ALERT_SMS"
- done
- fi
- echo $SERVICES_STATUS > $SYSCHECK_DIR/$SERVICES_NAME
- else
- echo $SERVICES_STATUS > $SYSCHECK_DIR/$SERVICES_NAME
- fi
-
- #检测服务器状态是否正常,然后进行报警
- cd $CHECK_SCRIPT_DIR
- if [ -n "$ALERT_MAIL" ]
- then
- for maildst in $(GET_ALERT_DST mail)
- do
- SEND_MAIL "$maildst" "$HOST_NAME : $IP_ADD\n$ALERT_MAIL"
- done
- fi
-
- if [ -n "$ALERT_SMS" ]
- then
- for smsdst in $(GET_ALERT_DST sms)
- do
- SEND_SMS "$smsdst" "$HOST_NAME : $IP_ADD $ALERT_SMS"
- done
- fi
- done
- }
-
- MONITOR "$SYSCHECK_DIR"
此脚本出自 http://blog.chinaunix.net/uid-25837154-id-3043851.html
经本人添加功能,故障恢复后的报警,其中有一些修改,如果侵犯作者权益,请与我联系,我将立即删除
阅读(1980) | 评论(0) | 转发(0) |