java实现zabbix接口开发
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线程Run和Start的区别
下一篇: 别人动手能力怎么这么好呢