美文网首页
zabbixApi4j-Host

zabbixApi4j-Host

作者: 差不多先生_tl | 来源:发表于2018-01-11 11:55 被阅读103次

    Host: 这个类的设计目的是与主机一起工作。

    host.create: 创建新主机
    host.delete: 删除主机
    host.exists: 检索主机是否存在
    host.get: 检索主机
    host.isreadable: 检索主机是否是可读的
    host.iswritable: 检索主机是否是可写的
    host.massadd: 向主机添加相关对象
    host.massremove: 移除主机相关对象
    host.massupdate: 从主机替换或移除相关对象
    host.update: 更新主机

    image.png
    HostGetTest
    package cn.com.yeexun.testzabbix.zabbix4j.example.host;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    
    import java.util.ArrayList;
    
    import org.junit.Test;
    
    import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
    
    import com.zabbix4j.ZabbixApiParamter;
    import com.zabbix4j.host.HostGetRequest;
    import com.zabbix4j.host.HostGetResponse;
    
    /**
     * Created by Suguru Yajima on 2014/05/02.
     */
    public class HostGetTest extends ZabbixApiTestBase {
    
        public HostGetTest() {
            super();
        }
    
        @Test
        public void testGet1() throws Exception {
    
            Integer targetHostId = 10108;
            HostGetRequest request = new HostGetRequest();
            HostGetRequest.Params params = request.getParams();
    
            ArrayList<Integer> hostIds = new ArrayList<Integer>();
            hostIds.add(targetHostId);
            params.setHostids(hostIds);
    
            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());
    
            HostGetResponse response = zabbixApi.host().get(request);
            assertNotNull(response);
    
            logger.debug(getGson().toJson(response));
    
            HostGetResponse.Result result = response.getResult().get(0);
            assertEquals(targetHostId, result.getHostid());
        }
    }
    
    
    HostCreateTest
    package cn.com.yeexun.testzabbix.zabbix4j.example.host;
    
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertTrue;
    import static org.junit.Assert.fail;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.junit.Test;
    
    import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
    
    import com.zabbix4j.host.HostCreateRequest;
    import com.zabbix4j.host.HostCreateResponse;
    import com.zabbix4j.hostinteface.HostInterfaceObject;
    import com.zabbix4j.usermacro.Macro;
    
    /**
     * Created by Suguru Yajima on 2014/05/01.
     */
    public class HostCreateTest extends ZabbixApiTestBase {
    
        public HostCreateTest() {
            super();
        }
    
        @Test
        public void testCreate1() {
            final Integer groupId = 25;
            final Integer templateId = 10093;
    
            try {
                HostCreateRequest request = new HostCreateRequest();
                HostCreateRequest.Params params = request.getParams();
    
                params.addTemplateId(templateId);
                params.addGroupId(groupId);
    
                List<Macro> macros = new ArrayList<Macro>();
                Macro macro1 = new Macro();
                macro1.setMacro("{$MACRO1}");
                macro1.setValue("value1");
                macros.add(macro1);
                params.setMacros(macros);
    
                HostInterfaceObject hostInterface = new HostInterfaceObject();
                hostInterface.setIp("192.168.255.255");
                params.addHostInterfaceObject(hostInterface);
    
                params.setHost("test host created1." + new Date().getTime());
                params.setName("test host created1 name" + new Date().getTime());
    
                HostCreateResponse response = zabbixApi.host().create(request);
                assertNotNull(response);
    
                assertNotNull(response.getResult().getHostids());
                int hostId = response.getResult().getHostids().get(0);
                assertTrue(0 < hostId);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    }
    
    
    HostDeleteTest
    package cn.com.yeexun.testzabbix.zabbix4j.example.host;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertTrue;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
    
    import com.zabbix4j.host.HostCreateRequest;
    import com.zabbix4j.host.HostCreateResponse;
    import com.zabbix4j.host.HostDeleteRequest;
    import com.zabbix4j.host.HostDeleteResponse;
    import com.zabbix4j.hostinteface.HostInterfaceObject;
    import com.zabbix4j.usermacro.Macro;
    
    /**
     * Created by Suguru Yajima on 2014/05/02.
     */
    public class HostDeleteTest extends ZabbixApiTestBase {
    
        public HostDeleteTest() {
            super();
        }
    
        @Test
        public void testDelete1() throws Exception {
    
            int hostId = createDummyHost();
    
            HostDeleteRequest request = new HostDeleteRequest();
            request.addParams(hostId);
    
            HostDeleteResponse response = zabbixApi.host().delete(request);
    
            assertNotNull(response);
            int deletedId = response.getResult().getHostids().get(0);
            assertEquals(hostId, deletedId);
        }
    
        private int createDummyHost() throws Exception {
    
            HostCreateRequest request = new HostCreateRequest();
            HostCreateRequest.Params params = request.getParams();
    
            List<Integer> templates = new ArrayList<Integer>();
            templates.add(10093);
            params.setTemplates(templates);
    
            params.addGroupId(12);
    
            List<Macro> macros = new ArrayList<Macro>();
            Macro macro1 = new Macro();
            macro1.setMacro("{$MACRO1}");
            macro1.setValue("value1");
            macros.add(macro1);
            params.setMacros(macros);
    
            List<HostInterfaceObject> interfaces = new ArrayList<HostInterfaceObject>();
            HostInterfaceObject hostInterface = new HostInterfaceObject();
            hostInterface.setIp("192.168.255.200");
            interfaces.add(hostInterface);
            params.setInterfaces(interfaces);
    
            params.setHost("test dummy host created1");
            params.setName("test dummy host created1 name");
    
            HostCreateResponse response = zabbixApi.host().create(request);
    
            assertNotNull(response);
            assertNotNull(response.getResult().getHostids());
            int hostId = response.getResult().getHostids().get(0);
            assertTrue(0 < hostId);
    
            return hostId;
    
        }
    }
    
    
    HostExistTest
    package cn.com.yeexun.testzabbix.zabbix4j.example.host;
    
    import static org.junit.Assert.assertFalse;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    
    import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
    
    import com.zabbix4j.host.HostExistRequest;
    import com.zabbix4j.host.HostExistResponse;
    
    /**
     * Created by Suguru Yajima on 2014/05/02.
     */
    public class HostExistTest extends ZabbixApiTestBase {
    
        public HostExistTest() {
            super();
        }
    
        @Test
        public void testExist1() throws Exception {
    
            int hostId = 10108;
    
            HostExistRequest request = new HostExistRequest();
            HostExistRequest.Params params = request.getParams();
            params.addHostId(hostId);
    
            HostExistResponse response = zabbixApi.host().exist(request);
    
            assertNotNull(response);
            assertTrue(response.isResult());
        }
    
        @Test
        public void testExist2() throws Exception {
    
            int hostId = 999999;
    
            HostExistRequest request = new HostExistRequest();
            HostExistRequest.Params params = request.getParams();
            params.addHostId(hostId);
    
            HostExistResponse response = zabbixApi.host().exist(request);
    
            assertNotNull(response);
            assertFalse(response.isResult());
        }
    }
    
    
    HostGetTest
    package cn.com.yeexun.testzabbix.zabbix4j.example.host;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    
    import java.util.ArrayList;
    
    import org.junit.Test;
    
    import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
    
    import com.zabbix4j.ZabbixApiParamter;
    import com.zabbix4j.host.HostGetRequest;
    import com.zabbix4j.host.HostGetResponse;
    
    /**
     * Created by Suguru Yajima on 2014/05/02.
     */
    public class HostGetTest extends ZabbixApiTestBase {
    
        public HostGetTest() {
            super();
        }
    
        @Test
        public void testGet1() throws Exception {
    
            Integer targetHostId = 10108;
            HostGetRequest request = new HostGetRequest();
            HostGetRequest.Params params = request.getParams();
    
            ArrayList<Integer> hostIds = new ArrayList<Integer>();
            hostIds.add(targetHostId);
            params.setHostids(hostIds);
    
            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());
    
            HostGetResponse response = zabbixApi.host().get(request);
            assertNotNull(response);
    
            logger.debug(getGson().toJson(response));
    
            HostGetResponse.Result result = response.getResult().get(0);
            assertEquals(targetHostId, result.getHostid());
        }
    }
    
    
    HostUpdateTest
    package cn.com.yeexun.testzabbix.zabbix4j.example.host;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.fail;
    
    import java.util.ArrayList;
    
    import org.junit.Test;
    
    import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
    
    import com.zabbix4j.host.HostUpdateRequest;
    import com.zabbix4j.host.HostUpdateResponse;
    import com.zabbix4j.usermacro.Macro;
    
    /**
     * Created by Suguru Yajima on 2014/05/01.
     */
    public class HostUpdateTest extends ZabbixApiTestBase {
    
        public HostUpdateTest() {
            super();
        }
    
        @Test
        public void testUpdate1() throws Exception {
    
            int targetHostId = 10108;
            try {
                HostUpdateRequest request = new HostUpdateRequest();
                HostUpdateRequest.Params params = request.getParams();
    
                params.setHostid(targetHostId);
                params.setHost("192.168.100.100");
                params.setName("test host update1");
    
                params.setGroup(2);
    
                params.setUnLinkTemplate(10093);
    
                ArrayList<Integer> templates = new ArrayList<Integer>();
                templates.add(10093);
                params.setTemplates(templates);
    
                Macro macro = new Macro();
                macro.setMacro("{$MACRO2}");
                macro.setValue("VALUE2");
                params.setMacro(macro);
    
                HostUpdateResponse response = zabbixApi.host().update(request);
    
                assertNotNull(response);
                int actualId = response.getResult().getHostids().get(0);
                assertEquals(targetHostId, actualId);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:zabbixApi4j-Host

          本文链接:https://www.haomeiwen.com/subject/bypznxtx.html