python抓取zabbix图形,并发送邮件

最近十九大非常烦,作为政府网站维护人员,简直是夜不能寐。各种局子看着你,内保局,公安部,360,天融信,华胜天成,中央工委,政治委员会...

360人员很傻X,作为安全公司,竟然不能抓到XX网站流量,在我们机房放的探针更是搞笑,让我们手工上报流量数据。白天还行,晚上怎么办?给他写个脚本吧

首先,用requests抓取zabbix流量图

 

#-*- coding:utf-8 -*-
import requests,os
import datetime

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('C:\\Users\\Administrator\\Desktop')
s=requests.session()
loginurl='http://10.1.2.61/index.php'
#如果是中文,enter里是 注册。如果是英文,enter里是sign in logininfo = {"name":'zabbix的用户名','password':'zabbix密码','autologin':1,"enter":'注册'} req=s.post(loginurl,data=logininfo,headers=header) now=datetime.datetime.now() nowtime=now.strftime('%Y%m%d%H%M%S') durl='http://10.1.2.61/chart2.php' payload={'graphid':715, 'period':3600, 'stime':nowtime} picurl=s.get(durl,params=payload,headers=header) with open ('1.png','wb') as code: code.write(picurl.content)

  接着发带图片的邮件,下面这个脚本从网上抄的。偷个懒

#-*- coding:utf-8 -*-
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  
import zabbix
# 格式化邮件地址  
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', '收件人2']  
	msg = MIMEMultipart()   
	msg['From'] = formatAddr('标题 <%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', '<image1>')  
	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('C:\\Users\\Administrator\\Desktop')
	body = """ 
	<h1>XX监控图片</h1> 
	<img src="cid:image1"/>
	"""  
	sendMail(body, '1.png') 

  

posted @ 2017-10-20 10:12  一起走过的路  阅读(705)  评论(0编辑  收藏  举报