Zabbix3.0.2 use an external python script to implement the Mail alarm method

Source: Internet
Author: User
This article details Zabbix3.0.2 how to use an external python script to implement mail alarm method 1, find the path where the script is stored by default (I put it under the default path, if you want to change the path, modify the zabbix_server.conf configuration file)

/Usr/local/zabbix-server/share/zabbix/alertscripts script storage path

2. create a mail. py file in the directory. for the script, refer:

Cd/usr/local/zabbix-server/share/zabbix/alertscripts

Vim mail. py

#! /Usr/bin/python

# Coding: UTF-8

# Author: itnihao

# Mail: itnihao@qq.com

# Url: the https://github.com/itnihao/zabbix-book/edit/master/06-chapter/zabbix_sendmail_v2.py script directly down to modify the copy will have a format problem ~

Import smtplib

From email. mime. text import MIMEText

Import OS

Import argparse

Import logging

Import datetime

# QQ enterprise

# Smtp_server = 'smtp .exmail.qq.com'

# Smtp_port = 25

# Smtp_user = 'itnihao _ zabbix@itnihao.com'

# Smtp_pass = '123'

#163 Mail

# Smtp_server = 'smtp .163.com'

# Smtp_port = 25

# Smtp_user = 'itnihao _ zabbix@163.com'

# Smtp_pass = '123'

# QQ Mail

Smtp_server = 'smtp .qq.com'

Smtp_port = 25

Smtp_user = 'itnihao _ zabbix@qq.com'

Smtp_pass = '123'

Def send_mail (mail_to, subject, content ):

Msg = MIMEText (content, _ subtype = 'plain ', _ charset = 'utf-8 ')

Msg ['subobject'] = unicode (Subject, 'utf-8 ')

Msg ['from'] = smtp_user

Msg ['to'] = mail_to

Global sendstatus

Global senderr

Try:

If smtp_port = 465:

Smtp = smtplib. SMTP_SSL ()

Else:

Smtp = smtplib. SMTP ()

Smtp. connect (smtp_server, smtp_port)

Smtp. login (smtp_user, smtp_pass)

Smtp. sendmail (smtp_user, mail_to, msg. as_string ())

Smtp. close ()

Print 'send OK'

Sendstatus = True

Except t Exception, e:

Senderr = str (e)

Print senderr

Sendstatus = False

Def logwrite (sendstatus, mail_to, content ):

Logpath = '/var/log/zabbix/alert'

If not sendstatus:

Content = senderr

If not OS. path. isdir (logpath ):

OS. makedirs (logpath)

T = datetime. datetime. now ()

Daytime = t. strftime ('% Y-% m-% d ')

Daylogfile = logpath + '/' + str (daytime) + '. log'

Logging. basicConfig (filename = daylogfile, level = logging. DEBUG)

OS. system ('chown zabbix. zabbix {0} '. format (daylogfile ))

Logging.info ('* 130)

Logging. debug (str (t) + 'mail send to {0}, content is: \ n {1} '. format (mail_to, content ))

If _ name _ = "_ main __":

Parser = argparse. ArgumentParser (description = 'send mail to user for zabbix alerting ')

Parser. add_argument ('mail _ to', action = "store", help = 'the address of The E-mail that send to user ')

Parser. add_argument ('subobject', action = "store", help = 'the subject of The e-mail ')

Parser. add_argument ('content', action = "store", help = 'The content of The e-mail ')

Args = parser. parse_args ()

Mail_to = args. mail_to

Subject = args. subject

Content = args. content

Send_mail (mail_to, subject, content)

Logwrite (sendstatus, mail_to, content)

3. modify the script permission.

# Chown zabbix. zabbix mail. py

# Chmod 775 mail. py

4. zabbix web configuration

In configure media type selection, create a media type

6. Summary

The ubuntu 16.4 System I used here started to use mailx. later, some library files could not be installed, later, when other methods fail to receive emails, the external python script will be selected for implementation ~ · Ubuntu systems have many software dependent environments that are not provided. apt-get is really not easy to use. you cannot log on to the python script using the client authorization code in the 126 mailbox. QQ mail can only happen to you. the final choice is the company's enterprise mailbox. However, because zabbix frequently sends many emails, it is also thrown into the garbage bin... Finally, the enterprise mailbox is forwarded to the QQ mailbox.

Ubuntu development and learning are fine, but there are still many problems with using it as a server.

Correct posture: if centos + zabbix + enterprise mail has the chance to write an alarm in zabbix.

The above is a detailed description of Zabbix3.0.2 using an external python script to implement the Mail alarm method. For more information, see other related articles in the first PHP community!

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.