下面亲测可用

#!/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. xxx.service is not a native service, redirecting to /sbin/chkconfig.

    centos7编译安装的程序无法用systemctl 启动 [root@nagios-server check_commands]# systemctl enable npcd.service npc ...

  2. 计蒜客 28319.Interesting Integers-类似斐波那契数列-递推思维题 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 I)

    I. Interesting Integers 传送门 应该是叫思维题吧,反正敲一下脑壳才知道自己哪里写错了.要敢于暴力. 这个题的题意就是给你一个数,让你逆推出递推的最开始的两个数(假设一开始的两个 ...

  3. (12)oracle事务

    事物 http://www.cnblogs.com/linjiqin/archive/2012/02/06/2340637.htm 在当前的事务中设置保存点 savepoint  名字; 保存点回滚 ...

  4. [Math Review] Statistics Basic: Estimation

    Two Types of Estimation One of the major applications of statistics is estimating population paramet ...

  5. [JSOI2017]原力

    题目大意: 一个$n(n\le5\times10^4)$个点,$m(m\le10^5)$条边的无向图.每条边有一个边权$w_i(w_i\le10^6)$和一个附加属性$t_i(t_i\in\{R,G, ...

  6. SQL PARTITION BY:列值改变时重置计数

    现有数据如下: 需求:以科目为单位 对分数进行排序 SELECT *, ROW_NUMBER() OVER (PARTITION BY 科目 ORDER BY 分数 DESC) AS NUM FROM ...

  7. P2P通信标准协议(三)之ICE

    在P2P通信标准协议(二)中,介绍了TURN的基本交互流程,在上篇结束部分也有说到,TURN作为STUN 协议的一个拓展,保持了STUN的工具性质,而不作为完整的NAT传输解决方案,只提供穿透NAT的 ...

  8. UBIFS分区制作及UBIFS烧写和启动

    参考 http://blog.csdn.net/chongzi865458/article/details/6799258 ubiattach version 1.0 - a tool to atta ...

  9. 【报错】spring boot启动 报错 找不到实体类Not a managed type: class com.pisen.cloud.luna.feign.ten.beans.SysUser

    Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.pisen.cloud.luna.feign. ...

  10. httpanalyzer 抓包时会更换证书

    今天是要httpanalyzer时发现,在启用的时候,如果当前网络地址是https的话,那么当前证书会被更换掉,效果如下: 而原来的证书如下: 所以,请注意,如果你的请求有严格的证书验证,那么证书验证 ...