Zabbix3.0.2 using an external Python script to implement the Mail alert method

Source: Internet
Author: User
Tags all mail
1, find the default script path (I am in 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 Store Path

2. Create a new mail.py file in the directory, script reference:

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

Vim mail.py

#!/usr/bin/python

#coding: Utf-8

#author: Itnihao

#mail: itnihao@qq.com

#url: https://github.com/itnihao/zabbix-book/edit/master/06-chapter/zabbix_sendmail_v2.py script directly under Modify the copy will appear formatting problems ~

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 = ' 1234567890 '

#163 Mail

#smtp_server = ' smtp.163.com '

#smtp_port = 25

#smtp_user = ' itnihao_zabbix@163.com '

#smtp_pass = ' 1234567890 '

#QQ Mail

Smtp_server = ' smtp.qq.com '

Smtp_port = 25

Smtp_user = ' itnihao_zabbix@qq.com '

Smtp_pass = ' 1234567890 '

def send_mail (mail_to,subject,content):

msg = Mimetext (content,_subtype= ' plain ', _charset= ' utf-8 ')

msg[' Subject ' = 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 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 (' Subject ', 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 permissions for Scripts

# chown Zabbix.zabbix mail.py

# chmod 775 mail.py

4,zabbix Web Configuration

In configuring Media type selection, create a media type

Fill in the following information, type Select script, need to fill in zabbix3.0 need the following 3 parameters.

Create user or set alarm in Admin user

Here is the message that the message was notified to after the failure, that is, the recipient.

Create an action to implement a mail alert, in the configuration-action-create action

(My picture is not the same as the template given below, it is recommended to use the template, because I use the server is not in the Chinese region, all mail headers will be garbled.) So I've shortened the title of the picture. )

Name:

Action-email

Default Subject:

Fault {TRIGGER. STATUS}, server: {HOSTNAME1} occurred: {Trigger.name} failed!

Default message:

Alarm host: {HOSTNAME1}

Alarm time: {EVENT. DATE} {EVENT. TIME}

Alarm level: {TRIGGER. SEVERITY}

Warning message: {trigger.name}

Alarm item: {TRIGGER. KEY1}

Problem Details: {item.name}:{item. VALUE}

Current status: {TRIGGER. Status}:{item. VALUE1}

Event Id:{event.id}

Please see details in montoring-events.

Recovery Subject:

Restore {TRIGGER. STATUS}, server: {HOSTNAME1}: {trigger.name} restored!

Recovery message:

Alarm host: {HOSTNAME1}

Alarm time: {EVENT. DATE} {EVENT. TIME}

Alarm level: {TRIGGER. SEVERITY}

Warning message: {trigger.name}

Alarm item: {TRIGGER. KEY1}

Problem Details: {item.name}:{item. VALUE}

Current status: {TRIGGER. Status}:{item. VALUE1}

Event Id:{event.id}

The alarm has been restored, please relax your mood.

Here you have to note the type of operation, the message sent, and the script for the message. Have to choose the right.

5, test Zabbix send mail

Find an agent client and stop. Then look at the alarm mail ~

After killing, you can see that the success has occurred in the send here ~

Note: Zabbix alarms are usually sent in 5 minutes. For example: 16:20 stopped, 16:25 will receive the fault mail, 16:30 processing. 16:35 can receive recovery information ~ ...

6. Summary

I use the Ubuntu 16.4 system here, began to taste the MAILX hair, and then less library files can not be installed, and later switch other methods have not received the mail finally selected with an external Python script to implement ~ The Ubuntu system has many software dependencies that are not self-contained. Apt-get is really useless, using 126 mailbox Client authorization code cannot be logged in the Python script. QQ mailbox can only happen to yourself. The last choice is the company's corporate mailbox. But because the Zabbix will send the Mail frequently many also be thrown into the rubbish bin ... Finally in the enterprise mailbox to do the forwarding to the QQ mailbox.

Ubuntu is not a problem with development and learning, but there are a lot of things to use as a server.

Correct posture: centos+zabbix+ Enterprise Mailbox Follow-up if there is a chance to write the next Zabbix in the alarm.

Related Article

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.