python zabbix 5.0 监控 图形 mail发送

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import HTMLParser
import urlparse
import urllib
import urllib2
import cookielib
import string
import requests,os
import datetime
import smtplib,os
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
from email.mime.multipart import MIMEMultipart 
from email.header import Header 
from email.utils import parseaddr, formataddr 

# 登录的主页面
loginurl = 'http://zabbix地址/zabbix/index.php'
posturl = 'http://zabbix地址/zabbix/chart2.php?graphid=2386&from=now-24h&to=now&height=201&width=1734&profileIdx=web.charts.filter&_=w3gfusus'
# 保存图片的地址
path = '/etc/zabbix/alertscripts/py-get-network-png/'  
# 图片名称
image_name = 'network'
# 日期
daytime = datetime.datetime.now().strftime('%Y-%m-%d')

chrome='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
header = {'User-Agent':chrome}
os.chdir(path)
s=requests.session()
logininfo = {"name":'zabbix用户名','password':'zabbix密码','autologin':1,"enter":'注册'}
req=s.post(loginurl,data=logininfo,headers=header)

picurl = s.get(posturl,headers=header)

file = path + image_name + daytime  + '.png'

with open(file,'wb') as f:
	f.write(picurl.content)
#####################################################

tupian = image_name + daytime + '.png'

def formatAddr(s): 
    name, addr = parseaddr(s) 
    return formataddr((Header(name, 'utf-8').encode(), addr)) 
def sendMail(body, image): 
    smtp_server = '邮箱smtp地址' 
    from_mail = '邮箱账号' 
    mail_pass = '邮箱密码' 
    to_mail = ['用户1@mail', '用户2@mail'] 
    msg = MIMEMultipart()  
    msg['From'] = formatAddr('外网24小时带宽 <%s>' % from_mail).encode() 
    msg['To'] = ','.join(to_mail) 
    msg['Subject'] = Header('监控', 'utf-8').encode() 
    msg.attach(MIMEText(body, 'html', 'utf-8')) 
    with open(image, 'rb') as f: 
        msgImage = MIMEImage(f.read()) 
    msgImage.add_header('Content-ID', '') 
    msg.attach(msgImage) 
    try: 
        s = smtplib.SMTP()      
        s.connect(smtp_server, "25")    
        s.login(from_mail, mail_pass) 
        s.sendmail(from_mail, to_mail, msg.as_string())  
        s.quit() 
    except smtplib.SMTPException as e: 
        print ('发送失败')
if __name__ == "__main__": 
    os.chdir(path)
    body = """
    

外网24小时带宽 监控图片

""" sendMail(body, tupian)

你可能感兴趣的:(zabbix,zabbix)