通过调用API获取zabbix监控服务(JAVA实现)案例

        因为项目保密的原因,实际代码无法贴出,但与他人博客中的代码有一定的相似度,现拷贝部分作为参考,再结合我专栏中的技术文档,以便理解对zabbix的部分封装过程

 Zabbix4jSampleGetHost.java

    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 zabbixApi) {  
            super(zabbixApi);  
        }  
      
        // 这里配置zabbix的url,帐号和密码  
        public static final String ZBX_URL = "http://192.168.4.132/zabbix/api_jsonrpc.php";  
        public static final String USERNAME = "Admin";  
        public static final String PASSWORD = "zabbix";  
      
        // 调用api的host get方法的封装  
        public HostGetResponse getHost() throws ZabbixApiException {  
      
	        Integer targetHostId = 10105;  
	        HostGetRequest request = new HostGetRequest();  
	        HostGetRequest.Params params = request.getParams();  
	      
	        ArrayList hostIds = new ArrayList();  
	        hostIds.add(targetHostId);  
	        // params.setHostids(hostIds);  
	        // 这里可以设指定的id值,也可以不设值。设值的话,取指定的内容,不设的话,获取全部的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's print result");  
	      
	            // 对response进行处理  
	            for (int i = 0; i < response.getResult().size(); i++) {  
	      
		            //response 返回的信息非常大,可以赋值给HostObject,也可以其他对象  
		            HostObject myHostObject = response.getResult().get(i);  
		            //response 返回的信息非常大,可以赋值给ItemObject,也可以其他对象  
		            List myItemObjectList = response.getResult().get(i)  
		                .getItems();  
		      
		            //打印host信息  
		            if (null == myHostObject)  
		                System.out.println("Get host null, program will 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());  
		            }  
		      
		            //打印这个host下的item信息  
		            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();  
	        }  
        }  
}  

ZabbixApiTestDummyMethodBase.java

package com.zabbix4j.apitest;

import com.zabbix4j.ZabbixApi;

public class ZabbixApiTestDummyMethodBase {  
	  
    protected ZabbixApi zabbixApi;  
  
    public ZabbixApiTestDummyMethodBase(ZabbixApi zabbixApi) {  
        this.zabbixApi = zabbixApi;  
    }  
}

相关jar包下载地址:http://zabbix.org/wiki/Docs/api/libraries#Java

你可能感兴趣的:(Zabbix服务监控,Zabbix监控服务)