zoukankan      html  css  js  c++  java
  • smokeping 微信报警配置

    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 '{
    '
            printf '	"touser":"'"$UserID""",
    "
            printf '	"toparty":"'"$PartyID""",
    "
            printf '	"msgtype": "text",'"
    "
            printf '	"agentid":'$AppID\,"
    "
            printf '	"text":{
    '
            printf '		"content":"'"$Msg""
            printf '
    	},
    '
            printf '	"safe":"0"
    '
            printf '}
    '
    }
    
    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源码,如下部分:

      1. foreach my $addr (map {$_ ? (split /s*,s*/,$_) : ()} $cfg->{Alerts}{to},$tree->{alertee},$alert->{to}){
    在尝试学习新的语言之前先理解这门语言的设计原理能够让你在探索这门新语言时保持一个清醒而且开发的状态。
  • 相关阅读:
    关于oralce字符集问题(复制别人的,纯属自己学习)
    Linux下oracle11gR2系统安装到数据库建立配置及最后oracle的dmp文件导入一站式操作记录
    Linux下部署ASP.NET服务连接oracle遇到的问题记录
    前端金钱分转元,元转分精度问题解决
    vue-element的form表单显示图片
    vue页面刷新技巧--(v-if指令)以及vue动态设置css属性
    vue后端获取的数据无法进行双向数据绑定
    Vue实现勾选框全选和局部选择功能
    VUE/jQuery生成二维码扫描跳转地址
    uni-app之数据状态改动后页面不刷新踩坑
  • 原文地址:https://www.cnblogs.com/jackchen001/p/6953024.html
Copyright © 2011-2022 走看看