{"id":714,"date":"2022-09-28T22:44:20","date_gmt":"2022-09-28T14:44:20","guid":{"rendered":"https:\/\/frogvps.com\/?p=714"},"modified":"2022-10-10T16:55:27","modified_gmt":"2022-10-10T08:55:27","slug":"zabbix%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%ef%bc%8c%e4%bd%bf%e7%94%a8api%e6%89%b9%e9%87%8f%e6%b7%bb%e5%8a%a0%e4%b8%bb%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/frogvps.com\/?p=714","title":{"rendered":"zabbix\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u4f7f\u7528API\u6279\u91cf\u6dfb\u52a0\u4e3b\u673a"},"content":{"rendered":"\n<p>\u901a\u8fc7API\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u6216\u8005\u811a\u672c\u76f4\u63a5\u5bf9host\u4e3b\u673a\u8fdb\u884c\u589e\u5220\u6539\u67e5\uff0c\u4e0d\u9700\u8981\u5728web\u7aef\u624b\u52a8\u64cd\u4f5c\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u8fd0\u7ef4\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\u53c2\u8003zabbix\u7684\u5b98\u65b9\u6587\u6863<\/p>\n\n\n\n<p><a rel=\"nofollow\" href=\"https:\/\/www.zabbix.com\/documentation\/4.0\/zh\/manual\/api\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.zabbix.com\/documentation\/4.0\/zh\/manual\/api<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">API\u4f7f\u7528\u57fa\u7840<\/h2>\n\n\n\n<p><strong>\u7528curl\u547d\u4ee4\u76f4\u63a5\u83b7\u53d6token<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#http \u662fzabbix server\u7684IP\u5730\u5740\n#user \u548c password \u662f\u767b\u5f55zabbix web\u7aef\u7684\u8d26\u53f7\u5bc6\u7801\n#token \u5982\u679c\u5931\u6548\u53ef\u4ee5\u91cd\u65b0\u83b7\u53d6\n\ncurl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"user.login\",\n\"params\": {\n\"user\": \"Admin\",\n\"password\": \"xiaoqinwa\"\n},\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool<\/code><\/pre>\n\n\n\n<p><strong>\u7528python3\u811a\u672c\u83b7\u53d6token<\/strong><\/p>\n\n\n\n<p>\u7f16\u8f91token1.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n#Author: frogvps.com\n#centos7 \u7528python3\u7684\u65b9\u6cd5\n#\u5b89\u88c5epel yum install epel-release -y\n#\u5b89\u88c5python3 yum - y install python3\n#\u5b89\u88c5pip yum -y install python3-pip\n#\u8f6f\u94fe\u63a5 ln -s \/usr\/bin\/pip3.6 \/usr\/bin\/pip\n#\u5b89\u88c5requests pip install requests\n\nimport requests\nimport json\nurl = 'http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php'\npost_data = {\n\"jsonrpc\": \"2.0\",\n\"method\": \"user.login\",\n\"params\": {\n\"user\": \"Admin\",\n\"password\": \"xiaoqinwa\"\n},\n\"id\": 1\n}\npost_header = {'Content-Type': 'application\/json'}\nret = requests.post(url, data=json.dumps(post_data), headers=post_header)\nzabbix_ret = json.loads(ret.text)\nif not zabbix_ret.__contains__('result'):\n  print ('login error')\nelse:\n  print (zabbix_ret.get('result')) <\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p><em>\u6ce8\uff1a\u4e0b\u9762\u6240\u6709json\u6570\u636e\u4e2d\u7684auth\u540e\u9762\u5185\u5bb9\u66ff\u6362\u6210\u4f60\u83b7\u53d6\u5230\u7684token<\/em><\/p>\n\n\n\n<p><strong>\u67e5\u770b\u6307\u5b9a\u4e3b\u673a\u4fe1\u606f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#host \u5c31\u662fzabbix agent\u914d\u7f6e\u6587\u4ef6\u91cc\u7684hostname\n\ncurl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"host.get\",\n\"params\": {\n\"filter\": {\n\"host\": &#91;\n\"154.17.29.27\",\n\"frog-proxy\"\n]\n}\n},\n\"auth\": \"67335cf7fec3c6f871dcc63b97916ff3\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p><strong>\u83b7\u53d6\u6240\u6709\u4e3b\u673a\u5217\u8868<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"host.get\",\n\"params\": {\n\"output\": &#91;\"host\"]\n},\n\"auth\": \"67335cf7fec3c6f871dcc63b97916ff3\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p><strong>\u83b7\u53d6\u6240\u6709\u7528\u6237<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"user.get\",\n\"params\": {\n\"output\": \"extend\"\n},\n\"auth\": \"67335cf7fec3c6f871dcc63b97916ff3\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p><strong>\u83b7\u53d6\u5355\u4e2a\u6a21\u677f\u4fe1\u606f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#host \u5199\u6a21\u677f\u540d\u79f0\n\ncurl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"template.get\",\n\"params\": {\n\"output\": \"extend\",\n\"filter\": {\n\"host\": &#91;\n\"Template OS Linux by Zabbix agent active\"\n]\n}\n},\n\"auth\": \"67335cf7fec3c6f871dcc63b97916ff3\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python -m json.tool<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p><strong>\u901a\u8fc7API\u6dfb\u52a0\u4e3b\u673a\uff0c\u6ca1\u6709\u8bbe\u7f6eproxy<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#API\u6dfb\u52a0\u4e3b\u673a\u4e3a\u9884\u5148\u77e5\u9053\u8981\u6dfb\u52a0\u7684\u4e3b\u673aIP\u3001\u9884\u5148\u5b89\u88c5\u5e76\u914d\u7f6e\u597dzabbix agent\u3001\u9884\u5148\u77e5\u9053\u8981\u5173\u8054\u7684\u6a21\u677fID\/\u7ec4ID\u7b49\u4fe1\u606f\uff0c\u7136\u540e\u540cAPI\u63d0\u4ea4\u8bf7\u6c42\u6dfb\u52a0\n#host \u81ea\u5b9a\u4e49\u6dfb\u52a0\u540e\u7684agent\u7684\u540d\u79f0,\u5fc5\u987b\u548cagent\u914d\u7f6e\u6587\u4ef6\u91cc\u7684hostname\u4e00\u81f4\n#type \u7c7b\u578b\u4e3a1\u8868\u793aagent\uff0c2\u662fSNMP\uff0c3\u662fIMPI,4\u662fJMX\n#useip #0\u662f\u4f7f\u7528DNS\uff0c1\u662f\u4f7f\u7528IP\u5730\u5740\n#ip \u6dfb\u52a0\u7684zabbix agent\u7684IP\u5730\u5740\n#port agent\u7aef\u53e3\n#groupid \u6dfb\u52a0\u5230\u7684\u7ec4\u7684ID\n#templateid \u5173\u8054\u7684\u6a21\u677f\u7684ID\n\ncurl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"host.create\",\n\"params\": {\n\"host\": \"API Add Host Test\", \n\"interfaces\": &#91;\n{\n\"type\": 1, \n\"main\": 1, \n\"useip\": 1, \n\"ip\": \"146.56.143.177\", \n\"dns\": \"\",\n\"port\": \"10050\"\n}\n],\n\"groups\": &#91;\n{\n\"groupid\": \"18\" \n}\n],\n\"templates\": &#91;\n{\n\"templateid\": \"10284\"\n}\n]\n},\n\"auth\": \"47c9c7bccc24003ad16899c88ae9696c\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"116\" src=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a1-1024x116.jpg\" alt=\"\" class=\"wp-image-718\" srcset=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a1-1024x116.jpg 1024w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a1-300x34.jpg 300w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a1-768x87.jpg 768w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a1-1536x174.jpg 1536w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a1-2048x232.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u53bbweb\u7aef\u9a8c\u8bc1\u662f\u5426\u6dfb\u52a0\u6210\u529f<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p><strong>\u901a\u8fc7API\u6dfb\u52a0\u4e3b\u673a-zabbix proxy\u6a21\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#proxy_hostid zabbix proxy\u7684ID\n\ncurl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"host.create\",\n\"params\": {\n\"host\": \"API Add Host Test\", \n\"proxy_hostid\": \"10441\",\n\"interfaces\": &#91;\n{\n\"type\": 1, \n\"main\": 1, \n\"useip\": 1, \n\"ip\": \"146.56.143.177\", \n\"dns\": \"\",\n\"port\": \"10050\"\n}\n],\n\"groups\": &#91;\n{\n\"groupid\": \"18\" \n}\n],\n\"templates\": &#91;\n{\n\"templateid\": \"10284\"\n}\n]\n},\n\"auth\": \"47c9c7bccc24003ad16899c88ae9696c\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u67e5\u770b\u6a21\u677fID\/\u7ec4ID\/\u4e3b\u673aID<\/h2>\n\n\n\n<p>\u4f8b\u5982zabbix5.0\u7684\u56fe\u5f62\u754c\u9762\uff0c\u5728web\u7aef\u70b9\u51fb\u8bbe\u65bd\u2014\u2014\u6a21\u677f\u2014\u2014\u627e\u5230\u5e76\u70b9\u51fb\u4f60\u60f3\u8981\u7684\u6a21\u677f\u540d\u79f0\uff0c\u6bd4\u5982Template OS Linux by Zabbix agent active\u3002\u70b9\u8fdb\u53bb\uff0c\u5728\u6d4f\u89c8\u5668\u7684\u5730\u5740\u680f\u4e0a\u65b9\u53ef\u4ee5\u770b\u5230 &#8220;templateid=10284&#8221; \uff0c\u8fd9\u4e2a\u503c\u5c31\u662f\u6a21\u677fID\u3002\u5176\u4ed6\u7684ID\u83b7\u53d6\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u6211\u5c31\u4e0d\u591a\u8bf4\u660e\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a2-1024x502.jpg\" alt=\"\" class=\"wp-image-716\" srcset=\"https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a2-1024x502.jpg 1024w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a2-300x147.jpg 300w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a2-768x376.jpg 768w, https:\/\/frogvps.com\/wp-content\/uploads\/2022\/09\/20220928a2.jpg 1532w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">zabbix API\u6279\u91cf\u6dfb\u52a0\u4e3b\u673a<\/h2>\n\n\n\n<p>\u5728\u539f\u5148\u7684curl\u547d\u4ee4\u57fa\u7840\u4e0a\u52a0\u4e00\u4e2afor\u5faa\u73af\uff0c\u53ef\u4ee5\u901a\u8fc7shell\u811a\u672c\u7684\u65b9\u6cd5\u6279\u91cf\u6dfb\u52a0\u591a\u53f0\u4e3b\u673a\uff0c\u56db\u4e94\u767e\u53f0\u670d\u52a1\u5668\u53ea\u9700\u8981\u51e0\u5206\u949f\u5c31\u80fd\u81ea\u52a8\u6dfb\u52a0\uff0c\u8282\u7701\u4e86\u8fd0\u7ef4\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u53c2\u8003\u811a\u672c addhost.sh<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n#AUTHOR: \u5c0f\u9752\u86d9 frogvps.com\n#\u628a\u6240\u6709\u8981\u6dfb\u52a0\u7684\u4e3b\u673a\u5730\u5740\u8d4b\u503c\u7ed9IP\u53d8\u91cf\n#\u6ce8\u610f\uff0ccurl\u63d0\u4ea4json\u6570\u636e\u4e2d\u5982\u679c\u5f15\u7528shell\u53d8\u91cf\u8981\u4f7f\u7528\u5355\u5f15\u53f7\n\nIP=\"\n146.56.143.177\n152.70.233.16\n\"\nfor i in ${IP};do\ncurl -s -X POST -H 'Content-Type:application\/json' -d '\n{\n    \"jsonrpc\": \"2.0\",\n    \"method\": \"host.create\",\n    \"params\": {\n      \"host\": \"'${i}'\", \n      \"proxy_hostid\": \"10441\",\n      \"interfaces\": &#91;\n         {\n            \"type\": 1, \n            \"main\": 1, \n            \"useip\": 1, \n            \"ip\": \"146.56.143.177\", \n            \"dns\": \"\",\n            \"port\": \"10050\"\n         }\n       ],\n\"groups\": &#91;\n   {\n  \"groupid\": \"18\" \n   }\n],\n\"templates\": &#91;\n  {\n\"templateid\": \"10284\"\n  }\n]\n},\n\"auth\": \"47c9c7bccc24003ad16899c88ae9696c\",\n\"id\": 1\n}' http:\/\/175.24.227.179\/zabbix\/api_jsonrpc.php | python3 -m json.tool\ndone<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7API\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u6216\u8005\u811a\u672c\u76f4\u63a5\u5bf9host\u4e3b\u673a\u8fdb\u884c\u589e\u5220\u6539\u67e5\uff0c\u4e0d\u9700\u8981\u5728web\u7aef\u624b\u52a8\u64cd\u4f5c\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u8fd0\u7ef4\u3002 \u4f7f\u7528\u65b9\u6cd5\u53c2\u8003zabbix\u7684\u5b98\u65b9\u6587\u6863 https:\/\/www.zabbix.com\/documentation\/4.0\/zh\/manu&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,47],"tags":[48],"topic":[],"class_list":["post-714","post","type-post","status-publish","format-standard","hentry","category-zabbix","category-47","tag-zabbix"],"_links":{"self":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=714"}],"version-history":[{"count":3,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions"}],"predecessor-version":[{"id":720,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions\/720"}],"wp:attachment":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=714"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}