Python script for Zabbix system email alert

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.