#/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文档相关推荐

  1. python打印生成word_使用python调用zabbix接口截取监控图并生成Word文档

    #/usr/bin/python #coding=utf-8 import json,urllib2,time,os,re,shutil from docx import Document from ...

  2. python实现生成word文档并转为pdf

    python实现生成word文档,格式转为pdf 使用的是python-docx模块,在生成word文档后转为pdf格式是使用的是docx2pdf中的convert(使用convert转换时,要先创建 ...

  3. python读取word指定内容_python解析html提取数据,并生成word文档实例解析

    简介 今天试着用ptyhon做了一个抓取网页内容,并生成word文档的功能,功能很简单,做一下记录以备以后用到. 生成word用到了第三方组件python-docx,所以先进行第三方组件的安装.由于w ...

  4. 用 Python 自动生成 Word 文档并在指定位置插入图片

    用Python生成Word文档 在指定位置插入图片 首先当然需要第三方库啦(▽) 天才第一步,第三方的库O(∩_∩)O哈哈~ 参照转发的是这位大佬的博客:https://blog.csdn.net/c ...

  5. python生成word文档_python实现的生成word文档功能示例

    本文实例讲述了python实现的生成word文档功能.分享给大家供大家参考,具体如下: 每月1次的测试费用报销,需要做一个文档.干脆花点时间写个程序吧. # -*- coding: utf-8 -*- ...

  6. python新建word文档_使用Python 自动生成 Word 文档的教程

    当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...

  7. python自动生成和读取word_使用Python自动生成Word文档的教程

    当然要用第三方库啦 :) 使用以下命令安装: pip install python-docx 使用该库的基本步骤为: 1.建立一个文档对象(可自动使用默认模板建立,也可以使用已有文件). 2.设置文档 ...

  8. freemarker生成word文档,通过libreoffice完美转为pdf文件排版不乱(包含调用浏览器打印pdf)

    了解libreoffice的朋友都知道,freemarker是用xml生成的word文档,libreoffice打开这些文档会不兼容,会直接显示xml代码或排版差别很大,尤其是文档中包含一些复杂表格的 ...

  9. Poi 如何使用Java和POI技术生成折线图,柱状图,饼状图导出到word文档

    这篇文章主要介绍POI生成图表并导出word文档的基本操作.主要介绍三种图表:折线图.柱状图.饼状图. 一.效果展示 使用Java和POI技术生成的折线图,柱状图,饼状图的效果如下图所示: 二.环境准 ...

最新文章

  1. IDEA配置GitHub报错GitHub Invalid authentication data.404 Not Found-Not Found
  2. mysql timestamp 转型_MySQL的timestamp类型自动更新问题【转】
  3. linux c 删除 文件 第一行
  4. MATLAB 线性规划实例应用
  5. groupby多个字段性能_MySQL今天我们说下分组(group by)
  6. linux系统中/etc/syslog.conf文件解读
  7. 采访问题 What is your role at XX and what are your responsibilities
  8. loop和[bx]的联合应用
  9. ARKit:增强现实技术在美团到餐业务的实践
  10. 越狱(第二季)19集,开始了!
  11. 翻译 - 【Dojo Tutorials】Part 2 - Developing a Dojo Mobile Application: FlickrView
  12. maven没有servlet(创建servlet后报错)
  13. 数据库锁机制1------共享锁
  14. Windows XP优化指南
  15. pr视频两边模糊_Premiere视频教程 在使用Premiere中常见3种模糊效果的制作方法
  16. PDF文件太大,两个超级实用方法压缩PDF
  17. 谷歌邮箱lmap服务器填什么_GMAIL邮箱IMAP设置
  18. 微信录制视频转圈效果如何实现?
  19. 牛客网经典120道Java面试常见题(试题+答案)
  20. JavaWeb-Servlet(上)

热门文章

  1. AI算法起家的今日头条为何败给了色情?一文告诉你机器学习何以搞不定鉴黄
  2. 分页场景(limit,offset)为什么会慢?
  3. 单元测试框架怎么搭?快来看看新版Junit5的这些神奇之处吧!
  4. 十个最常用的JVM 配置参数
  5. 它是谁?一个比 c3p0 快200倍的数据库连接池!
  6. Spring Cloud Sleuth 进阶实战
  7. 深入理解Ribbon之源码解析
  8. 特征工程在实际业务中的应用!
  9. 在一家开源公司工作的体验!
  10. PYTHON编程导论群问题汇总(四)