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<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 }

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值