1.前言
当事件发生,根据事件执行相应动作,创建触发器动作,当触发器事件满足必要条件时,会触发相应的动作。
2.创建动作并定义触发器条件及相关操作
3.配置报警媒介及配置用户并在用户组添加用户权限,这里不再阐述。
4.内容发送脚本如下:
#!/usr/bin/python
#coding=utf‐8
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib,sys,os,time,re,requests
from smtplib import SMTP
user='Admin' #Zabbix Web登录用户名
password='zabbix' #Zabbix Web登录用户密码
graph_path='/tmp/zabbix/graph' #图片存放路径
graph_url='http://192.168.196.321/chart.php' #图表访问地址
loginurl="http://192.168.196.321/index.php" #Zabbix Web登录地址
host='192.168.196.321'
to_email=sys.argv[1]
subject=sys.argv[2]
from_emailname = sys.argv[3]
mail_passward = sys.argv[4]
subject=subject.decode('utf‐8')
smtp_host = 'smtp.163.com'
def get_itemid():
itemid=re.search(r'ITEM ID:(\d+)',sys.argv[5]).group(1)
return itemid
def get_graph(itemid):
session=requests.Session()
try:
loginheaders={
"Host":host,
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,imag
png,*/*;q=0.8"
}
payload = {
"name":user,
"password":password,
"autologin":"1",
"enter":"Sign in",
}
login=session.post(url=loginurl,headers=loginheaders,data=payload)
graph_params={
"from" :"now‐10m",
"to" : "now",
"itemids" : itemid,
"width" : "400",
}
graph_req=session.get(url=graph_url,params=graph_params)
time_tag=time.strftime("%Y%m%d%H%M%S", time.localtime())
graph_name='baojing_'+time_tag+'.png'
graph_name = os.path.join(graph_path, graph_name)
with open(graph_name,'wb') as f:
f.write(graph_req.content)
return graph_name
except Exception as df:
print df
return False
def text_to_html(text):
d=text.splitlines()
html_text=''
for i in d:
i='' + i + '<br>'
html_text+=i + '\n'
return html_text
def send_mail(graph_name,from_emailname,mail_passward):
msg = MIMEMultipart('related')
with open(graph_name,'rb') as f:
graph=MIMEImage(f.read())
graph.add_header('Content‐ID','imgid1')
text=text_to_html(sys.argv[3])
html="""
<html>
<body>
%s <br><img src="cid:imgid1">
</body>
</html>
""" % text
html=MIMEText(html,'html','utf‐8')
msg.attach(html)
msg.attach(graph)
msg['Subject'] = subject
msg['From'] = from_email
try:
server=SMTP(smtp_host,"25")
server.starttls()
server.login(from_emailname,mail_passward)
server.sendmail(from_email,to_email,msg.as_string())
server.quit()
except smtplib.SMTPException as t:
print t
def run():
itemid=get_itemid()
graph_name=get_graph(itemid)
send_mail(graph_name,from_emailname,mail_passward)
if __name__ =='__main__':
run()