码迷,mamicode.com
首页 > 编程语言 > 详细

java实现zabbix接口开发

时间:2019-02-20 17:04:31      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:zabbix   用例   格式   map   buffer   item   monit   man   public   

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");
}

java实现zabbix接口开发

标签:zabbix   用例   格式   map   buffer   item   monit   man   public   

原文地址:https://www.cnblogs.com/bcydsl/p/10407614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!