#!/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的更多相关文章

  1. 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 ...

  2. Windows 配置Apache以便在浏览器中运行Python script的CGI模式

    打开httpd.conf,找到”#ScriptInterpreterSource Registry “,移除前面的注释# (如果找不到这行,就自己添加进去) 找到“Options Indexes Fo ...

  3. Notepad++插件Emmet和Python Script的安装

    最近在做一个项目,涉及到大量的HTML.CSS代码的编写,手动写代码效率实在 是低下.于是想搜索一下,有没有Notepad++插件可以支持自动生成的,果不其然还真有.Emmet,这款神器其实就是 Ze ...

  4. rc.local 注意事項,call python script, file position

    如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...

  5. [Python] Execute a Python Script

    Python scripts can be executed by passing the script name to the python command or created as execut ...

  6. 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 ...

  7. 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 ...

  8. [译]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 ...

  9. 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 ...

随机推荐

  1. 武汉天喻的NFS 磁盘问题

    public void AsyncPaper() { while (true) { try { var jsonText = RedisHelper.BlockPopItemFromList(&quo ...

  2. Codeforces 798 B. Mike and strings-String的find()函数

    好久好久好久之前的一个题,今天翻cf,发现这个题没过,补一下. B. Mike and strings time limit per test 2 seconds memory limit per t ...

  3. Spfa【P1813】拯救小tim_NOI导刊2011提高(02)

    Description 小tim在游乐场,有一天终于逃了出来!但是不小心又被游乐场的工作人员发现了„„所以你的任务是安全地把小tim护送回家.但是,A市复杂的交通状况给你出了一大难题. A市一共有n个 ...

  4. Spoj SUBST1 New Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  5. Nginx的server为0.0.0.0/0.0.0.1的作用?

    看到kong默认的代理和后台server 都是0.0.0.0,代理到上游的服务器proxy_pass $upstream_scheme://kong_upstream;配置如下, upstream k ...

  6. selenium清空元素时,.clear不执行

    应该是由于鼠标焦点没有定位到相应元素 driver.find_element_by_xpath('//input[@type="password"]').click() drive ...

  7. 数据库学习--wildfly配置postgreSQL数据源

    前言 实验室最近在做一个物品管理系统的小项目,其中涉及到postgreSQL的使用,刚开始部署到wildfly服务器上时遇到了若干问题,终于在导师的讲解下大体上明白了 ,特此记录分享学习一下. 配置数 ...

  8. vim常用操作之复制剪切粘贴,注释取消注释,多行缩进等

    进入vim按下v键,选择要操作的对象 按下y复制,d剪切,p粘贴 >缩进,<取消缩进 按下esc退出操作 按下ctrl+v,选择要操作对象, 按下大写I,写入注释符号,按下d取消注释 按下 ...

  9. 使用nvDXT.exe把图片转换成dds图片【转】

    从nvidia官网下载工具包DDS Utilities [https://developer.nvidia.com/legacy-texture-tools] 转换图片格式需要的工具是 nvdxt.e ...

  10. hive 行转列,列转行

    行转列: concat_ws 列转行: explode