java實現zabbix介面開發

伊人如夢月猶殘發表於2019-02-20

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

相關文章