Zabbix-java Library to access Zabbix Api__java

Source: Internet
Author: User
Tags auth documentation

Using the Java language to invoke the Zabbix API to get monitoring data


1, create the Request class

Import Java.util.HashMap;

Import Java.util.Map;

Import Com.alibaba.fastjson.JSON;

	public class Request {private String JSONRPC = "2.0";

	Private map<string, object> params = new hashmap<> ();

	Private String method;

	Private String auth;

	Private Integer ID;
	public void Putparam (String key, Object value) {params.put (key, value);
	Public Object Removeparam (String key) {return params.remove (key);
	Public String Getjsonrpc () {return jsonrpc;
	} public void Setjsonrpc (String jsonrpc) {this.jsonrpc = Jsonrpc;
	Public map<string, Object> Getparams () {return params;
	public void SetParams (map<string, object> params) {this.params = params;
	Public String GetMethod () {return method;
	public void Setmethod (String method) {This.method = method;
	Public String GetAuth () {return auth;
	} public void Setauth (String auth) {This.auth = auth;
	Public Integer GetId () {return id; } public void SetId (INteger id) {this.id = ID;
	@Override public String toString () {return json.tojsonstring (this);
 }
}

2, create the call () method

Public Jsonobject Call (Request request) throws URISyntaxException {
		if (request.getauth () = null) {
			Request.setauth (auth);
		}
		String url = "Zabbix_url";
		try {
			Httpurirequest HttpRequest = Org.apache.http.client.methods.RequestBuilder
					. Post (). Seturi (New URI ( Url.trim ()))
					. AddHeader ("Content-type", "Application/json")
					. Setentity (New Stringentity ( Json.tojsonstring (Request))
					. Build ();
			Closeablehttpclient Httpclient=httpclients.custom (). build ();
			Closeablehttpresponse response = Httpclient.execute (HttpRequest);
			httpentity entity = response.getentity ();
			byte[] data = Entityutils.tobytearray (entity);
			Return (Jsonobject) json.parse (data);
		} catch (IOException e) {
			throw new RuntimeException ("Defaultzabbixapi call exception!", e);
		}
	


3, create the login () method

public Boolean login () throws URISyntaxException {
		 String uname = "Zabbix_uname";
		 String upwd = "Zabbix_upwd";
		 
		Request Request = Requestbuilder.newbuilder (). Paramentry ("user", uname)
				. Paramentry ("password", Upwd). Method (" User.login "). Build ();
		Jsonobject response = call (request);
		String auth = response.getstring ("result");
		if (auth!= null &&!auth.isempty ()) {
			This.setauth (auth);
			Logger.info ("Login is success!");
			return true;
		}
		return false;
	}


4. Query Item

Public Jsonobject queryitems (String hostname,string searchkey,string groupname) throws exception{Jsonobject
		Response = null;
		if (login ()) {
			Jsonobject search = new Jsonobject ();
			Logger.info ("Queryitems--searchkey:" +searchkey+ "HostName:" +hostname+ "groupname  :" +groupname);
		    Search.put ("Key_", Searchkey);
			Request Request = Requestbuilder.newbuilder (). Method ("Item.get")
					. Paramentry ("Output", "extend")
					. Paramentry ("host", HostName)
					. Paramentry ("Search", search)
					//.paramentry ("group", GroupName)
					. Build ();
			 Response = call (request);
		}
		return response;
	}

In the Jsonobject class, take the value http://codingfarmer-v.iteye.com/blog/2299598


API Documentation Https://www.zabbix.com/documentation/2.2/manual/api

Docs http://www.zabbix.org/wiki/Docs/api/libraries

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.