#!/usr/bin/env python
# coding=utf-8
import json
import urllib
import urllib2
from datetime import datetime
import cookielib
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def zab_api(data):
url = 'http://116.213.207.5:15080/zabbix/api_jsonrpc.php'
header = {"Content-Type": "application/json"}
request = urllib2.Request(url, data, header)
result = urllib2.urlopen(request)
response = json.loads(result.read())
result.close()
return response
auth_data = json.dumps({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "admin",
"password": "dachui$5zabbix"
}, "id": 2
})
auth = zab_api(auth_data)["result"]
hostget = json.dumps({
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["hostid", "host"],
"selectInterfaces": ["interfaceid", "ip"]
}, "id": 2, "auth": auth
})
def getgraphid(hostid):
graph1 = json.dumps({
"jsonrpc": "2.0",
"method": "graph.get",
"params": {
"output": "extend",
"hostids": hostid,
"sortfield": "name"
},
"auth": auth,
"id": 1
})
for graph in zab_api(graph1)['result']:
print graph['name'], " : ", graph['graphid']
def getcookie():
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
values = {"name": "admin", "password": "dachui$5zabbix", 'autologin': 1, "enter": 'Sign in'}
data = urllib.urlencode(values)
request = urllib2.Request('http://116.213.207.5:15080/zabbix/index.php', data)
try:
urlOpener.open(request, timeout=10)
return urlOpener
except urllib2.HTTPError, e:
print e
def getgraph(graid):
gr_url = "http://116.213.207.5:15080/zabbix/chart2.php"
# http://116.213.207.5:15080/zabbix/chart6.php 饼图
stime = datetime.now().strftime('%Y%m%d%H%M%S')
values = {"graphid": graid, "period": 86400, "stime": stime, "width": 800, "height": 200}
data = urllib.urlencode(values)
request = urllib2.Request(gr_url, data)
url = getcookie().open(request)
image = url.read()
imagename = "img/%s.png" % graid
f = open(imagename, 'wb')
f.write(image)
def SendMail(imglist):
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'zabbix report'
msgRoot['From'] = "[email protected]"
to_list = ["[email protected]", "[email protected]"]
msgRoot['To'] = ",".join(to_list)
for img in imglist:
imagename = "img/%s.png" % img
msgImage = MIMEImage(open(imagename, 'rb').read())
msgImage.add_header('Content-ID', str(img))
msgRoot.attach(msgImage)
sendText = '<html><body><p>web服务器 cpu使用:</p>' + \
'<p><img src="cid:525"></p>' + \
'<p><img src="cid:550"></p>' + \
'<p>web服务器 内存剩余:</p>' + \
'<p><img src="cid:534"></p>' + \
'<p><img src="cid:553"></p>' + \
'<p>web服务器 网络使用:</p>' + \
'<p><img src="cid:568"></p>' + \
'<p><img src="cid:575"></p>' + \
'<p>web服务器 连接数:</p>' + \
'<p><img src="cid:626"></p>' + \
'<p><img src="cid:628"></p>' + \
'<p>mysql服务器 cpu使用:</p>' + \
'<p><img src="cid:557"></p>' + \
'<p><img src="cid:564"></p>' + \
'<p>mysql服务器 内存剩余:</p>' + \
'<p><img src="cid:560"></p>' + \
'<p><img src="cid:567"></p>' + \
'<p>mysql服务器 网络使用:</p>' + \
'<p><img src="cid:583"></p>' + \
'<p><img src="cid:590"></p>' + \
'<p>mysql服务器 查询情况:</p>' + \
'<p><img src="cid:614"></p>' + \
'<p><img src="cid:631"></p>' + \
'</body></html>'
msgText = MIMEText(sendText, 'html', 'utf-8')
msgRoot.attach(msgText)
smtp = smtplib.SMTP_SSL()
smtp.connect('smtp.exmail.qq.com', "465")
smtp.login("[email protected]", "lianggyA01")
smtp.sendmail(msgRoot['From'], to_list, msgRoot.as_string())
smtp.quit()
# for hostmsg in zab_api(hostget)['result']:
# print hostmsg['host'], ' : ', hostmsg['hostid']
# Zabbix server : 10084
# 192.168.1.6 : 10105
# 192.168.1.7 : 10106
# 192.168.1.2 : 10107
# 192.168.1.3 : 10108
# web cpu:525 mem:534 net:568 connect:626
# slave cpu:550 mem:553 net:575 connect:628
#mysql cpu:557 mem:560 net:583 qps: 614
# mysql-slae cpu:564 mem:567 net:590 qps:631
#getgraphid("10084")
imglist = [525,534,568,626,550,553,575,628,557,560,583,614,564,567,590,631]
for graphid in imglist:
getgraph(graphid)
SendMail(imglist)