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 ...
随机推荐
- 本地yum仓库的搭建
. 1.直接断开网络,模拟生产内网环境 2.将原先的网络yum仓库全部移动到 backup目录下 3.创建本地yum仓库 local_yum.repo vi /etc/yum.repos.d/loc ...
- CentOS下yum安装
centos最小化安装不会装yum,以下是安装方法:(所有操作均在ROOT用户下,系统版本是centos7) 一.删除原有YUM # rpm -aq|grep yum|xargs rpm -e --n ...
- C/C++/C#程序如何打成DLL动态库
C/C++程序如何打成DLL动态库:1.在VS中新建main.h,添加如下内容:extern "C" _declspec(dllexport) int onLoad(); 2.新建 ...
- SQL调优的基本原则
在使用DBMS时经常对系统的性能有非常高的要求:不能占用过多的系统内存和CPU资源.要尽可能快的完成的数据库操作.要有尽可能高的系统吞吐量.如果系统开发出来不能满足要求的所有性能指标,则必须对系统进行 ...
- php的function() use($args)用法
使用use返回 aaa aaa.使用函数传参数aaa bbb. use的参数必须是已经存在的,如果没有定义返回Notice: Undefined variable: word ,使用函数参数方式不需要 ...
- SQL SERVER 内存学习系列
http://www.cnblogs.com/double-K/p/5049417.html http://blog.sina.com.cn/s/blog_5deb2f5301014wti.html ...
- 鼠标悬浮tip 显示
鼠标悬浮tip 显示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- ubifs & mtd
前天晚上在写完另一篇总结之时,赵XX向我咨询了关于mtd 和ubifs的相关内容.而我在这方面只是略懂皮毛,所以向他许愿共同调查这个方面的知识.经过昨天一天的调查,最后感觉是有了一定的经验和基础了,所 ...
- vuejs -- 如何使一个自定义函数在加载时自动执行
- Linux下的权限问题
刚刚经理给我讲了有关权限的问题,后自己也在网上收集整理了下,特记于此. ---------------------------------------------------------------- ...