{"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

\u539f\u6587\uff1a https:\/\/www.cnblogs.com\/kevingrace\/p\/8129943.html<\/a>
\u539f\u6587\u7528\u7684 urllib2 \u6211\u8fd9\u5b89\u88c5\u4e0d\u4e0a\uff0c\u7b80\u5355\u505a\u4e86\u4fee\u6539\u7528\u7684requests\u3002 <\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"

#!\/bin\/env python import json import requests import ar<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13,33],"tags":[],"yoast_head":"\nZabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"og:description\" content=\"#!\/bin\/env python import json import requests import ar\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-07T01:46:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-07T01:46:22+00:00\" \/>\n<meta name=\"author\" content=\"\u7ba1\u7406\u5458\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\"},\"author\":{\"name\":\"\u7ba1\u7406\u5458\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"headline\":\"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62\",\"datePublished\":\"2020-04-07T01:46:21+00:00\",\"dateModified\":\"2020-04-07T01:46:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\"},\"wordCount\":11,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"articleSection\":[\"Linux\",\"Python\",\"zabbix\"],\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\",\"name\":\"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\"},\"datePublished\":\"2020-04-07T01:46:21+00:00\",\"dateModified\":\"2020-04-07T01:46:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.linuxdevops.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/www.linuxdevops.cn\/linux\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\",\"url\":\"https:\/\/www.linuxdevops.cn\/\",\"name\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"description\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0\",\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"alternateName\":\"linuxdevops\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.linuxdevops.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\",\"name\":\"\u7ba1\u7406\u5458\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-CN\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"contentUrl\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"width\":512,\"height\":512,\"caption\":\"\u7ba1\u7406\u5458\"},\"logo\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\"},\"description\":\"\u7ba1\u7406\u5458\",\"url\":\"https:\/\/www.linuxdevops.cn\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/","og_locale":"zh_CN","og_type":"article","og_title":"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","og_description":"#!\/bin\/env python import json import requests import ar","og_url":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/","og_site_name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","article_published_time":"2020-04-07T01:46:21+00:00","article_modified_time":"2020-04-07T01:46:22+00:00","author":"\u7ba1\u7406\u5458","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#article","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/"},"author":{"name":"\u7ba1\u7406\u5458","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"headline":"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62","datePublished":"2020-04-07T01:46:21+00:00","dateModified":"2020-04-07T01:46:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/"},"wordCount":11,"commentCount":0,"publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"articleSection":["Linux","Python","zabbix"],"inLanguage":"zh-CN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/","url":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/","name":"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/#website"},"datePublished":"2020-04-07T01:46:21+00:00","dateModified":"2020-04-07T01:46:22+00:00","breadcrumb":{"@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-api-bulk-add-aggregate-graphics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.linuxdevops.cn\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/www.linuxdevops.cn\/linux\/"},{"@type":"ListItem","position":3,"name":"Zabbix API\u6279\u91cf\u6dfb\u52a0\u805a\u5408\u56fe\u5f62"}]},{"@type":"WebSite","@id":"https:\/\/www.linuxdevops.cn\/#website","url":"https:\/\/www.linuxdevops.cn\/","name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","description":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0","publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"alternateName":"linuxdevops","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.linuxdevops.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":["Person","Organization"],"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1","name":"\u7ba1\u7406\u5458","image":{"@type":"ImageObject","inLanguage":"zh-CN","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/","url":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","contentUrl":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","width":512,"height":512,"caption":"\u7ba1\u7406\u5458"},"logo":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/"},"description":"\u7ba1\u7406\u5458","url":"https:\/\/www.linuxdevops.cn\/author\/root\/"}]}},"_links":{"self":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/674"}],"collection":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":1,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":675,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/674\/revisions\/675"}],"wp:attachment":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}