下面亲测可用

#!/usr/bin/env python
#coding:utf8
import urllib2
import sys
import json
import argparse #定义通过HTTP方式访问API地址的函数,后面每次请求API的各个方法都会调用这个函数
def requestJson(url,values):
data = json.dumps(values)
req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
response = urllib2.urlopen(req, data)
output = json.loads(response.read())
# print output
try:
message = output['result']
except:
message = output['error']['data']
print message
quit() return output['result'] #API接口认证的函数,登录成功会返回一个Token
def authenticate(url, username, password):
values = {'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': username,
'password': password
},
'id': ''
}
idvalue = requestJson(url,values)
return idvalue #定义更加主机分组名称获取各个hostid的函数
def getHosts(groupname,url,auth):
host_list = []
values = {'jsonrpc': '2.0',
'method': 'hostgroup.get',
'params': {
'output': 'extend',
'filter': {
'name': groupname
}, 'selectHosts' : ['hostid','host'],
},
'auth': auth,
'id': ''
}
output = requestJson(url,values)
for host in output[0]['hosts']:
host_list.append(host['hostid'])
return host_list #定义获取graphid的函数
def getGraphs(host_list,name_list, url, auth, columns, graphtype=0 ,dynamic=0):
if (graphtype == 0):
selecttype = ['graphid']
select = 'selectGraphs'
if (graphtype == 1):
selecttype = ['itemid', 'value_type']
select = 'selectItems'
values=({'jsonrpc' : '2.0',
'method' : 'graph.get',
'params' : {
'output' : ['graphid','name'],
select : [selecttype,'name'],
'hostids' : host_list,
'sortfield' : 'name',
'filter' : {
'name' : name_list, },
},
'auth' : auth,
'id' : 3
})
output = requestJson(url,values)
bb = sorted(output,key = lambda x:x['graphid'])
graphs = []
if (graphtype == 0):
for i in bb:
print i
graphs.append(i['graphid'])
if (graphtype == 1):
for i in bb:
if int(i['value_type']) in (0, 3):
graphs.append(i['itemid']) graph_list = []
x = 0
y = 0
for graph in graphs:
print "x is " + str(x)
print "y is " + str(y)
graph_list.append({
"resourcetype": graphtype,
"resourceid": graph,
"width": "",
"height": "",
"x": str(x),
"y": str(y),
"colspan": "",
"rowspan": "",
"elements": "",
"valign": "",
"halign": "",
"style": "",
"url": "",
"dynamic": str(dynamic)
})
x += 1
# print type(x)
# print type(columns)
if x == int(columns):
x = 0
y += 1
# print graph_list
return graph_list #定义创建screen的函数
def screenCreate(url, auth, screen_name, graphids, columns):
columns = int(columns)
if len(graphids) % columns == 0:
vsize = len(graphids) / columns
else:
vsize = (len(graphids) / columns) + 1 #先使用screen.get判断给定的screen name是否存在
values0 = {
"jsonrpc" : "2.0",
"method" : "screen.get",
"params" : {
"output" : "extend",
"filter" : {
"name" : screen_name,
}
},
"auth" : auth,
"id" : 2
}
values = {
"jsonrpc": "2.0",
"method": "screen.create",
"params": {
"name": screen_name,
"hsize": columns,
"vsize": vsize,
"screenitems": []
},
"auth": auth,
"id": 2
}
output0 = requestJson(url,values0)
print output0 #如果给定的screen name不存在则直接创建screen
if output0 == []:
print "The Given Screen Name Not Exists"
print "Creating Screen %s" %screen_name
for i in graphids:
values['params']['screenitems'].append(i)
output = requestJson(url,values)
else: #如果给定的screen name已经存在,直接创建screen是不行的,
#要么先使用screen.delete把原来的screen删除掉,然后再创建,
#要么直接使用screen.update更新原来那个screen,
#使用screen.delete会产生新的screenid,
#使用screen.update比较合理一点。
print "The Given Screen Name Already Exists"
update_screenid=output0[0]["screenid"]
print update_screenid
print "Updating Screen Name %s Screen ID %s" %(screen_name,update_screenid)
values1 = {
"jsonrpc" : "2.0",
"method" : "screen.update",
"params" : {
"screenid" : update_screenid,
"screenitems": []
},
"auth" : auth,
"id" : 2
}
output1 = requestJson(url,values1)
print output1
print "Updating Screen Name %s" %screen_name
for i in graphids:
values1['params']['screenitems'].append(i)
output = requestJson(url,values1) def main():
url = 'http://zbx.luge.cn/api_jsonrpc.php'
username = 'mikeluge'
password = 'lugehaoshuai'
auth = authenticate(url, username, password)
host_list = getHosts(groupname,url,auth)
print host_list
graph_ids = getGraphs(host_list,graphname, url, auth, columns)
screenCreate(url, auth, screenname, graph_ids, columns)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create Zabbix screen from all of a host Items or Graphs.')
parser.add_argument('-G', dest='graphname', nargs='+',metavar=('grah name'),
help='Zabbix Host Graph to create screen from')
parser.add_argument('-H', dest='hostname', nargs='+',metavar=('10.19.111.145'),
help='Zabbix Host to create screen from')
parser.add_argument('-g', dest='groupname', nargs='+',metavar=('linux server'),
help='Zabbix Group to create screen from')
parser.add_argument('-n', dest='screenname', type=str,
help='Screen name in Zabbix. Put quotes around it if you want spaces in the name.')
parser.add_argument('-c', dest='columns', type=int,
help='number of columns in the screen')
args = parser.parse_args()
print args
hostname = args.hostname
groupname = args.groupname
screenname = args.screenname
columns = args.columns
graphname = args.graphname
if columns is None:
columns = len(graphname)
# print columns
main()

