I wrote the code to take the history of the item in Zabbix server and display it in Java. This time, I decided to extract the history of local time, which is the easiest to understand.
https://github.com/hengyunabc/zabbix-api Debug item
Connect to the API and view the version.
Main.java
DefaultZabbixApi ZabbixAPI = new DefaultZabbixApi("http://[Host IP]/zabbix/api_jsonrpc.php");
ZabbixAPI.init();//Initialization
System.out.println(ZabbixAPI.apiVersion());//Version display
Then log in.
Main.java
boolean login = ZabbixAPI.login("name", "password");
System.err.println("login:" + login);
If the login is successful, login: true is displayed. From here you can use the functions of Zabbix API.
Before fetching the history, fetch the item ID with item.get.
Main.java
//request
JSONObject filter = new JSONObject();
filter.put("key_", new String[]{"system.localtime"});//Item key
filter.put("name", new String[]{"myitem"});//Item name
Request getRequest = RequestBuilder.newBuilder().method("item.get").paramEntry("filter", filter).build();
//response
JSONObject getResponse = ZabbixAPI.call(getRequest);
String itemid = getResponse.getJSONArray("result").getJSONObject(0).getString("itemid");
String name = getResponse.getJSONArray("result").getJSONObject(0).getString("name");
The name of the item is also included in the response to confirm that the correct data has been obtained.
Next, get the history.
Main.java
Date date = new Date();
Timestamp now = new Timestamp(date.getTime()/1000L);//time_In till, specify by Unix Timestamp
//iteids:itemid specification history:Specify data type sortfield:Specify sort criteria sortoder:Ascending or descending time_till:Display data up to the specified time
getRequest = RequestBuilder.newBuilder().method("history.get")
.paramEntry("itemids", itemid).paramEntry("history", 3)
.paramEntry("sortfield", "clock").paramEntry("sortorder", "DESC")
.paramEntry("time_till", now).build();
getResponse = ZabbixAPI.call(getRequest);
for(int i = 0;i < getResponse.getJSONArray("result").size();i++) {
String history = getResponse.getJSONArray("result").getJSONObject(i).getString("value");//You can take it with Int type
System.out.println(new Timestamp(Integer.parseInt(history) * 1000L));//Convert Unix Timestamp to java Timestamp
}
Whole code
Main.java
public class Main {
public void start() {
DefaultZabbixApi ZabbixAPI = new DefaultZabbixApi("http://[Host IP]/zabbix/api_jsonrpc.php");
ZabbixAPI.init();
System.out.println(ZabbixAPI.apiVersion());
boolean login = ZabbixAPI.login("name", "password");
System.err.println("login:" + login);
JSONObject filter = new JSONObject();
filter.put("key_", new String[]{"system.localtime"});
filter.put("name", new String[]{"myitem"});
Request getRequest = RequestBuilder.newBuilder().method("item.get").paramEntry("filter", filter).build();
JSONObject getResponse = ZabbixAPI.call(getRequest);
String itemid = getResponse.getJSONArray("result").getJSONObject(0).getString("itemid");
String name = getResponse.getJSONArray("result").getJSONObject(0).getString("name");
Date date = new Date();
Timestamp now = new Timestamp(date.getTime()/1000L);//time_In till, specify by Unix Timestamp
getRequest = RequestBuilder.newBuilder().method("history.get")
.paramEntry("itemids", itemid).paramEntry("history", 3)
.paramEntry("sortfield", "clock").paramEntry("sortorder", "DESC")
.paramEntry("time_till", now).build();
getResponse = ZabbixAPI.call(getRequest);
for(int i = 0;i < getResponse.getJSONArray("result").size();i++) {
String history = getResponse.getJSONArray("result").getJSONObject(i).getString("value");
long time = getResponse.getJSONArray("result").getJSONObject(i).getLong("clock");
System.out.println(new Timestamp(Integer.parseInt(history) * 1000L));//Convert Unix Timestamp to java Timestamp
}
}
public static void main(String[] args) {
Main m = new Main();
m.start();
}
}
First, I wrote it in Go language to understand how to use the API and how it works, and then wrote it in Java, but since the JSON protocol is fixed, the code itself did not have to be that long. I will write this from the place where JSON is thrown with httpclient.
https://www.zabbix.com/documentation/2.2/manual/api A list of API functions is listed here, so if you want to use other functions, please refer to it.
Recommended Posts