Home > Article > Backend Development > How zabbix uses python script to send alarm emails
Preface
zabbix is a very powerful monitoring tool that can monitor server data on Linux and Windows. You can also extend the default monitoring items through custom keys, but the information provided by the built-in email alarm is not Not very friendly. This article wants to use a custom script to send the corresponding image and URL connection at the same time as the alarm email.
The steps are as follows:
1. Edit the zabbix_server.conf file and modify the AlertScriptsPath parameter, which is used to specify the absolute path of the external script.
vim /etc/zabbix/zabbix_server.conf AlertScriptsPath=/usr/lib/zabbix/alertscripts
2. Upload the new py script to the absolute path specified by the AlertScriptsPath parameter. The py file is as follows:
#! /usr/bin/env python # coding:utf-8 ''' [INFORMATION] Zabbix Send Email With Python AUTHOR : Wing GitHub : https://github.com/wing324 Email : wing324@126.com ''' from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr import smtplib import sys def send_mail(_to_email,_subject,_message): # 定义邮件发送 smtp_host = 'smtp.xxx.xx' from_email = 'xxx@xxx.xx' passwd = 'xxxxxx' msg = MIMEText(_message,'plain','utf-8') msg['Subject'] = _subject smtp_server = smtplib.SMTP(smtp_host,25) smtp_server.login(from_email,passwd) smtp_server.sendmail(from_email,[_to_email],msg.as_string()) smtp_server.quit() if __name__ == '__main__': send_mail(sys.argv[1],sys.argv[2],sys.argv[3])
3. Modify the permissions of the python script
chown -R zabbix:zabbix zabbix_send_email.py chmod 755 zabbix_send_email.py
4. Zabbix web side configuration
Administration –> Media types –> Create media type
##Create a test user Administration –> ; Users –> Create user Specify media for the newly created user:Administration –> Users –> Create user –> Media Create action to implement email alarmConfiguration –> Actions –> Create action 5. Zabbix test sends emails