Heim  >  Artikel  >  Backend-Entwicklung  >  Python利用Nagios增加微信报警通知的功能

Python利用Nagios增加微信报警通知的功能

WBOY
WBOYOriginal
2016-06-10 15:05:581883Durchsuche

Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。

Nagios是调用微信公共平台的api接口发送报警邮件。在正式操作之前,有几个准备工作要做。先安装nagios,可以使用我提供的nagios一键安装脚本。然后是去微信公共平台申请一个企业号,我申请时填的是组织,没有认证也可以使用。最后登录公共平台添加通讯录,和新建应用(例如nagios),记录应用的id后面要用。用户权限什么自己设定。

微信脚本

nagios和微信平台都准备好之后,接下来下载微信命令脚本。右键点击 下载 ,保存。然后上传到nagios服务器,例如/usr/local/nagios/python/weixin目录,设置文件权限为可执行。在/usr/local/nagios/python/weixin目录新建config.py文件,添加如下配置。

#coding:utf-8
CorpID='xxxxx'
Secret='xxxxxxxxxxxxxxxxx'
DEBUG=0
ToUser='sijitao.net'
AgentId=1

其中CorpID和Secret这两个可以通过登录微信公共平台,打开添加的应用(例如nagios)获取。

微信脚本命令传递的参数通过“-@@-”符号来分割,详细说明可以参考我的github地址:https://github.com/zhangnq/nagios/tree/master/weixin

Nagios配置

下面就是nagios上的常规配置。

commands.cfg命令文件中添加weixin命令:

define command{
command_name notify-host-by-weixin
command_line /usr/local/nagios/python/weixin/NotifyByWeixin.py "host-@@-$NOTIFICATIONTYPE$-@@-$HOSTNAME$-@@-$HOSTSTATE$-@@-$HOSTADDRESS$-@@-$HOSTOUTPUT$-@@-$CONTACTALIAS$"
}
define command{
command_name notify-service-by-weixin
command_line /usr/local/nagios/python/weixin/NotifyByWeixin.py "service-@@-$NOTIFICATIONTYPE$-@@-$SERVICEDESC$-@@-$HOSTALIAS$-@@-$HOSTADDRESS$-@@-$SERVICESTATE$-@@-$SERVICEOUTPUT$-@@-$CONTACTALIAS$"
}

templates.cfg模板文件中添加联系人模板:

define contact{
name weixin-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-weixin
host_notification_commands notify-host-by-weixin
register 0
}

contacts.cfg联系人中添加微信通知联系人,这里alias名字要和微信公共平台通讯录中名字帐号一样才会发送成功。

define contact{
contact_name zhangnq-weixin
use weixin-contact
alias zhangnq
email admin@sijitao.net
}

最后在配置service的时候添加zhangnq-weixin这个联系人后就可以通过微信发送报警邮件了。

 

关于小编给大家分享的Python利用Nagios增加微信报警通知的功能就给大家介绍这么多,希望对大家有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn