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<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,创建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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值