zabbix根据graph name 做screen的更多相关文章

  1. zabbix 面板graph图上没有数据显示

    1. 问题: zabbix_server日志出现大量如下错误: query failed: [1526] Table has no partition for value 1507509984 2. ...

  2. 分布式监控系统之Zabbix基础使用

    前文我们了解了分布式监控系统zabbix的相关组件的作用和zabbix的部署,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13997582.html:今天我们 ...

  3. zabbix 布署实践【7 H3C网络设备监控模版制作思路】

    我们知道,zabbix安装后自带Template OS Linux 模版已满足了绝大部分Linux服务器的基础环境监控,只是我们在其模版上稍微修改,可配合将SWAP监控取消,另存为一个叫OS Linu ...

  4. zabbix自动截图留档_python版

    1 背景     每个DB Server都有zabbix监控,除了异常情况的报警信息外,也会在日检.周检.月检等工作中用到zabbix的监控数据,对zabbix监控数据会做两种处理:1 数据分析(环比 ...

  5. zabbix http服务监控实例

    1在被监控主机安装http服务 ,监听80端口 systemctl start httpd.service       启动服务  80端口已经启动 设定,监控80端口,当服务不当时先自动重启服务 2 ...

  6. DevOps-ISC,CSS,Prometheus,Ansible ,Terraform,zabbix

    https://www.terraform.io/ Terraform Use Infrastructure as Code to provision and manage any cloud, in ...

  7. 屏幕序列Screen Sequences

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. Graph Databases—The NOSQL Phenomenon阅读笔记

    本章内容着重对了NOSQL和RDBMS(关系型数据库管理系统)的不同,以及其各自背后设计时考虑的因素.然后接下来,着重讲述了NOSQL的4种分类方法.下面我们将对重要知识点进行汇总. 1.We def ...

  9. centos安装zabbix集群监控(亲测无坑版)

    一. 安装lemp环境 下载安装包:wget bbs.linuxtone.org/docs/autoinstall/lemp_auto_v1.0.6.tar.gz 包解压:tar zxvf lemp_ ...

随机推荐

  1. [BZOJ1052][HAOI2007]覆盖问题 二分+贪心

    1052: [HAOI2007]覆盖问题 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2053  Solved: 959 [Submit][Sta ...

  2. ASP.NET MVC4 MVC 当前上下文中不存在名称“Scripts”

    Views目录下的web.config文件 <pages>下<namespaces>下 加入<add namespace="System.Web.Optimiz ...

  3. #420 Div2 D

    #420 Div2 D 题意 给出一个方格矩阵,其中存在亮着的方格,只能在亮着的方格上行走,可以在初始亮的方格上花费一枚硬币临时点亮任意一行或一列,地图上同一时间只能存在一个这样的行或列,问走到终点最 ...

  4. 背包【p1858】 多人背包(次优解 or 第k优解)

    题目描述--->p1858 多人背包 分析: 很明显,这题是背包问题的一种变形. 求解 次优解or第k优解. 表示刚开始有点懵,看题解也看不太懂. 又中途去补看了一下背包九讲 然后感觉有些理解, ...

  5. python 传不可变对象 or 可变对象

    可更改(mutable)与不可更改(immutable)对象 在 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象 ...

  6. Lowest Common Ancestor of a Binary Search Tree -- LeetCode

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  7. POJ 2686 Traveling by Stagecoach(状压DP)

    [题目链接] http://poj.org/problem?id=2686 [题目大意] 给出一张无向图,你有n张马车票每张车票可以租用ti匹马, 用一张马车票从一个城市到另一个城市所用的时间为这两个 ...

  8. HDU 2586 How far away? LCA 转化成RMQ

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 [题意] 给出一个N 个和N-1条边的连通图,询问任意两点间的距离.N<=40000 . [分 ...

  9. linux mysql cluser集群

    管理节点的安装与启动 config.init内容如下 [NDBD DEFAULT] NoOfReplicas=1 #定义在Cluster环境中相同数据的份数,最大为4 [NDB_MGMD] #设置管理 ...

  10. 【mybatis】in查询+判断list查询条件是否进行in查询

    mybatis中的in查询: 并且判断in查询的list是否为null或者list有值才进行In查询 <select id="find" parameterType=&quo ...