{"id":674,"date":"2020-04-07T09:46:21","date_gmt":"2020-04-07T01:46:21","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=674"},"modified":"2020-04-07T09:46:22","modified_gmt":"2020-04-07T01:46:22","slug":"zabbix-api-bulk-add-aggregate-graphics","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/","title":{"rendered":"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62"},"content":{"rendered":"\n
#!\/bin\/env python\nimport json\nimport requests\nimport argparse\nimport re\n\nre_digits = re.compile(r'(\\d+)')\n\n\ndef emb_numbers(s):\n pieces = re_digits.split(s)\n pieces[1::2] = map(int, pieces[1::2])\n return pieces\n\n\ndef sort_strings_with_emb_numbers2(alist):\n return sorted(alist, key=emb_numbers)\n\n\n\ndef requestjson(url, values):\n data = json.dumps(values)\n # \u6dfb\u52a0\u5934\u90e8\u4fe1\u606f\n hd = {\n 'Content-Type': 'application\/json-rpc'\n }\n # \u4f7f\u2f64post\u2f45\u5f0f \u6dfb\u52a0\u767b\u5f55\u2f64\u6237\u53ca\u5bc6\u7801\n req = requests.post(url, data=data, headers=hd)\n output = req.json()\n return output\n\n\ndef authenticate(url, username, password):\n values = {'jsonrpc': '2.0',\n 'method': 'user.login',\n 'params': {\n 'user': username,\n 'password': password\n },\n 'id': '0'\n }\n output = requestjson(url, values)\n\n return output['result']\n\n\ndef gethosts(groupname, url, auth):\n host_list = {}\n values = {'jsonrpc': '2.0',\n 'method': 'hostgroup.get',\n 'params': {\n 'output': 'extend',\n 'filter': {'name': groupname},\n 'selectHosts': ['host']\n },\n 'auth': auth,\n 'id': '2'\n }\n output = requestjson(url, values)\n for host in output['result'][0]['hosts']:\n host_list[host['host']] = (host['hostid'])\n\n # return host_list\n hosts_sort = []\n for host in sort_strings_with_emb_numbers2(host_list.keys()):\n hosts_sort.append(host_list[host])\n return hosts_sort\n\n\ndef getgraphs(host_list, name_list, url, auth, columns, graphtype=0, dynamic=0):\n if (graphtype == 0):\n selecttype = ['graphid']\n select = 'selectGraphs'\n if (graphtype == 1):\n selecttype = ['itemid', 'value_type']\n select = 'selectItems'\n\n graphs = []\n for host in host_list:\n values = ({'jsonrpc': '2.0',\n 'method': 'graph.get',\n 'params': {\n select: [selecttype, 'name'],\n 'output': ['graphid', 'name'],\n 'hostids': host,\n 'filter': {'name': name_list},\n 'sortfield': 'name'\n },\n 'auth': auth,\n 'id': '3'\n })\n output = requestjson(url, values)\n bb = sorted(output['result'])\n if (graphtype == 0):\n for i in bb:\n graphs.append(i['graphid'])\n if (graphtype == 1):\n for i in bb:\n if int(i['value_type']) in (0, 3):\n graphs.append(i['itemid'])\n\n graph_list = []\n x = 0\n y = 0\n for graph in graphs:\n graph_list.append({\n 'resourcetype': graphtype,\n 'resourceid': graph,\n 'width': '600',\n 'height': '100',\n 'x': str(x),\n 'y': str(y),\n 'colspan': '1',\n 'rowspan': '1',\n })\n x += 1\n if x == int(columns):\n x = 0\n y += 1\n\n return graph_list\n\n\ndef screencreate(url, auth, screen_name, graphids, columns):\n columns = int(columns)\n if len(graphids) % columns == 0:\n vsize = len(graphids) \/ columns\n else:\n vsize = (len(graphids) \/ columns) + 1\n\n values = {'jsonrpc': '2.0',\n 'method': 'screen.create',\n 'params': [{\n 'name': screen_name,\n 'hsize': columns,\n 'vsize': vsize,\n 'screenitems': []\n }],\n 'auth': auth,\n 'id': 2\n }\n for i in graphids:\n values['params'][0]['screenitems'].append(i)\n output = requestjson(url, values)\n\n\ndef main():\n url = 'http:\/\/172.17.0.4\/api_jsonrpc.php'\n username = 'admin'\n password = 'password'\n auth = authenticate(url, username, password)\n host_list = gethosts(groupname, url, auth)\n graph_ids = getgraphs(host_list, graphname, url, auth, columns)\n screencreate(url, auth, screen_name, graph_ids, columns)\n\n\nif __name__ == '__main__':\n parser = argparse.ArgumentParser()\n parser.add_argument('-g', dest='groupname', nargs='+', metavar='groupname', type=str,help='which group you want to select')\n parser.add_argument('-G', dest='graphname', nargs='+', metavar='graphname', type=str,help='which graph you want to select')\n parser.add_argument('-c', dest='columns', metavar='columns', type=int, help='the screen columns')\n parser.add_argument('-n', dest='screen_name', metavar='screen_name', type=str, help='the screen name')\n args = parser.parse_args()\n\n groupname = args.groupname\n graphname = args.graphname\n columns = args.columns\n screen_name = args.screen_name\n\n main()<\/pre>\n\n\n\n\u793a\u4f8b\uff1a<\/p>\n\n\n\n
python3.6 create_screen.py -g '\u4e0a\u6d77G\u533a Linux' -G 'CPU utilization' -c 2 -n '\u4e0a\u6d77G\u533a Linux---CPU utilization'\npython3.6 create_screen.py -g '\u4e0a\u6d77G\u533a Linux' -G 'Memory usage' -c 2 -n '\u4e0a\u6d77G\u533a Linux---Memory usage' \npython3.6 create_screen_disk.py -g '\u4e0a\u6d77G\u533a Linux' -G '\/: Disk space usage' -c 3 -n '\u4e0a\u6d77G\u533a Linux---\/: Disk space usage' <\/pre>\n\n\n\n
\u5bf9\u4e8e\u4ee5\u4e0a\u547d\u4ee4\u4e2d\u7684\u53c2\u6570\u89e3\u91ca\uff1a<\/code>
-g\u00a0\u00a0\u00a0 \u8981\u663e\u793azabbix\u7684\u7fa4\u7ec4\uff08\u6ce8\u610f\u8fd9\u4e2a\u662f\u5728zabbix\u754c\u9762\u7684\"\u56fe\u5f62\"\u91cc\u663e\u793a\u7684\"\u7fa4\u7ec4\"\u9009\u9879\uff0c\u4e0d\u80fd\u968f\u4fbf\u5199\uff0c\u5fc5\u987b\u662f\u5b58\u5728zabbix\u91cc\u7684\uff09\u3002<\/code>
-G\u00a0\u00a0\u00a0 \u8981\u663e\u793a\u7684zabbix\u56fe\u5f62\uff08\u6ce8\u610f\u8fd9\u4e2a\u662f\u5728zabbix\u754c\u9762\u7684\"\u56fe\u5f62\"\u91cc\u663e\u793a\u7684\"\u56fe\u5f62\"\u9009\u9879\uff0c\u4e0d\u80fd\u968f\u4fbf\u5199\uff0c\u5fc5\u987b\u662f\u5b58\u5728zabbix\u91cc\u7684\uff09\u3002<\/code>
-c\u00a0\u00a0\u00a0 \u663e\u793a\u51e0\u5217\uff0c\u6ce8\u610f\u8981\u8c03\u6574\u811a\u672c\u91cc\u7684\uff1a'width': \uff0c'height':\u00a0 \u53c2\u6570\u6765\u8bbe\u7f6e\u5927\u5c0f\u3002<\/code>
-n\u00a0\u00a0\u00a0 \u5728screen<\/code>\u00a0
\u91cc\u9762\u663e\u793a\u7684\u540d\u79f0\u3002<\/code> <\/p><\/blockquote>\n\n\n\n