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 ...
随机推荐
- bottle框架学习(1):命令行
在最初的一段代码,内容如下: if __name__ == '__main__': from optparse import OptionParser _cmd_parser = OptionPars ...
- rgmanager 介绍
版本: rgmanager-2.0.52-14.el6.x86_64 服务: /etc/init.d/rgmanager 配置文件: /etc/cluster/cluster.conf 日志文件: 相 ...
- 第一章:1-22、长度为100字节的应用层数据交给运输层传送,需加上20字节的TCP首部。再交给网络层传送,需加上20字节的IP首部。最后交给数据链路层的以太网传送,加上首部和尾部18字节。试求数据的传输效率。 若应用层数据长度为1000字节,数据的传输效率是多少?
<计算机网络>谢希仁著第四版课后习题答案答: 数据长度为100字节时 传输效率=100/(100+20+20+18)=63.3% 数据长度为1000字节时, 传输效率=1000/(1000 ...
- easyui常用
清空列表选中项 //easyui datagrid 清空勾选 $('#List').datagrid('clearSelections'); 扩展修改查询方法 function QueryList() ...
- .net 实用功能
c#的DateTime.Now函数详解 [C#]Color颜色对照表 Sql日期时间格式转换 关于asp.net中页面事件加载的先后顺序 asp.net在ie7中使用FileUpload上传前预览图片 ...
- StringBuffer 清空
几种方法: 方法1: 1 2 3 4 StringBuffer my_StringBuffer = new StringBuffer(); my_StringBuffer.append('hellow ...
- TestList汇总
[基本类型] 类的构造方法调用问题 重载复写的区别 Final finally finalized 的区别 定义final变量是否需要初始化 forward和redirect的区别 equals方法和 ...
- lua中self.__index = self是什么意思?
example1 Account = {balance = 0} function Account:new(conf) conf = conf or {} setmetatable(conf,self ...
- SQL表操作习题4 14~25题 缺20题
- 在java代码中设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...