首页 > 编程知识 正文

钉钉机器人定时发送消息,钉钉机器人自动定时通知任务

时间:2023-05-05 23:30:45 阅读:250074 作者:3412

一、钉钉机器人配置

1、设置钉钉机器人报警
详见阿里云官方文档  https://help.aliyun.com/document_detail/106247.html?spm=5176.2020520101.0.0.57824df5GNP6Jn

重点:获取生成的webhook机器人地址
https://oapi.dingtalk.com/robot/send?access_token=8b21a85c6d22d25dca5bbee5a207bc31e93022d4f68ac1d8b45ddbc9xxxxxxxx

2、创建钉订告警发送 python脚本 nagios-dingding-no@.py

#!/usr/含蓄的康乃馨/python# -*- coding: utf-8 -*-import requestsimport jsonimport sysimport os headers = {'Content-Type': 'application/json;charset=utf-8'}api_url = "https://oapi.dingtalk.com/robot/send?access_token=8b21a85c6d22d25dca5bbee5a207bc31e93022d4f68ac1d8b45ddbc9xxxxxxxx" def msg(text):    json_text= {     "msgtype": "text",#        "at": {#            "atMobiles": [#                "13812345678"#            ],#            "isAtAll": False#        },        "text": {            "content": text        }    }    print requests.post(api_url,json.dumps(json_text),headers=headers).content     if __name__ == '__main__':    text = sys.argv[1]    msg(text)

####注释部分  at 为@指定手机号码的人,不想@任何人可以取消at代码段

测试脚本
# python nagios-dingding-no@.py 'nagios监控测试 2020.04.01'

返回 {"errcode":0,"errmsg":"ok"},说明发送成功

钉钉群会收到消息:

 

将nagios-dingding-no@.py脚本放至 /usr/local/nagios/libexec/目录

至此,告警发送脚本就设置完成了,接下来就来配置nagios,通过调用告警脚本实现nagios的告警信息实时地通过钉订发送出来。

二、nagios相关设置


在/usr/local/nagios/etc/objects目录,编辑命令配置文件commands.cfg (使用之前的py脚本发送告警信息)
   增加如下内容

###########################  notify by dingding #################################################### 'notify-host-by-DD' command definitiondefine command{    command_name    notify-host-by-DD    command_line    /usr/含蓄的康乃馨/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/Time: $LONGDATETIME$n" | /usr/local/nagios/libexec/nagios-dingding-no@.py '[$NOTIFICATIONTYPE$] Host Alert: $HOSTNAME$ is $HOSTSTATE$ 【ECS-nagios监控】'    }# 'notify-service-by-DD' command definitiondefine command{    command_name    notify-service-by-DD    command_line    /usr/含蓄的康乃馨/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SERVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTPUT$n" | /usr/local/nagios/libexec/nagios-dingding-no@.py '[$NOTIFICATIONTYPE$] Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ 【ECS-nagios监控】'    }####################################################################################################

修改联系人配置文件 contacts.cfg,将默认告警方式改为钉钉

define contact{        contact_name                    nagiosadmin                  ; Short name of user        use                             generic-contact              ; Inherit default values from generic-contact template (defined above)        service_notification_period     24x7                         ;                host_notification_period        24x7                         ;        alias                           Nagios Admin                 ; Full name of user        email                           nagios@localhost             ;         service_notification_commands   notify-service-by-DD ;        host_notification_commands      notify-host-by-DD        ;         }

修改完成后,用以下命令检查配置文件是否有误:

[root@yunwei_server ~]# /usr/local/nagios/含蓄的康乃馨/nagios  -v  /usr/local/nagios/etc/nagios.cfg

Nagios Core 4.2.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 09-06-2016
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
        Checked 19 services.
        Checked 43 hosts.
        Checked 2 host groups.
        Checked 0 service groups.
        Checked 1 contacts.
        Checked 1 contact groups.
        Checked 33 commands.
        Checked 5 time periods.
        Checked 0 host escalations.
        Checked 0 service escalations.
Checking for circular paths...
        Checked 43 hosts
        Checked 0 service dependencies
        Checked 0 host dependencies
        Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

------------------------------------------------------------------------------------------------------------

最后结果为0,则说明配置文件正常

重启nagios服务       #service nagios restart

 

三、shell监控脚本调用

【判断网站状态,出现502等状态即告警】
[root@yunwei_server ~]# cat /home/shell/Web_Status_Check.sh 
 

#!/含蓄的康乃馨/bash############## web status check #############webcheck_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} www.webcheck.com`sleep 0.2time=$(date "+%Y-%m-%d %H:%M:%S")echo "www.webcheck.com URL status   is ${webcheck_code},   on ${time} " >>/tmp/web_status.logecho "-------------------------------------------------------- " >>/tmp/web_status.logif [ ${webcheck_code} -ne 200 ] && [ ${webcheck_code} -ne 000 ]  then       /usr/local/nagios/libexec/nagios-dingding-no@.py "[webcheck.com网站告警]  www.webcheck.com HTTP_status is ${webcheck_code} at ${time}, 请及时查看!"fi

【判断mysql同步状态】
[root@yunwei_server ~]# cat /home/shell/mysql-slavestatuscheck.sh 

#!/含蓄的康乃馨/bashtime=$(date "+%Y-%m-%d %H:%M:%S")mysql_cmd="mysql --defaults-extra-file=/etc/my.cnf"                     ###远程连接mysql的账号密码写在配置文件array=($($mysql_cmd -e "show slave statusG"|egrep '_Running:|Behind_Master|Last_SQL_Errno'|awk '{ print $NF }'))if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ]thenecho "【Mysql主从】 Mysql Slave is OK, ${time}" >>/home/shell/logs/mysql_master_slave_check.logelseecho "【Mysql主从】 Mysql Slave is error, ${time}, please check!" >>/home/shell/logs/mysql_master_slave_check.log/usr/local/nagios/libexec/nagios-dingding-no@.py "【Mysql主从】 Mysql Slave is error, ${time}, please check!" >>/dev/nullbreakfi

给上述脚本设置定时任务
*/15 * * * * /home/shell/Web_Status_Check.sh          #网站状态检查每15分钟一次
30 */2 * * * /home/shell/mysql-slavestatuscheck.sh    #mysql主从同步检查,每两个小时

 

 

2020.04.01  愚人节   深圳南山.南海大道

 

 

 

 

 

Vue新一代状态管理工具Pinia的具体使用Python matplotlib怎么绘制不同类型的表格vue3.x使用swiper实现卡片自动轮播效果历史中提交的图片或压缩文件tiktok直播怎么做

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。