{"id":1818,"date":"2020-10-28T21:53:14","date_gmt":"2020-10-28T12:53:14","guid":{"rendered":"https:\/\/esoro.jp\/?p=1818"},"modified":"2020-10-28T23:03:26","modified_gmt":"2020-10-28T14:03:26","slug":"springboot%e3%81%a7zabbixapi%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/esoro.jp\/?p=1818","title":{"rendered":"SpringBoot\u3067ZabbixAPI\u3092\u4f7f\u3063\u3066\u307f\u308b"},"content":{"rendered":"<p>\u3000Zabbix\u306e\u4f5c\u696d\u3092\u81ea\u52d5\u5316\u3057\u305f\u3044\u3068\u3044\u3046\u4e8b\u3067\u3001ZabbixAPI\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002\u5bfe\u8c61\u306eZabbix\u306f3.2\u3068\u3061\u3087\u3063\u3068\u53e4\u3081\u306e\u3088\u3046\u3067\u3059\u304c\u3001API\u81ea\u4f53\u306f\u3042\u307e\u308a\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u6c17\u306b\u3057\u306a\u304f\u3066\u3088\u3055\u305d\u3046\u3067\u3057\u305f\u3002<a href=\"https:\/\/www.zabbix.com\/documentation\/3.2\/manual\/api\" rel=\"noopener noreferrer\" target=\"_blank\">\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u304c\u5145\u5b9f\u3057\u3066\u3044\u308b\u306e\u3067\u52a9\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>\u8a8d\u8a3c\u5f8c\u3001\uff12\u4ef6\u306e\u30db\u30b9\u30c8\u3092\u6307\u5b9a\u3057\u3066\u30db\u30b9\u30c8\u60c5\u5831\u3092\u53d6\u3063\u3066\u304f\u308b\u30c6\u30b9\u30c8\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"zabbixapiTest.java\" >\t@Test\r\n\tpublic void testZabbixAPI() throws Exception {\r\n\t\tString zabbixUser = \"zabbixuser\";\r\n\t\tString zabbixPass = \"****\";\r\n\t\tString zabbixEndpoint = \"http:\/\/zabbixserver\/zabbix\/api_jsonrpc.php\";\r\n\r\n\t\t\/\/\u8a8d\u8a3c\r\n\t\tJSONObject json = new JSONObject();\r\n\t\tjson.put(\"user\", zabbixUser);\r\n\t\tjson.put(\"password\", zabbixPass);\r\n\t\tjson.put(\"userData\", \"false\");\r\n\t\t\r\n\t\tString token = null;\r\n\t\tResponseEntity&lt;String&gt; response = Request(\"user.login\", json, zabbixEndpoint, token);\r\n\t\t\r\n\t    JsonNode root = new ObjectMapper().readTree(response.getBody());\r\n\t    \r\n\t    token = root.get(\"result\").get(\"sessionid\").asText();\r\n\r\n\t\t\/\/\u30db\u30b9\u30c8\u60c5\u5831\u53d6\u5f97\r\n\t\tJSONObject target = new JSONObject();\r\n\t    String[] chkTarget = {\"server01\",\"server02\"};\r\n\t\tJSONArray targetArray = new JSONArray(chkTarget);\r\n\t    \r\n    \tJSONObject param = new JSONObject();\r\n\t\tparam.put(\"output\", new JSONArray(Arrays.asList(\"hostid\",\"host\",\"name\")));\r\n\t\ttarget.put(\"host\", targetArray);\r\n\t\tparam.put(\"filter\", target);\r\n\r\n\t\tresponse = Request(\"host.get\", param, zabbixEndpoint, token);\r\n\t\tObjectMapper mapper = new ObjectMapper();\r\n\t\tArrayNode arrayHost = (ArrayNode) mapper.readTree(response.getBody()).path(\"result\");\r\n\t\t\r\n\t    System.out.println(\"hosts: \" + arrayHost);\r\n    }\r\n\t\r\n\tprivate ResponseEntity&lt;String&gt; Request(String method, JSONObject param, String endPoint, String token) throws Exception {\r\n\t\tJSONObject json = new JSONObject();\r\n\r\n\t\tjson.put(\"jsonrpc\", \"2.0\");\r\n        json.put(\"method\", method);\r\n        json.put(\"params\", param);\r\n        json.put(\"id\", 1);\r\n\t\tif(token != null){\r\n            json.put(\"auth\", token);\r\n        }\r\n        HttpMethod httpMethod = HttpMethod.POST;\r\n        \r\n\t    HttpEntity &lt;String&gt; entity ;\r\n\t\tHttpHeaders headers = new HttpHeaders();\r\n\t    headers.add(\"Content-Type\", \"application\/json-rpc\");\r\n\t    headers.add(\"Accept-Language\", \"ja\");\r\n\t    \r\n\t    if(param == null){\r\n\t    \tentity = new HttpEntity &lt;String&gt;(headers);\r\n\t    }\r\n\t    else{\r\n\t    \tentity = new HttpEntity &lt;String&gt;(json.toString(), headers);\r\n\t    }\r\n\t    RestTemplate restTemplate = new RestTemplate();\r\n\t    restTemplate.getMessageConverters()\r\n        .add(0, new StringHttpMessageConverter(Charset.forName(\"UTF-8\")));\r\n\t    \r\n\t    ResponseEntity&lt;String&gt; response = restTemplate.exchange(endPoint, httpMethod, entity, String.class);\r\n\t    \r\n\t\treturn response;\r\n\t}\r\n<\/pre>\n<p>\u3067\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u623b\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>hosts: [{&#8220;hostid&#8221;:&#8221;10387&#8243;,&#8221;host&#8221;:&#8221;server01&#8243;,&#8221;name&#8221;:&#8221;server01_DB&#8221;},{&#8220;hostid&#8221;:&#8221;10388&#8243;,&#8221;host&#8221;:&#8221;server02&#8243;,&#8221;name&#8221;:&#8221;server02_DB&#8221;}]<\/p>\n<p>\u554f\u984c\u7121\u304f\u4f7f\u3048\u305d\u3046\u306a\u306e\u3067\u3001\u8981\u4ef6\u3092\u6574\u7406\u3057\u3066\u304b\u3089\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000Zabbix\u306e\u4f5c\u696d\u3092\u81ea\u52d5\u5316\u3057\u305f\u3044\u3068\u3044\u3046\u4e8b\u3067\u3001ZabbixAPI\u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f &hellip; <a href=\"https:\/\/esoro.jp\/?p=1818\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,2],"tags":[],"_links":{"self":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1818"}],"collection":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1818"}],"version-history":[{"count":0,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions"}],"wp:attachment":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}