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