教你如何用smokeping實現釘釘告警

安全劍客發表於2019-09-14
現在越來越多的人開始使用釘釘,對於運維告警來說,除了傳統的郵件告警之外,簡訊告警、企業微信告警、釘釘機器人告警等也是首選考慮。
第一:釘釘機器人API獲取方式

釘釘開放平臺

第二:配置smokeping呼叫外部 指令碼進行郵件報警
vim /opt/smokeping/etc/config
*** Alerts ***
#to = 88@wsfnk.com
to = |/opt/smokeping/bin/mailx_alert.sh		# 使用|接指令碼路徑,呼叫外部指令碼
from = smokealert@company.xy
#注意:smokeping在告警的時候會傳送5個引數到告警接收媒介(這裡也就是我們自定義的alert指令碼),引數按照順序分別為:name-of-alert, target, loss-pattern, rtt-pattern, hostname,對應下面報警指令碼的$1-$5
第三:編寫報警指令碼,並附報警效果圖

如下是報警效果
教你如何用smokeping實現釘釘告警教你如何用smokeping實現釘釘告警
cat /opt/smokeping/bin/mailx_alert.sh

#!/bin/bash
#########################################################
# Script to email a ping report on alert from Smokeping #
#########################################################
# 解析變數
Alertname=$1
Target=$2
Losspattern=$3
Rtt=$4
Hostname=$5
Date=$( date '+%Y-%m-%d %H:%M:%S' )
#這是smokeping報警會創出的$1-$5值的型別
#A1=hightloss
#B2="HK.HK-9S.9s-3 [from template]"
#C3="loss: 0%, 0%, 0%, 0%, 15%, 45%, 40%"
#D4="rtt: 29ms, 29ms, 29ms, 29ms, 33ms, 33ms, 33ms"
#E5="1.1.1.1"
# 注意:為釘釘機器人告警,重新處理變數,因為釘釘機器人告警裡(變數的內容不能包含空格)
#A=`echo $1 | sed 's/ //g'`
	case "$Alertname" in
	hightloss)
		A=hightloss-高丟包;;
	hostdown)
		A=hostdown-DOWN;;
	rtt-1)
		A=rtt-1-高延時;;
	esac
#處理變數B
B=`echo $2 | sed 's/ //g'`
B=`echo $B | sed 's/\[fromSH_youfu_CT_Proxy\]//g'`
B=`echo $B | sed 's/\[fromtemplate\]//g'`
#  這裡為了報警的內容的美觀,順帶將rtt:和loss:抹去了
C=`echo $3 | sed 's/ //g'`
C=`echo $C | sed 's/loss://g'`
D=`echo $4 | sed 's/ //g'`
D=`echo $D | sed 's/rtt://g'`
E=`echo $5 | sed 's/ //g'`
F=`echo $Date |sed 's/ /_/g'`
G=未知類別
#定義"目標分類的函式"
Mubiao() {
		H=/opt/smokeping/etc/targets
                P=`cat -n "$H"-"$X" |grep "host = $E$" |  cut -f 1 `
                PP=`expr $P - 2`
                G=`sed -n "s/menu = //g;$PP p" $H-$X `
}
#設定報警值的A端Z端,這裡預設設定為	"AEND=深圳阿里雲,ZEND=DNS"
#根據報警的target判斷那個機房出現報警
        if [ `echo "$Target" |grep SGP | wc -l`  -ne 0 ];then
                ZEND='新加坡'
		X=SGP
		Mubiao
          elif [ `echo "$Target" |grep PHI | wc -l`  -ne 0 ];then
                ZEND='菲律賓'
		X=PHI
		Mubiao
          elif [ `echo "$Target" |grep HK | wc -l`  -ne 0 ];then
                ZEND='香港'
		X=HK
		Mubiao
          elif [ `echo "$Target" |grep LA | wc -l`  -ne 0 ];then
                ZEND='洛杉磯'
		X=LA
		Mubiao
          elif [ `echo "$Target" |grep JP | wc -l`  -ne 0 ];then
                ZEND='日本'
		X=JP
		Mubiao
          elif [ `echo "$Target" |grep KOR | wc -l`  -ne 0 ];then
                ZEND='韓國'
		X=KOR
		Mubiao
          elif [ `echo "$Target" |grep CD-PBS | wc -l`  -ne 0 ];then
                ZEND='成都鵬博士'
		X=TG
		Mubiao
          elif [ `echo "$Target" |grep BJ-SJQ | wc -l`  -ne 0 ];then
                ZEND='北京四季青'
		X=TG
		Mubiao
          elif [ `echo "$Target" |grep SH-YF | wc -l`  -ne 0 ];then
                ZEND='有孚雙線'
		X=TG
		Mubiao
          elif [ `echo "$Target" |grep SZ-YX | wc -l`  -ne 0 ];then
                ZEND='深圳易信'
		X=TG
		Mubiao
          elif [ `echo "$Target" |grep SZ-PBS | wc -l`  -ne 0 ];then
                ZEND='深圳鵬博士'
		X=TG
		Mubiao
          elif [ `echo "$Target" |grep TG-YN | wc -l`  -ne 0 ];then
                ZEND='越南'
		X=TG
		Mubiao
          else
	     [ `echo "$Target" |grep DNS | wc -l`  -ne 0 ]
                ZEND='DNS'
		X=DNS
		Mubiao
        fi
