1. 准備alert腳本,用來調用微信腳本
#!/bin/bash alertname=$1 target=$2 losspattern=$3 rtt=$4 hostname=$5 smokename="hq_to_idc " if [ "$losspattern" = "loss: 0%" ]; then subject="Clear-${smokename}-Alert: $target host: ${hostname}" else subject="${smokename}Alert: ${target} – ${hostname}" fi echo "MTR Report for hostname: ${hostname}" > /tmp/mtr.txt echo "" >> /tmp/mtr.txt echo "sudo mtr -n –report ${hostname} " sudo /usr/sbin/mtr -n –report ${hostname} >> /tmp/mtr.txt echo "" >> /tmp/mtr.txt echo "Name of Alert: " $alertname >> /tmp/mtr.txt echo "Target: " $target >> /tmp/mtr.txt echo "Loss Pattern: " $losspattern >> /tmp/mtr.txt echo "RTT Pattern: " $rtt >> /tmp/mtr.txt echo "Hostname: " $hostname >> /tmp/mtr.txt echo "" >> /tmp/mtr.txt echo "Full mtr command is: sudo /usr/sbin/mtr -n –report ${hostname}" >> /tmp/mtr.txt echo "subject: " $subject if [ -s /tmp/mtr.txt ]; then /usrt/smokeping/weixin.sh "s" "s" "${subject}:${hostname}:$losspattern:$rtt:$target" fi
准備微信報警代碼
#!/bin/bash ### script name weixin.sh ### send messages from weixin for zabbix monitor ### 000 ### 000 ### usage: curl -s -G url 獲取acessToken ### curl --data url 傳送憑證調用企業號接口 ### zabbix 會傳送三個參數給腳本,$1 是消息接收賬號,$2 報警標題, $3 報警內容 CropID='' Secret='' GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F '"' '{print $4}') now_time=$(date -d now) now_timestamp=$(date -d "${now_time}" +%s) options=$[2*60*60] token_file='/tmp/token_file.txt' PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" LOG_FILE='/tmp/weixin_log.txt' function logMessageToFile(){ echo "[ $1 ] - ["$(date "+%Y-%m-%d %H:%M:%S")"] - $2" >> $LOG_FILE } function create_tokenfile(){ echo "${now_time} > ${Gtoken}" > ${token_file} } function get_Gtoken(){ GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F '"' '{print $4}') } function check_token(){ if [ -f "${token_file}" ]; then file_token=$(awk -F '>' '{print $2}' ${token_file}) file_time=$(awk -F '>' '{print $1}' ${token_file}) file_timestamp=$(date -d "${file_time}" +%s) time_difference=$[${now_timestamp}-${file_timestamp}] if [ "${time_difference}" -gt "${options}" ]; then get_Gtoken create_tokenfile logMessageToFile "INFO" "上次token是: ${file_token},到現在過了${time_difference} 秒, 新的token是:${Gtoken}" else logMessageToFile "INFO" "not need update the token, use old token ${file_token} 到現在過了${time_difference} 秒" Gtoken=$(echo ${file_token}) fi else get_Gtoken logMessageToFile "INFO" "the token_file : ${token_file} not exsits ,will create it . 到現在過了${time_difference} 秒" create_tokenfile fi } function body(){ local int AppID=1 # 企業號中的應用ID local UserID="@all" # 部門成員id,zabbix中定義的微信接受者 local PartyID="1" # 部門id,定義了范圍,組內成員都可接收到消息 local Msg=$(echo "$@" | cut -d " " -f3-) # 過濾出zabbix中傳遞的第三個參數 printf '{\n' printf '\t"touser":"'"$UserID"\"",\n" printf '\t"toparty":"'"$PartyID"\"",\n" printf '\t"msgtype": "text",'"\n" printf '\t"agentid":'$AppID\,"\n" printf '\t"text":{\n' printf '\t\t"content":"'"$Msg"\" printf '\n\t},\n' printf '\t"safe":"0"\n' printf '}\n' } check_token #smokeping_alert=${1}-${2}-${3}-${4}-${5] /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
# smokeping 配置文件 config中需要修改的參數 # to 后面默認是 填寫 郵件 # 如果 = 后面 跟上 | 代表執行用戶自己的腳本 # 腳本會讀入5或者6個參數:name-of-alert, target, loss-pattern, rtt-pattern, # hostname,[raise]。 *** Alerts *** #to = |/opt/smokeping/alert.sh to =test@test.com from = smokealert@company.xy # 報警條件 +someloss type = loss # in percent pattern = >0%,*12*,>0%,*12*,>0% comment = loss 3 times in a row
# 意思是 12次檢查中 出現了 3次丟包的情況 監控主機參數增加 alerts = someloss
后續規則可以自己調整
關於smokeping告警規則 官方文檔
http://oss.oetiker.ch/smokeping/doc/smokeping_config.en.html
http://blog.csdn.net/achejq/article/details/51556494
2、特定主機發送到特定用戶
to 表示接受所有報警的郵箱,如果需要在特定的節點報警發送到特定的郵箱
則在該節點上增加alertee = testmonitor@139.com即可。具體可以參看/opt/smokeping/lib/Smokeping.pm源碼,如下部分:
- foreach my $addr (map {$_ ? (split /\s*,\s*/,$_) : ()} $cfg->{Alerts}{to},$tree->{alertee},$alert->{to}){