{"id":995,"date":"2025-03-21T10:04:00","date_gmt":"2025-03-21T01:04:00","guid":{"rendered":"https:\/\/roroing.net\/?p=995"},"modified":"2025-03-20T22:18:29","modified_gmt":"2025-03-20T13:18:29","slug":"zabbix7-2%e3%81%ab%e3%81%a6api%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b%e3%81%9f%e3%82%81%e3%81%ae%e4%be%8b","status":"publish","type":"post","link":"https:\/\/roroing.net\/?p=995","title":{"rendered":"zabbix7.2\u306b\u3066API\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306e\u4f8b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u3053\u308c\u307e\u3067\u306eapi\u306e\u53d6\u5f97\u65b9\u6cd5\u3060\u3068api\u306eauth\u8a8d\u8a3c\u306e\u554f\u984c\u306b\u3088\u308a\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3002\nchatGPT\u3068\u7c98\u3063\u3066\u4f1a\u8a71\u3057\u305f\u3068\u3053\u308d\u4ee5\u4e0b\u306e\u65b9\u6cd5\u306b\u3066\u6210\u529f\u3057\u305f\u306e\u3067\u3001\u30e1\u30e2\u3092\u3059\u308b\npython3\u3000\u30b3\u30de\u30f3\u30c9\u306b\u3066\u5b9f\u884c\u53ef\u80fd\n\nhosts_get.py\n\nimport requests\nimport json\n\nurl = \"https:\/\/yourdomain\/zabbix\/api_jsonrpc.php\"\nheaders = {\n    'Authorization': 'Bearer apitoken',\n    'Content-Type': 'application\/json-rpc'\n}\n\n# host.get \u30ea\u30af\u30a8\u30b9\u30c8\ndef get_hosts():\n    payload = {\n        \"jsonrpc\": \"2.0\",\n        \"method\": \"host.get\",\n        \"params\": {\n            \"output\": &#91;\"hostid\", \"host\", \"name\"],\n            \"sortfield\": \"name\"  # \u540d\u524d\u9806\u306b\u30bd\u30fc\u30c8\n        },\n        \"id\": 1\n    }\n\n    # \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\n    response = requests.post(url, headers=headers, data=json.dumps(payload))\n    data = response.json()\n\n    # \u7d50\u679c\u3092\u898b\u3084\u3059\u304f\u8868\u793a\n    if 'result' in data:\n        with open(\"hosts.txt\", \"w\") as file:\n            for host in data&#91;'result']:\n                line = f\"Host ID: {host&#91;'hostid']}, Host: {host&#91;'host']}, Name: {host&#91;'name']}\\n\"\n                print(line.strip())  # \u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u3082\u51fa\u529b\n                file.write(line)  # \u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u307f\n        print(\"\u2705 \u30db\u30b9\u30c8\u4e00\u89a7\u3092 'hosts.txt' \u306b\u51fa\u529b\u3057\u307e\u3057\u305f\u3002\")\n    else:\n        print(f\"Error: {data.get('error', 'Unknown error')}\")\n\n# \u5b9f\u884c\nif __name__ == \"__main__\":\n    get_hosts()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[],"class_list":{"0":"post-995","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-zabbix","7":"h-entry","8":"hentry","9":"h-as-article"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/roroing.net\/index.php?rest_route=\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/roroing.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/roroing.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/roroing.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/roroing.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=995"}],"version-history":[{"count":1,"href":"https:\/\/roroing.net\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions"}],"predecessor-version":[{"id":996,"href":"https:\/\/roroing.net\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions\/996"}],"wp:attachment":[{"href":"https:\/\/roroing.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/roroing.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/roroing.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}