Get history from Zabbix server in Java

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.

What to prepare

https://github.com/hengyunabc/zabbix-api Debug item

Things to start

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.

Source code

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

Get history from Zabbix server in Java
Zabbix API in Java
How to get Class from Element in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Get EXIF information in Java
Java history in this world
[Java] Get KClass in Java [Kotlin]
[Android development] Get an image from the server in Java and set it in ImageView! !!
JSON in Java and Jackson Part 1 Return JSON from the server
Get weather forecasts from Watson Weather Company Data in simple Java
Get attributes and values from an XML file in Java
Get a non-empty collection from an Optional stream in java
Try implementing GraphQL server in Java
Get country from IP address (Java)
Get Null-safe Map values in Java
Get stuck in a Java primer
[Java] Get data from DB using singleton service in Spring (Boot)
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
Study Deep Learning from scratch in Java.
Get the result of POST in Java
Call Java method from JavaScript executed in Java
OCR in Java (character recognition from images)
Get caller information from stack trace (java)
Kick ShellScript on the server from Java
Reverse Key from Value in Java Map
Using JavaScript from Java in Rhino 2021 version
How to get the date in java
Get weather forecast from OpenWeatherMap in Rails
Run chromium in kiosk mode from Ubuntu Server
Java method call from RPG (method call in own class)
Migrate from Java to Server Side Kotlin + Spring-boot
Changes in Java 11
Rock-paper-scissors in Java
Text extraction in Java from PDF with pdfbox-2.0.8
Capture and save from selenium installation in Java
Library "OSHI" to get system information in Java
[Java] Get multiple values from one return value
[Java] Get a random value from an array
[Deep Learning from scratch] in Java 3. Neural network
Until you start Zabbix Server with docker-compose and get information from other hosts
Pi in Java
Minecraft BE server development from PHP to Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
FizzBuzz in Java
Get along with Java containers in Cloud Run
Get the URL of the HTTP redirect destination in Java
Try calling synchronized methods from multiple threads in Java
Delete All from Java SDK in Azure Cosmos DB
Source used to get the redirect source URL in Java
Get Locale objects for all locales available in Java
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
Using the database (SQL Server 2014) from a Java program 2018/01/04
What I learned when building a server in Java
Reverse Enum constants from strings and values in Java
Call a program written in Swift from Processing (Java)
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java