一、钉钉机器人配置

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/bin/python
# -*- coding: utf-8 -*-import requests
import json
import sys
import osheaders = {'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).contentif __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 definition
define command{command_name    notify-host-by-DDcommand_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/nagios/libexec/nagios-dingding-no@.py '[$NOTIFICATIONTYPE$] Host Alert: $HOSTNAME$ is $HOSTSTATE$ 【ECS-nagios监控】'}# 'notify-service-by-DD' command definition
define command{command_name    notify-service-by-DDcommand_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$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 useruse                             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 useremail                           nagios@localhost             ; service_notification_commands   notify-service-by-DD         ;host_notification_commands      notify-host-by-DD            ; }

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

[root@yunwei_server ~]# /usr/local/nagios/bin/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

#!/bin/bash
############## web status check #############
webcheck_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} www.webcheck.com`
sleep 0.2
time=$(date "+%Y-%m-%d %H:%M:%S")
echo "www.webcheck.com URL status   is ${webcheck_code},   on ${time} " >>/tmp/web_status.log
echo "-------------------------------------------------------- " >>/tmp/web_status.log
if [ ${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

#!/bin/bashtime=$(date "+%Y-%m-%d %H:%M:%S")
mysql_cmd="mysql --defaults-extra-file=/etc/my.cnf"                     ###远程连接mysql的账号密码写在配置文件array=($($mysql_cmd -e "show slave status\G"|egrep '_Running:|Behind_Master|Last_SQL_Errno'|awk '{ print $NF }'))
if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ]
then
echo "【Mysql主从】 Mysql Slave is OK, ${time}" >>/home/shell/logs/mysql_master_slave_check.log
else
echo "【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/null
break
fi

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

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

使用钉钉机器人实现运维告警信息自动发送(Python、Nagios、Shell)相关推荐

  1. 运维服务器告警规则阈值,运维告警管理困难重重,我是怎么做到的

    随着IT基础设施的云化,应用运行环境的容器化,系统架构的微服务化,越来越多的企业不得不引入更多的工具.更复杂的流程和更多的运维人员,来提升IT系统管理的精细度,但新的问题也随之而来. 在如此庞杂的环境 ...

  2. python和linux运维学哪个--学习Python在Linux运维上的应用应该看哪些书 什么样的学习路线...

    python 元类生成类的方法是什么类型的 首先请一下习惯,python中的类名习惯用大写字母开 我是这样理解的,不一定对,通过type() ,还是实例方法instancemethod 只不过B这个类 ...

  3. 运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(二)

    现在开始介绍php+mysql+shell监控系统 1.目的 此监控系统主要是通过php+mysql+shell的方式,通过shell脚本对各个机器的其各个服务进行监控,达到及时的了解其各个应用服务的 ...

  4. 给运维工程师的Cheatsheets! 《Shell脚本速查手册》免费下!

    简介:Shell 作为 Linux 中的第一语言,几乎每一个使用 Linux 的人都用到或用过 Shell,但绝大多数人都并不能掌握 Shell 编程的基本能力和技巧. 2021 年,阿里云开发者学堂 ...

  5. python paramiko模块下载_Python自动化运维实战:使用Python管理网络设备

    现在,我们已经知道如何在不同的操作系统中使用和安装Python以及如何使用EVE-NG搭建网络拓扑.在本章中,我们将学习如何使用目前常用的网络自动化库自动完成各种网络任务.Python可以在不同的网络 ...

  6. 运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(一)

    前言 记得刚来这家公司的时候,我部门就我一个运维工程师,然后就是经理,刚开始公司平台什么监控都没有,在我与经理的努力下,先搭建nagios+cacti监控平台,后来随着公司业务的增加,平台的功能与服务 ...

  7. 运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(五)

    4.监控视图 主要是通过shell脚本进行收集mysql的数据,然后同gnuplot软件进行数据视图化,然后php加载这些图片到web里显示,用到的php文件为view.php day.php.mon ...

  8. 运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(四)

    (5)数据库服务器(以甘肃数据库为例,脚本解释参照甘肃web) #!/bin/bash #ip db_ip=$(/sbin/ifconfig eth0|grep "inet addr&quo ...

  9. 运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统

    转载来自:http://dl528888.blog.51cto.com/2382721/1034992 前言 记得刚来这家公司的时候,我部门就我一个运维工程师,然后就是经理,刚开始公司平台什么监控都没 ...

  10. python运维开发做什么_想做运维开发工程师,Python Django学的很迷茫,怎么办?...

    不只一个想要转型成为运维开发工程师的小伙伴问过我,Python语法学起来那么简单,为什么一个Django框架就那么难? 学Python的小伙伴往往会有这样的困惑: 我看了100多个小时的教学视频,为什 ...

最新文章

  1. 浏览器内核Trident/Gecko/WebKit/Presto
  2. Java集群优化——dubbo+zookeeper构建高可用分布式集群
  3. 【数理知识】《积分变换与场论》王振老师-第2章-拉普拉斯变换
  4. Window核心编程
  5. GDI对象和CDC的使用方法(防止资源泄漏)
  6. 虚拟化精华问答 | 怎样使虚拟机能够达到比较高的IO性能?
  7. Linux上Java的安装与配置,java初级面试笔试题
  8. 青龙面板 抖音极速版
  9. 设计思维的定义与一些步骤
  10. creator创建密匙库,查询密匙库密匙,校对密匙
  11. linux播放器安装包下载地址,linux安装MPLAYER播放器
  12. 2019级软件工程应用与实践-人工智能快递柜(代码分析7)
  13. python最长的单词判断_Python 找出英文单词列表(list)中最长单词链
  14. matlab 二维地图常用绘图函数用法(plot,fill,rectangle)
  15. SQL Server 排序函数 ROW_NUMBER和RANK 用法总结
  16. php.ini maxfileuploads,细说PHP高洛峰文件上传类源文件
  17. 橙色优学:互联网对传统行业说是机会,对年轻人来说更是风口
  18. 机器学习零基础?手把手教你用TensorFlow搭建图像识别系统
  19. 2019腾讯游戏客户端面试
  20. C语言 | Linux下的静态链接与动态链接

热门文章

  1. win10应用商店无法打开重新 加载
  2. ES6---数组的spread扩展运算符
  3. [论文笔记]Vision-based Control of 3D Facial Animation
  4. 计算机培训后的感言,计算机中级培训感言
  5. jira 工作日志导出 工具
  6. 机器学习实战——xgboost股票close预测
  7. 移动通信网络规划:误码率
  8. 选修课程期末作业 : 大象基金交易信息系统分析与设计报告
  9. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
  10. javascript中数据类型转换为Number