IDC网络质量分析工具之(六)smokeping如何实现钉钉告警

第一:钉钉机器人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

第三:编写报警脚本,并附报警效果图

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://boke.wsfnk.com/archives/224.html
谢谢你请我吃辣条谢谢你请我吃辣条

如果文章对你有帮助,欢迎点击上方按钮打赏作者

最后编辑于:2022/12/11作者: 辣条①号

现在在做什么? 接下来打算做什么? 你的目标什么? 期限还有多少? 进度如何? 不负遇见,不谈亏欠!

暂无评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

arrow grin ! ? cool roll eek evil razz mrgreen smile oops lol mad twisted wink idea cry shock neutral sad ???

文章目录