Zabbix Practice (V): Java-based Zabbix API invocation to achieve data sharing

Source: Internet
Author: User

After the use of Zabbix for monitoring, often because of the requirements of the project, the need for Zabbix data, two times to share and develop. How to obtain data from Zabbix is a problem to be solved in this practice.

There are two ways to Zabbix data sharing

1 through the database query, get the data. The premise is to have a deep understanding of the Zabbbix database model, and in a large number of monitoring, can not because the query affect the operation of Zabbix biological

2 through the Zabbix API, data acquisition, this way relatively reliable one, the target

The Java API is used to achieve ZABBIX data acquisition. Second, the environment

On the virtual machine 144 on this machine, the Zabbix server is installed, and the monitoring agent is deployed on 144,155 respectively. It's all CentOS systems.

Use Eclipse for data acquisition on this computer. JDK for 1.7 III, access to the API

Http://zabbix.org/wiki/Docs/api/libraries This site lists the API conditions, as shown in the following figure

After this, you can see that there are three kinds of Java APIs

The first and third are the APIs that connect to the Zabbix server, the first of which is the simplest API that needs to construct the JSON-formatted parameter itself. The second sender is the development of an API to send data to the server. The third is the API that the practice concerns, the API inside, further encapsulation, also provides some test code. This practice is based on this practice.

The dependent libraries are as follows

Below the code, the code has done a more detailed description.

Package com.zabbix4j.apitest;
Import Com.zabbix4j.ZabbixApi;
Import com.zabbix4j.ZabbixApiException;
Import Com.zabbix4j.ZabbixApiParamter;
Import com.zabbix4j.host.*;
Import Com.zabbix4j.hostinteface.HostInterfaceObject;
Import Com.zabbix4j.item.ItemObject;

Import Com.zabbix4j.usermacro.Macro;
Import java.util.ArrayList;

Import java.util.List; public class Zabbix4jsamplegethost extends Zabbixapitestdummymethodbase {public zabbix4jsamplegethost (Zabbixapi Zabb
    IXAPI) {super (ZABBIXAPI);
    ///Here Configure Zabbix URL, account number and password public static final String Zbx_url = "http://192.168.136.144/zabbix/api_jsonrpc.php";
    public static final String USERNAME = "admin";

    public static final String PASSWORD = "Zabbix";
	The wrapper public Hostgetresponse GetHost () throws zabbixapiexception {Integer Targethostid = 10105) of the host get method that invoked the API;
	Hostgetrequest request = new Hostgetrequest ();

	Hostgetrequest.params Params = Request.getparams (); arraylist<integer> hostids = new ArrayList<integer> ();
	Hostids.add (Targethostid);
	Params.sethostids (Hostids); Here you can set the specified ID value or do not set the value.

	Set the value, take the specified content, not set, get all the host params.sethostids (NULL);
	Params.setselectdiscoveryrule (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectgroups (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectitems (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectapplications (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectdiscoveries (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectgraphs (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselecthostdiscovery (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselecthttptests (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectinterfaces (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectinventory (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectmacros (ZabbixApiParamter.QUERY.extend.name ());
	Params.setselectparenttemplates (ZabbixApiParamter.QUERY.extend.name ()); Params.setselectscreens (ZabbIxApiParamter.QUERY.extend.name ());

	Params.setselecttriggers (ZabbixApiParamter.QUERY.extend.name ());
	System.out.println ("Parames setting complete ...");

	Hostgetresponse response = Zabbixapi.host (). get (request);
    return response;
	    public static void Main (string[] args) {try {//Login to Zabbix System.out.println ("beging ...");
	    Zabbixapi Zabbixapi = new Zabbixapi (Zbx_url);

	    Zabbixapi.login (USERNAME, PASSWORD);
	    System.out.println ("Create new Get Host instance ...");

	    Zabbix4jsamplegethost mygethost = new Zabbix4jsamplegethost (ZABBIXAPI);
	    System.out.println ("Get host beging ...");

	    Hostgetresponse response = Mygethost.gethost ();

	    System.out.println ("Get-host End...let ' print result"); Response is processed for (int i = 0; i < Response.getresult (). Size (); i++) {//response The information returned is very large, can be assigned to Hostobject, and can be
		With other objects Hostobject Myhostobject = Response.getresult (). get (i); Response The information returned is very large, can be assigned to Itemobject, or other objects List<ItemObject> myitemobjectlist = Response.getresult (). Get (i) getItems ();
		Print host information if (NULL = = Myhostobject) System.out.println ("Get host NULL, program'll Exit");
		    else {System.out.println ("++++++print result" + i + "hostobject+++++++++++");
		    System.out.println (Myhostobject.gethost ());
		    System.out.println (Myhostobject.getname ());
		    System.out.println (Myhostobject.getavailable ());
		    System.out.println (Myhostobject.gethostid ());
		System.out.println (Myhostobject.getstatus ()); //Print the item information for (int j = 0; J < Myitemobjectlist.size (); j + +) {if (j = = 0) System.out.println ("
		    ++++++print itemobject List "+" +++++++++++ ");
		    Itemobject Tmpitemobject = Myitemobjectlist.get (j);
		    System.out.println ("Itemid:" + tmpitemobject.getitemid ());
		    System.out.println ("TemplateID:" + Tmpitemobject.gettemplateid ());
		    System.out.println ("Hostid:" + Tmpitemobject.gethostid ()); System.ouT.println ("Name:" + tmpitemobject.getname ());
		    System.out.println ("Key:" + tmpitemobject.getkey_ ());
		    System.out.println ("Lastvalue:" + tmpitemobject.getlastvalue ());
		    System.out.println ("Lastclock:" + tmpitemobject.getlastclock ());

		System.out. println ("------------------------------------------");

	    } System.out. println ("*****************%%%%%%%%%%%%%%************");
	} catch (Zabbixapiexception e) {e.printstacktrace ();
 }
    }
}
Finally, post the results of the execution.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.