使用python调用zabbix接口截取监控图并生成Word文档
#/usr/bin/python
#coding=utf-8
import json,urllib2,time,os,re,shutil
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn
from selenium import webdriver
period='604800'#需要获取的监控周期,单位是秒
zabbix_server='http://zabbix.net'
url = "http://zabbix.net/api_jsonrpc.php"
header = {"Content-Type":"application/json"}
zabbix_user = 'xxxx'
zabbix_passwd = 'xxxx'
document = Document()
def http_access():
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": zabbix_user,
"password": zabbix_passwd
},
"id": 0
})
# 将请求头部重新编码
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
# 获取登录认证密钥。
try:
result = urllib2.urlopen(request)
except URLError as e:
print "Auth Failed, Please Check Your Name AndPassword:",e.code
else:
response = json.loads(result.read())
result.close()
return response['result']
def get_groups():
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"hostgroup.get",
"params":{
"output":["groupid","name"],
},
"auth":auth_code, # theauth id is what auth script returns, remeber it is string
"id":1,
})
# create request object
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
# get host list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
groups = {}
response = json.loads(result.read())
result.close()
print "Number Of Hosts: ", len(response['result'])
#print response
for group in response['result']:
print "ID",group['groupid'],"\t",group['name']
groups.update({group['groupid']:group['name']})
return groups
def get_graphid():
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "graph.get",
"params": {
"output": "graphid",
"groupids": groupid
},
"auth": auth_code,
"id": 1
})
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
# get graph list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
graphids=[]
response = json.loads(result.read())
for r in response['result']:
graphids.append(r['graphid'])
result.close()
print "Number Of Graphs: ", len(response['result'])
return graphids
if __name__ =='__main__':
auth_code = http_access()
groups = get_groups()
groupid = raw_input("select wanted group:")
date=raw_input('Input start time:') + '000000'#zabbix时间参数格式是20171123000000
graphids = get_graphid()
pic_save_path_dir= os.path.join('E:\\python\\report',groups[groupid]) #修改图片保存位置
if not os.path.exists(pic_save_path_dir):
os.makedirs(pic_save_path_dir)
driver=webdriver.Chrome()
for graph in graphids:
driver.get('%s/charts.php?fullscreen=1&graphid=%s&stime=%s&period=%s'%(zabbix_server,graph,date,period))
time.sleep(1)
driver.get_screenshot_as_file(pic_save_path_dir + "\\" + graph +".png")
for i in os.listdir(pic_save_path_dir):#遍历目录下的图片,并全部添加到Word文件里面
os.chdir(pic_save_path_dir)
document.add_picture(i, width=Inches(6))#
document.save(u'E:\\python\\report\\' + groups[groupid] +'.docx')
运行效果:
我才学Python2个月,写得有点乱,大神勿喷。
转载于:https://blog.51cto.com/5814380/2043360
使用python调用zabbix接口截取监控图并生成Word文档相关推荐
- python打印生成word_使用python调用zabbix接口截取监控图并生成Word文档
#/usr/bin/python #coding=utf-8 import json,urllib2,time,os,re,shutil from docx import Document from ...
- python实现生成word文档并转为pdf
python实现生成word文档,格式转为pdf 使用的是python-docx模块,在生成word文档后转为pdf格式是使用的是docx2pdf中的convert(使用convert转换时,要先创建 ...
- python读取word指定内容_python解析html提取数据,并生成word文档实例解析
简介 今天试着用ptyhon做了一个抓取网页内容,并生成word文档的功能,功能很简单,做一下记录以备以后用到. 生成word用到了第三方组件python-docx,所以先进行第三方组件的安装.由于w ...
- 用 Python 自动生成 Word 文档并在指定位置插入图片
用Python生成Word文档 在指定位置插入图片 首先当然需要第三方库啦(▽) 天才第一步,第三方的库O(∩_∩)O哈哈~ 参照转发的是这位大佬的博客:https://blog.csdn.net/c ...
- python生成word文档_python实现的生成word文档功能示例
本文实例讲述了python实现的生成word文档功能.分享给大家供大家参考,具体如下: 每月1次的测试费用报销,需要做一个文档.干脆花点时间写个程序吧. # -*- coding: utf-8 -*- ...
- python新建word文档_使用Python 自动生成 Word 文档的教程
当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...
- python自动生成和读取word_使用Python自动生成Word文档的教程
当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...
- freemarker生成word文档,通过libreoffice完美转为pdf文件排版不乱(包含调用浏览器打印pdf)
了解libreoffice的朋友都知道,freemarker是用xml生成的word文档,libreoffice打开这些文档会不兼容,会直接显示xml代码或排版差别很大,尤其是文档中包含一些复杂表格的 ...
- Poi 如何使用Java和POI技术生成折线图,柱状图,饼状图导出到word文档
这篇文章主要介绍POI生成图表并导出word文档的基本操作.主要介绍三种图表:折线图.柱状图.饼状图. 一.效果展示 使用Java和POI技术生成的折线图,柱状图,饼状图的效果如下图所示: 二.环境准 ...
最新文章
- IDEA配置GitHub报错GitHub Invalid authentication data.404 Not Found-Not Found
- mysql timestamp 转型_MySQL的timestamp类型自动更新问题【转】
- linux c 删除 文件 第一行
- MATLAB 线性规划实例应用
- groupby多个字段性能_MySQL今天我们说下分组(group by)
- linux系统中/etc/syslog.conf文件解读
- 采访问题 What is your role at XX and what are your responsibilities
- loop和[bx]的联合应用
- ARKit:增强现实技术在美团到餐业务的实践
- 越狱(第二季)19集,开始了!
- 翻译 - 【Dojo Tutorials】Part 2 - Developing a Dojo Mobile Application: FlickrView
- maven没有servlet(创建servlet后报错)
- 数据库锁机制1------共享锁
- Windows XP优化指南
- pr视频两边模糊_Premiere视频教程 在使用Premiere中常见3种模糊效果的制作方法
- PDF文件太大,两个超级实用方法压缩PDF
- 谷歌邮箱lmap服务器填什么_GMAIL邮箱IMAP设置
- 微信录制视频转圈效果如何实现?
- 牛客网经典120道Java面试常见题(试题+答案)
- JavaWeb-Servlet(上)