zabbix-Java library to access Zabbix API

使用java语言调用zabbix api,获取监控数据


1,创建 Request 类

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

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

	private Map 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 getParams() {
		return params;
	}

	public void setParams(Map 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,创建call()方法

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,创建login()方法

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.查询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;
	}

在jsonobject类中取值 http://codingfarmer-v.iteye.com/blog/2299598


api文档   https://www.zabbix.com/documentation/2.2/manual/api

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

你可能感兴趣的:(zabbix)