API:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/user/login
如果你使用jar包開發的話,會出現*** 1h ***,*** 1m ***這樣類似的錯誤,是因為jar包里的實體類定義的屬性類型不合適。所以目前jar包還不成熟,所以使用下面這種方法式。(注意:這種開發方式定義的實體類也要根據返回的類型做出匹配否則也會出現上面的問題。)
工具類:
1 public class MonitorUtils { 2 3 private String ZBX_URL = null; 4 private String USERNAME = null; 5 private String PASSWORD = null; 6 private String AUTH = null; 7 8 public void setParam() { 9 ConfigurationParameterUtil configurationParameterUtil = new ConfigurationParameterUtil(); 10 Properties prop = configurationParameterUtil.GetProperties("monitor.properties"); 11 ZBX_URL= prop.getProperty("ZBX_URL"); 12 USERNAME= prop.getProperty("USERNAME"); 13 PASSWORD= prop.getProperty("PASSWORD"); 14 } 15 16 17 /** 18 * 向Zabbix發送Post請求,並返回json格式字符串 19 * 20 * @param param 請求參數 21 * @return 22 * @throws Exception 23 */ 24 public String sendPost(Map map) { 25 String param = JSON.toJSONString(map); 26 HttpURLConnection connection = null; 27 DataOutputStream out = null; 28 BufferedReader reader = null; 29 StringBuffer sb = null; 30 try { 31 // 創建連接 32 URL url = new URL(ZBX_URL); 33 connection = (HttpURLConnection) url.openConnection(); 34 connection.setDoOutput(true); 35 connection.setDoInput(true); 36 connection.setUseCaches(false); 37 connection.setInstanceFollowRedirects(true); 38 connection.setRequestMethod("POST"); 39 connection.setRequestProperty("Accept", "application/json"); // 設置接收數據的格式 40 connection.setRequestProperty("Content-Type", "application/json"); // 設置發送數據的格式 41 42 connection.connect(); 43 44 // POST請求 45 out = new DataOutputStream(connection.getOutputStream()); 46 out.writeBytes(param); 47 out.flush(); 48 49 // 讀取響應 50 reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 51 String lines; 52 sb = new StringBuffer(""); 53 while ((lines = reader.readLine()) != null) { 54 lines = new String(lines.getBytes(), "utf-8"); 55 sb.append(lines); 56 } 57 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } finally { 61 if (out != null) { 62 try { 63 out.close(); 64 } catch (IOException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 if (reader != null) { 70 try { 71 reader.close(); 72 } catch (IOException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 75 } 76 } 77 if (connection != null) { 78 connection.disconnect(); 79 } 80 } 81 return sb.toString(); 82 83 } 84 85 86 /** 87 * 通過用戶名和密碼設置AUTH,獲得權限 @ 88 */ 89 public void setAuth() { 90 setParam(); 91 Map<String, Object> params = new HashMap<String, Object>(); 92 params.put("user", USERNAME); 93 94 params.put("password", PASSWORD); 95 Map<String, Object> map = new HashMap<String, Object>(); 96 map.put("jsonrpc", "2.0"); 97 map.put("method", "user.login"); 98 map.put("params", params); 99 map.put("auth", null); 100 map.put("id", 0); 101 102 String response = sendPost(map); 103 JSONObject json = JSON.parseObject(response); 104 AUTH = json.getString("result"); 105 } 106 107 public String getAuth() { 108 if (AUTH == null) { 109 setAuth(); 110 } 111 return AUTH; 112 } 113 } 114 下面新建一個類 115 使用例子: 116 public void GetItem(){ 117 Map<String, Object> search = new HashMap<String, Object>(); 118 search.put("key_", key); 119 Map<String, Object> params = new HashMap<String, Object>(); 120 params.put("output", "extend"); 121 params.put("hostids", hostId); 122 params.put("sortfield", "name"); 123 params.put("search", search); 124 125 Map<String, Object> map = new HashMap<String, Object>(); 126 map.put("jsonrpc", "2.0"); 127 map.put("method", "item.get"); 128 map.put("params", params); 129 map.put("auth", getAuth()); 130 map.put("id", 0); 131 String response = sendPost(map); 132 JSONArray result = JSON.parseObject(response).getJSONArray("result"); 133 }