Python script to create Screen from all Items/Graphs of a host
#!/usr/bin/env python
import urllib2
import json
import argparse def authenticate(url, username, password):
values = {'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': username,
'password': password
},
'id': ''
} data = json.dumps(values)
req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
response = urllib2.urlopen(req, data)
output = json.loads(response.read()) try:
message = output['result']
except:
message = output['error']['data']
print message
quit() return output['result'] def getGraph(hostname, url, auth, graphtype, dynamic, columns):
if (graphtype == 0):
selecttype = ['graphid']
select = 'selectGraphs'
if (graphtype == 1):
selecttype = ['itemid', 'value_type']
select = 'selectItems' values = {'jsonrpc': '2.0',
'method': 'host.get',
'params': {
select: selecttype,
'output': ['hostid', 'host'],
'searchByAny': 1,
'filter': {
'host': hostname
}
},
'auth': auth,
'id': ''
} data = json.dumps(values)
req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
response = urllib2.urlopen(req, data)
host_get = response.read() output = json.loads(host_get)
# print json.dumps(output) graphs = []
if (graphtype == 0):
for i in output['result'][0]['graphs']:
graphs.append(i['graphid']) if (graphtype == 1):
for i in output['result'][0]['items']:
if int(i['value_type']) in (0, 3):
graphs.append(i['itemid']) graph_list = []
x = 0
y = 0 for graph in graphs:
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
if x == columns:
x = 0
y += 1 return graph_list def screenCreate(url, auth, screen_name, graphids, columns):
# print graphids
if len(graphids) % columns == 0:
vsize = len(graphids) / columns
else:
vsize = (len(graphids) / columns) + 1 values = {"jsonrpc": "2.0",
"method": "screen.create",
"params": [{
"name": screen_name,
"hsize": columns,
"vsize": vsize,
"screenitems": []
}],
"auth": auth,
"id": 2
} for i in graphids:
values['params'][0]['screenitems'].append(i) data = json.dumps(values)
req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
response = urllib2.urlopen(req, data)
host_get = response.read() output = json.loads(host_get) try:
message = output['result']
except:
message = output['error']['data'] print json.dumps(message) def main():
url = 'http://<zabbix url>/zabbix/api_jsonrpc.php'
username = "Your API Users Username"
password = "Your API Users Username" parser = argparse.ArgumentParser(description='Create Zabbix screen from all of a host Items or Graphs.')
parser.add_argument('hostname', metavar='H', type=str,
help='Zabbix Host to create screen from')
parser.add_argument('screenname', metavar='N', 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, default=3,
help='number of columns in the screen (default: 3)')
parser.add_argument('-d', dest='dynamic', action='store_true',
help='enable for dynamic screen items (default: disabled)')
parser.add_argument('-t', dest='screentype', action='store_true',
help='set to 1 if you want to create only simple graphs of items, no previously defined graphs will be added to screen (default 0)') args = parser.parse_args()
hostname = args.hostname
screen_name = args.screenname
columns = args.columns
dynamic = (1 if args.dynamic else 0)
screentype = (1 if args.screentype else 0) auth = authenticate(url, username, password)
graphids = getGraph(hostname, url, auth, screentype, dynamic, columns) print "Screen Name: " + screen_name
print "Total Number of Graphs: " + str(len(graphids)) screenCreate(url, auth, screen_name, graphids, columns) if __name__ == '__main__':
main()
转载,调用zabbix接口画screen
Python script to create Screen from all Items/Graphs of a host的更多相关文章
- java + spring (jython\python\script) Error:SyntaxError: no viable alternative at character '\n'
使用Jython结合java和Python开发功能时,要是遇到如下情况: 2016-03-10 16:16:49 DEBUG [com.freedom.orion.configs.JyhtonConf ...
- Windows 配置Apache以便在浏览器中运行Python script的CGI模式
打开httpd.conf,找到”#ScriptInterpreterSource Registry “,移除前面的注释# (如果找不到这行,就自己添加进去) 找到“Options Indexes Fo ...
- Notepad++插件Emmet和Python Script的安装
最近在做一个项目,涉及到大量的HTML.CSS代码的编写,手动写代码效率实在 是低下.于是想搜索一下,有没有Notepad++插件可以支持自动生成的,果不其然还真有.Emmet,这款神器其实就是 Ze ...
- rc.local 注意事項,call python script, file position
如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...
- [Python] Execute a Python Script
Python scripts can be executed by passing the script name to the python command or created as execut ...
- Excel vba call Python script on Mac
How can I launch an external python process from Excel 365 VBA on OSX? It took me a while, but I fig ...
- A python script to check NE syncfail and get log from CIPS
#! /usr/bin/env python # -*- coding: UTF-8 -*- """The script is to check whether NE i ...
- [译]Why do people write #!/usr/bin/env python on the first line of a Python script?
If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is t ...
- My first python script for work
I write it yesterday to watch the NE process(rcpfd,cfgd) automatically, then i will write a window t ...
随机推荐
- ECMAScript和JavaScript的关系
JavaScript 是一种基于 ECMAScript 规范的脚本语言,并在此基础上进行了自己的封装.ECMAScript 不是一种编程语言,仅仅是一种脚本语言规范,由欧洲计算机协会制定和发布,任何基 ...
- C#中使用7Z进行压缩解压
SevenZipSharp相关文档下载地址: http://sevenzipsharp.codeplex.com/releases/view/51254 1. 解决方案中添加引用:SevenZipSh ...
- 51nod 1433 0和5【数论/九余定理】
1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 小K手中有n张牌,每张牌上有一个一位数的数,这个 ...
- UVA 10129 Play on Words (欧拉通路)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398627.html 题意: 输入N(N <= 100000)个单词,是否可以把所有这些单词排成一个序列,使得每个单 ...
- java标识符与命名规则
标识符就是给变量.类或方法起的名字.可以用字母.下划线或美元符号开头,区分大小写,没有最大长度限制.(关键字除外) 关键字 访问控制 private protected public ...
- POJ2796 Feel Good(单调栈)
题意:给一个非负整数序列,求哪一段区间的权值最大,区间的权值=区间所有数的和×区间最小的数. 用单调非递减栈在O(n)计算出序列每个数作为最小值能向左和向右延伸到的位置,然后O(n)枚举每个数利用前缀 ...
- 分金币 Uva 11300
题意 给定N个人成环状坐,每个人初始分配Ai的金币,金币总数可以被N整除,每个人可以给左右相邻的人一定数量的金币使得最终每个人的金币数量相同,求转移数量最小的方案所转移的总金币数量. N<=10 ...
- extjs grid合并单元格
http://blog.csdn.net/kunoy/article/details/7829395 /** * Kunoy * 合并单元格 * @param {} grid 要合并单元格的grid对 ...
- Spring Boot中使用Feign调用时Hystrix提示异常:"could not be queued for execution and no fallback available."以及"Rejected command because thread-pool queueSize is at rejection threshold"
说明: 1.我还没有真正理解Spring Cloud的精髓,现只停留在使用阶段,可能存在分析不到位的问题. 1.这个是由于线程池的最大数量导致的,官方说随着线程池的数量越大,资源开销也就越大,所以调整 ...
- delphi 设置开机自动启动函数
有些程序要设置为开机启动,所以自己写了个函数方便以后使用,供大家参考 procedure TMainForm.SetAutoRun(ok: boolean); var Reg:TRegistry; ...