HOST:{HOST.NAME} {HOST.IP} TIME:{EVENT.DATE} {EVENT.TIME} LEVEL:{TRIGGER.SEVERITY} NAME:{TRIGGER.NAME} messages:{ITEM.NAME}:{ITEM.VALUE} ID:{EVENT.ID}
HOST:{HOST.NAME} {HOST.IP} TIME:{EVENT.DATE} {EVENT.TIME} LEVEL:{TRIGGER.SEVERITY} NAME:{TRIGGER.NAME} messages:{ITEM.NAME}:{ITEM.VALUE} ID:{EVENT.ID}
A機器 [root@hf-01 ~]# vim /etc/zabbix/zabbix_server.conf 搜索 /alert AlertScriptsPath=/usr/lib/zabbix/alertscripts #報警腳本所在路徑
A機器 [root@hf-01 ~]# vim /usr/lib/zabbix/alertscripts/mail.py #!/usr/bin/env python #-*- coding: UTF-8 -*- import os,sys reload(sys) sys.setdefaultencoding('utf8') import getopt import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from subprocess import * def sendqqmail(username,password,mailfrom,mailto,subject,content): gserver = 'smtp.163.com' ##定義發郵件類型 gport = 25 try: msg = MIMEText(unicode(content).encode('utf-8')) msg['from'] = mailfrom msg['to'] = mailto msg['Reply-To'] = mailfrom msg['Subject'] = subject smtp = smtplib.SMTP(gserver, gport) smtp.set_debuglevel(0) smtp.ehlo() smtp.login(username,password) smtp.sendmail(mailfrom, mailto, msg.as_string()) smtp.close() except Exception,err: print "Send mail failed. Error: %s" % err def main(): to=sys.argv[1] subject=sys.argv[2] content=sys.argv[3] ##定義QQ郵箱的帳號和密碼,你須要修改爲你本身的帳號和密碼(請不要把真實的用戶名和密碼放到網上公開,不然你會死的很慘) sendqqmail('163郵箱','密碼','163郵箱',to,subject,content) if __name__ == "__main__": main() #####腳本使用說明###### #1. 首先定義好腳本中的郵箱帳號和密碼 #2. 腳本執行命令爲:python mail.py 目標郵箱 "郵件主題" "郵件內容" 保存退出
[root@hf-01 ~]# chmod 755 /usr/lib/zabbix/alertscripts/mail.py [root@hf-01 ~]#
[root@hf-01 ~]# cd /usr/lib/zabbix/alertscripts/ [root@hf-01 alertscripts]# python mail.py han1118feng@163.com "ssss" "dddd" [root@hf-01 alertscripts]#
[root@hf-01 alertscripts]# python mail.py han1118feng@163.com "郵件" "dddd" [root@hf-01 alertscripts]#
默認信息定義當發生報警時,zabbix會向你的郵箱發送什麼內容 HOST:{HOST.NAME} {HOST.IP} (定義主機名) TIME:{EVENT.DATE} {EVENT.TIME} (定義事件發生時間) LEVEL:{TRIGGER.SEVERITY} (定義示警度) NAME:{TRIGGER.NAME} (定義哪個觸發器發出的報警) messages:{ITEM.NAME}:{ITEM.VALUE} (發出告警會出現什麼狀態碼) ID:{EVENT.ID} (發生事件的id)
HOST:{HOST.NAME} {HOST.IP} TIME:{EVENT.DATE} {EVENT.TIME} LEVEL:{TRIGGER.SEVERITY} NAME:{TRIGGER.NAME} messages:{ITEM.NAME}:{ITEM.VALUE} ID:{EVENT.ID}