java实现zabbix接口开发

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 params = new HashMap();
 92 params.put("user", USERNAME);
 93 
 94 params.put("password", PASSWORD);
 95 Map map = new HashMap();
 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 search = new HashMap();
118 search.put("key_", key);
119 Map params = new HashMap();
120 params.put("output", "extend");
121 params.put("hostids", hostId);
122 params.put("sortfield", "name");
123 params.put("search", search);
124 
125 Map map = new HashMap();
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 }

 

转载于:https://www.cnblogs.com/bcydsl/p/10407614.html

你可能感兴趣的:(java实现zabbix接口开发)