java实现zabbix接口开发

2019-02-21 06:38:54来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

API:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/user/login

如果你使用jar包开发的话,会出现*** 1h ***,*** 1m ***这样类似的错误,是因为jar包里的实体类定义的属性类型不合适。所以目前jar包还不成熟,所以使用下面这种方法式。(注意:这种开发方式定义的实体类也要根据返回的类型做出匹配否则也会出现上面的问题。)

工具类:
public class MonitorUtils {

private String ZBX_URL = null;
private String USERNAME = null;
private String PASSWORD = null;
private String AUTH = null;

public void setParam() {
ConfigurationParameterUtil configurationParameterUtil = new ConfigurationParameterUtil();
Properties prop = configurationParameterUtil.GetProperties("monitor.properties");
ZBX_URL= prop.getProperty("ZBX_URL");
USERNAME= prop.getProperty("USERNAME");
PASSWORD= prop.getProperty("PASSWORD");
}


/**
* 向Zabbix发送Post请求,并返回json格式字符串
*
* @param param 请求参数
* @return
* @throws Exception
*/
public String sendPost(Map map) {
String param = JSON.toJSONString(map);
HttpURLConnection connection = null;
DataOutputStream out = null;
BufferedReader reader = null;
StringBuffer sb = null;
try {
// 创建连接
URL url = new URL(ZBX_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式

connection.connect();

// POST请求
out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(param);
out.flush();

// 读取响应
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return sb.toString();

}


/**
* 通过用户名和密码设置AUTH,获得权限 @
*/
public void setAuth() {
setParam();
Map<String, Object> params = new HashMap<String, Object>();
params.put("user", USERNAME);

params.put("password", PASSWORD);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "user.login");
map.put("params", params);
map.put("auth", null);
map.put("id", 0);

String response = sendPost(map);
JSONObject json = JSON.parseObject(response);
AUTH = json.getString("result");
}

public String getAuth() {
if (AUTH == null) {
setAuth();
}
return AUTH;
}
}

使用例子:
public void GetItem(){
Map<String, Object> search = new HashMap<String, Object>();
search.put("key_", key);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("hostids", hostId);
params.put("sortfield", "name");
params.put("search", search);

Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "item.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
}


原文链接:https://www.cnblogs.com/bcydsl/p/10407614.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:设计模式总结

下一篇:JAVA获取本机IP和Mac地址