Python script for Zabbix system email alert
There are many ways to configure email alerts in the Zabbix system, such as the built-in mail and zabbix + msmtp + mutt. However, using a Python script is a simple and effective method.
The main steps are as follows;
1. Set the zabbix_server.conf File
AlterScript =/usr/local/zabbix/share/zabbix/alterscript
2. Create an alarm script
Cd/usr/local/zabbix/share/zabbix/alterscript/
Sudo vim sendEmail. sh
#! /Usr/bin/env python
# Coding: UTF-8
Import time
Import smtplib
Import logging
From email. mime. text import MIMEText
Import sys
LOG_FILENAME = "/var/log/email_python.log"
Mail_host = 'mstp .163.com'
Mail_user = '[email protected]'
Mail_pass = 'xxxxx'
Mail_postfix = 'xxxxx'
Def send_mail (to_list, subject, content, format = 'html '):
Try:
Me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
Msg = MIMEText (content, format, 'utf-8 ')
Msg ["Accept-Language"] = "zh-CN"
Msg ["Accept-Charset"] = "ISO-8859-1, UTF-8"
Msg ['subobject'] = Subject
Msg ['from'] = me
Msg ['to'] = to_list
S = smtplib. SMTP ()
S. connect (mail_host, "25 ")
S. login (mail_user, mail_pass)
S. sendmail (me, to_list, msg. as_string ())
S. close ()
Except t Exception, e:
Logging. basicConfig (filename = LOG_FILENAME, level = logging. DEBUG)
Logging. error (time. strftime ('% Y-% m-% d % H: % I: % m', time. localtime (time. time () + e)
If _ name _ = "_ main __":
Send_mail (sys. argv [1], sys. argv [2], sys. argv [3])
3. Set the alarm script permission
Sudo chmod zabbix: zabbix sendEmail. sh
Sudo chown 755 sendEmail. sh
4. Finally, you only need to configure the zabbix frontend.
Some Zabbix Tutorials:
Install and deploy the distributed monitoring system Zabbix 2.06
Install and deploy the distributed monitoring system Zabbix 2.06
Install and deploy Zabbix in CentOS 6.3
Zabbix distributed monitoring system practice
Under CentOS 6.3, Zabbix monitors apache server-status
Monitoring MySQL database Parameters Using Zabbix in CentOS 6.3
Install Zabbix 2.0.6 in 64-bit CentOS 6.2
ZABBIX details: click here
ZABBIX: click here
This article permanently updates the link address: