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