#根據報警的target判斷是否報警源位置(smokeping_salve的主機名)
        if [ `echo "$Target" |grep SH_youfu | wc -l`  -ne 0 ];then
                AEND='有孚雙線'
          elif [ `echo "$Target" |grep template | wc -l`  -ne 0 ];then
                AEND='陝西聯通'
	  else
	       AEND='深圳阿里雲'
#	       mtr -r -n $Hostname >> /tmp/mtr.txt
#	       echo '++++++++++++' >> /tmp/mtr.txt
        fi
#釘釘二號機器人API
#https://oapi.dingtalk.com/robot/send?access_token=6c1712241129c425e5385d7d164ffe1ed4d4663b0651c4e
#釘釘三號機器人API
#https://oapi.dingtalk.com/robot/send?access_token=98ce2cf2925eb0b960745b449ee63a9917571a650f6e7ca4b323f73c7
####################釘釘機器人告警執行部分#######################
# 注意:為釘釘機器人告警,重新處理變數,因為釘釘機器人告警裡(變數的內容不能包含空格)
curl 'https://oapi.dingtalk.com/robot/send?access_token=6c1712241129c425e5385d7d164ffe1ed4d4663b065b50bf' \
   -H 'Content-Type: application/json' \
   -d '
{
    "msgtype": "text",
    "text": {
"content":"('$AEND'-'$ZEND')網路告警
告警策略:'$A'
目標類別:'$G'
目標名稱:'$B'
丟包率:'$C'
延 遲:'$D'
目標地址:'$E'
故障時間:'$F'"
    },
     "at": {
         "atMobiles": [
             "182****8240"
         ], 
         "isAtAll": false
     }
}'
#>> /tmp/dingding-alert.log  2>&1
#####################郵件告警呼叫執行部分,這裡暫不使用,只記錄到日誌檔案#######################
#zhuti="打碼 ($AEND-$ZEND) 網路質量告警"
#messages=`echo -e " 報警策略名: \t $Alertname \n 報警目標: \t $Target \n 丟包率: \t $Losspattern  \n 延遲時間: \t $Rtt \n 主機地址: \t$Hostname \n 報警時間: \t$Date "`
#email="zhsd@www.com"
echo "$Date -- $Alertname -- $Target -- $Losspattern -- $Rtt -- $Hostname" >> /tmp/smokeping-baojin
#echo "$messages" | mail -s "$zhuti" $email >>/tmp/mailx.log 2>&1

原文地址: https://www.linuxprobe.com/linux-smokeping.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31559985/viewspace-2656996/,如需轉載,請註明出處,否則將追究法律責任。

相關文章