Zabbix monitoring using Python scripts to send mail

Source: Internet
Author: User

Recently implemented a Zabbix monitoring, open source software, the leverage of the several streets of Nagios ...

Environment: centos6.6 + Zabbix 2.4.5 + Python 2.6.6

Cd/usr/local/zabbix/share/zabbix/alertscripts #进入脚本存放目录
VI sendalertemail.sh #编辑, add the following code
#!/bin/sh
echo "$ $" | Mail-s "$" $
: wq! #保存退出
Chown zabbix.zabbix/usr/local/zabbix/share/zabbix/alertscripts/sendalertemail.sh
#设置脚本所有者为zabbix用户
chmod +x/usr/local/zabbix/share/zabbix/alertscripts/sendalertemail.sh
#设置脚本执行权限

Originally I used the MAILX, which is written in the above script to send e-mails, but sent Chinese messages garbled, have to send a Chinese message via Python

sendalertemail.sh Script content:

Cat sendalertemail.sh
#!/bin/bash
#date: 20160701
#author: LJB
#funciton: Send alert mail use MAILX
echo "******" >>/usr/local/zabbix/share/zabbix/alertscripts/sendmail.log 2>&1
echo "$" "$" "$" >>/usr/local/zabbix/share/zabbix/alertscripts/sendmail.log 2>&1
/usr/bin/python/usr/local/zabbix/share/zabbix/alertscripts/send_alert_mail.py "$" "$" "$" >>/usr/local/ Zabbix/share/zabbix/alertscripts/sendmail.log 2>&1
echo "******" >>/usr/local/zabbix/share/zabbix/alertscripts/sendmail.log 2>&1

Note that the arguments must be extended in quotation marks, otherwise the parameters will not be fully recognized.
send_alert_mail.py Script content:

Cat send_alert_mail.py
#!/usr/bin/python
#coding =utf-8
‘‘‘
@date 20160808
@author LJB
‘‘‘
Import Sys
Import Smtplib
From Email.mime.text import Mimetext
mailto_list=[' [email protected]
Mail_host= "Smtp.yourdomain.com" #设置服务器
mail_user= "User" #用户名
mail_pass= "Password" #口令
mail_postfix= "YourDomain.com" #发件箱的后缀

def send_mail (to_list,sub,content):
Me= "Zabbix-alert-center" + "<" +mail_user+ "@" +mail_postfix+ ">"
msg = Mimetext (content,_subtype= ' plain ', _charset= ' utf-8 ')
msg[' Subject ' = Sub
Msg[' from '] = Me
Msg[' to '] = ";". Join (To_list)
Try
Server = Smtplib. SMTP ()
Server.connect (Mail_host)
Server.login (mail_user+ "@" +mail_postfix,mail_pass)
Server.sendmail (Me, To_list, msg.as_string ())
Server.close ()
Return True
Except Exception, E:
Print str (e)
Return False
if __name__ = = ' __main__ ':
If Send_mail (Mailto_list,sys.argv[2],sys.argv[3]):
Print "Sent successfully"
Else
Print "Send Failed"

Zabbix Web page settings:

Add outgoing message type
Add media type to the ZABBIX monitoring page
administration--Media types-create Media type

To receive alert messages for Administrator account settings:


administration-users--Zabbix Administrators

Add recipient mailbox in Send to location

Setting up triggers

Finally you can stare at your mailbox and wait for the mail to arrive.

Zabbix monitoring using Python scripts to send mail

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.