Python uses Nagios to add the alarm notification function, pythonnagios
Nagios is an open-source free network monitoring tool that can effectively monitor the status of Windows, Linux and Unix hosts, network settings such as vswitches and routers, and printers. If the system or service status is abnormal, an email or text message alert will be sent immediately to the website O & M personnel. After the status is restored, a normal email or text message notification will be sent.
Nagios sends alarm emails by calling APIs on the public platform. Before the formal operation, there are several preparations to do. Install nagios first. You can use the one-click nagios installation script provided by me. Then, I apply for an enterprise number on the public platform. When I apply for an enterprise number, I fill in the organization and can use it without authentication. Finally, log on to the public platform to add the address book and create an application (such as nagios). Record the Application id and use it later. Set user permissions.
Script
After nagios and the platform are ready, download the Command Script. Right-click Download and save. Then upload the file to the nagios server, such as the/usr/local/nagios/python/weixin directory, and set the File Permission to executable. Create the config. py file in the/usr/local/nagios/python/weixin directory and add the following configuration.
#coding:utf-8CorpID='xxxxx'Secret='xxxxxxxxxxxxxxxxx'DEBUG=0ToUser='sijitao.net'AgentId=1
The CorpID and Secret can be obtained by logging on to the public platform and opening the added Application (such as nagios.
The parameters passed by the script command are separated by the "-@-" symbol. For details, refer to my github address: https://github.com/zhangnq/nagios/tree/master/weixin.
Nagios Configuration
The general configuration on nagios is as follows.
Add the weixin command to the commands. cfg command file:
define command{command_name notify-host-by-weixincommand_line /usr/local/nagios/python/weixin/NotifyByWeixin.py "host-@@-$NOTIFICATIONTYPE$-@@-$HOSTNAME$-@@-$HOSTSTATE$-@@-$HOSTADDRESS$-@@-$HOSTOUTPUT$-@@-$CONTACTALIAS$"}define command{command_name notify-service-by-weixincommand_line /usr/local/nagios/python/weixin/NotifyByWeixin.py "service-@@-$NOTIFICATIONTYPE$-@@-$SERVICEDESC$-@@-$HOSTALIAS$-@@-$HOSTADDRESS$-@@-$SERVICESTATE$-@@-$SERVICEOUTPUT$-@@-$CONTACTALIAS$"}
Add a contact template to the templates. cfg template file:
define contact{name weixin-contactservice_notification_period 24x7host_notification_period 24x7service_notification_options w,u,c,r,f,shost_notification_options d,u,r,f,sservice_notification_commands notify-service-by-weixinhost_notification_commands notify-host-by-weixinregister 0}
Add a notification contact to the contacts. cfg contact. the alias name must be the same as the name account in the address book of the public platform before the notification is sent successfully.
define contact{contact_name zhangnq-weixinuse weixin-contactalias zhangnqemail admin@sijitao.net}
Finally, add the zhangnq-weixin contact When configuring the service, and then you can send an alarm email.
I would like to introduce so much to you about Python, which I shared with you, using Nagios's alarm notification function. I hope it will be helpful to you.
Articles you may be interested in:
- Python monitors host survival and sends an alert via email