Nagios监控-自定义开发报警插件:钉钉机器人群消息

  • shell发送钉钉机器人群消息
    参考博文:https://blog.csdn.net/cen50958/article/details/92230109

  • 编写shell脚本
    脚本文件下载地址:https://download.csdn.net/download/cen50958/11243237

    #!/bin/sh
    #############################################################################
    #nagios notify by dingtalk use rebot send group message  
    # Written by: silly ([email protected])
    # 
    # Last Modified: 16-06-2019
    #
    # NOTES:
    #This is a shell script file that sends group messages through a nailing robot
    #Different notification requirements can be modified according to individual needs.
    #############################################################################
    #define receive parameter values
    notifyToken=$1
    notifyActionType=$2
    notifyHostAlias=$3
    notifyServiceDesc=$4
    notifyServiceState=$5
    notifyType=$6
    notifyService=$7
    notifyHost=$8
    notifyAddress=$9
    notifyState=${10}
    notifyInfo=${11}
    
    #define dingtalk config
    dingtalkUrl="https://oapi.dingtalk.com/robot/send"
    
    #define notify msg template
    msgHead="\\n${notifyActionType} Service Alert: ${notifyHostAlias}/${notifyServiceDesc} is ${notifyServiceState}"
    msgTopic="'***** Nagios *****'"
    msgType="Notification Type: ${notifyType}"
    msgService="Service: ${notifyService}"
    msgHost="Host: ${notifyHost}"
    msgAddress="Address: ${notifyAddress}"
    msgState="State: ${notifyState}"
    msgDate="Date/Time: `date '+%Y-%m-%d %H:%M:%S'`"
    msgInfo="Additional Info:\\n\\n${notifyInfo}"
    msgTemplate=${msgHead}\\n\\n${msgTopic}\\n\\n${msgType}\\n\\n${msgService}\\n${msgHost}\\n${msgAddress}\\n${msgState}\\n\\n${msgDate}\\n\\n${msgInfo}
    #define dingtalk send msg template
    dingtalkMsgTemplate='{"msgtype": "text","text": {"content": ""}}'
    
    #dingtalk rebot send group message
    function dingtalkRebotSendMsg(){
    	notifyMsg=$1
    	sendMsg=${dingtalkMsgTemplate//$notifyMsg}   
    	sendUrl="${dingtalkUrl}?access_token=${notifyToken}"
    	curl $sendUrl -H 'Content-Type: application/json' -d "$sendMsg"
    }
    
    dingtalkRebotSendMsg "$msgTemplate"
    
  • 上传脚本(notify_dingtalk_rebot )并赋值权限
    脚本上传到:/usr/local/nagios/libexec/目录中
    格式转换

    dos2unix notify_dingtalk_rebot 
    

    赋权

    chmod 755 notify_dingtalk_rebot
    
  • 添加钉钉机器人token及组contacts.cfg

    define contact{
        contact_name                    dingtalk1
        use                             generic-contact
        alias                           Nagios Admin
        pager                           xxxxx    #添加钉钉机器人时的access_token
        }
    
    define contactgroup{
        contactgroup_name       dingtalk
        alias                   Nagios Administrators
        members                 dingtalk1    #成员可添加多个,表示发送到多个群里
        }
    
  • 添加报警的命令commands.cfg
    notify-service-by-dingtalk-rebot

    # 'notify-service-by-dingtalk-rebot' command definition
    define command{
        command_name    notify-service-by-dingtalk-rebot
        command_line    $USER1$/notify_dingtalk_rebot "$CONTACTPAGER$" "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$HOSTADDRESS$" "$SERVICEDESC$" "$SERVICESTATE$" "$SERVICEOUTPUT$"
    

    notify-host-by-dingtalk-rebot

    # 'notify-host-by-dingtalk-rebot' command definition
    define command{
        command_name    notify-host-by-dingtalk-rebot
        command_line     $USER1$/notify_dingtalk_rebot "$CONTACTPAGER$" "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$HOSTADDRESS$" "down" "down" "down"
        }
    
  • 调整联系人模板,添加报警的命令(来自于commands.cfg)
    编辑templates.cfg里的定义内容为:

    define contact{
        name                            generic-contact         
        service_notification_period     24x7                    
        host_notification_period        24x7                    
        service_notification_options    w,u,c,r,f,s             
        host_notification_options       d,u,r,f,s               
        service_notification_commands   notify-service-by-email,notify-service-by-dingtalk-rebot
        host_notification_commands      notify-host-by-email,notify-host-by-dingtalk-rebot
        register                        0                       
        }
    
  • hosts.cfg,services.cfg添加报警联系人及组,或者对应模板加

    contact_groups  dingtalk
    
  • 检查语法并重启

    /etc/init.d/nagios checkconfig
    /etc/init.d/nagios reload
    
  • 效果展示
    异常告警
    Nagios监控-自定义开发报警插件:钉钉机器人群消息_第1张图片
    恢复通知
    Nagios监控-自定义开发报警插件:钉钉机器人群消息_第2张图片

开发插件遇到的坑
  • 编写脚本打印输出:***** Nagios ***** 这行信息信息时,由于*是shell中的关键字,造成脚本无法按照正常预期参数输入与接收
  • 第一版本接收输入参数超过了10个值,需要接收第10个参数值,最开始采用$10接收,运行时接收参数不正常,输入值为$0的值+0,需要采用${10}来接收
  • $msgTemplate字符串中存在空格问题,在使用函数dingtalkRebotSendMsg传参时,需要使用 “$msgTemplate” 这样传入参数,如果采用$msgTemplate传参,则shell会自动根据空格分成多个参数传入,同理,在定义命令事,为防止出现空格造成程序运行不正常,则参数都采用了双引号(command_line $USER1$/notify_dingtalk_rebot “$CONTACTPAGER$” “$NOTIFICATIONTYPE$” “$HOSTALIAS$” “$HOSTADDRESS$” “$SERVICEDESC$” “$SERVICESTATE$” “$SERVICEOUTPUT$”)
  • 由于脚本文件编写时是在windows环境下编写完成后,放到linux中运行,最初采用直接上传完成赋权后就测试,经过查看nagios日志发现告警正常运行,但是未能够收到告警消息,经过排除需要采用 dos2unix notify_dingtalk_rebot 进行文本格式转换,因为windows中有些隐含字符串会影响shell脚本的正常运行

你可能感兴趣的:(架构)