美文网首页
zabbixApi4j-LLD rule

zabbixApi4j-LLD rule

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

LLD rule

discoveryrule.copy: 复制LLD规则
discoveryrule.create: 创建新的LLD规则
discoveryrule.delete: 删除LLD规则
discoveryrule.exists: 检查LLD规则是否存在
discoveryrule.get: 检索LLD规则
discoveryrule.isreadable: 检查LLD规则是否是可读的
discoveryrule.iswirtable: 检查LLD规则是否是可写的
discoveryrule.update: 更新LLD规则

image.png

LLDRuleCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.lldrule;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import cn.com.yeexun.testzabbix.zabbix4j.example.host.DummyHost;

import com.zabbix4j.lldrule.LLDRuleCreateRequest;
import com.zabbix4j.lldrule.LLDRuleCreateResponse;
import com.zabbix4j.lldrule.LLDRuleObject;

/**
 * @author Suguru Yajima
 */
public class LLDRuleCreateTest extends ZabbixApiTestBase {

    public LLDRuleCreateTest() {
        super();
    }

    @Test
    public void testCreate1() throws Exception {

        Integer targetHostId = null;
        try {
            DummyHost dummyHost = new DummyHost(zabbixApi);
            targetHostId = dummyHost.createHost();
            Integer interfaceId = dummyHost.getHostInterface(targetHostId);

            LLDRuleCreateRequest request = new LLDRuleCreateRequest();
            LLDRuleCreateRequest.Params params = request.getParams();
            params.setDelay(30);
            params.setHostid(targetHostId);
            params.setInterfaceid(interfaceId);
            params.setKey_("test");
            params.setName("LLD create");
            params.setType(LLDRuleObject.LLD_RULE_TYPE.SIMPLE_CHECK.value);

            LLDRuleCreateResponse response = zabbixApi.lldRule().create(request);
            assertNotNull(response);

            logger.debug(getGson().toJson(response));

            Integer lldRuleId = response.getResult().getItemids().get(0);
            assertNotNull(lldRuleId);

            new ZabbixApiTestDummyLLDRule(zabbixApi).deleteLLDRule(lldRuleId);
        } finally {
            new DummyHost(zabbixApi).deleteHost(targetHostId);
        }
    }
}

LLDRuleDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.lldrule;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.hamcrest.core.Is;
import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.lldrule.LLDRuleDeleteRequest;
import com.zabbix4j.lldrule.LLDRuleDeleteResponse;

/**
 * @author Suguru Yajima
 */
public class LLDRuleDeleteTest extends ZabbixApiTestBase {

    public LLDRuleDeleteTest() {
        super();
    }

    @Test
    public void testDelete1() throws Exception {

        Integer targetId = new ZabbixApiTestDummyLLDRule(zabbixApi).createLLDRule();

        LLDRuleDeleteRequest request = new LLDRuleDeleteRequest();
        request.addruleId(targetId);

        LLDRuleDeleteResponse response = zabbixApi.lldRule().delete(request);
        assertNotNull(response);

        logger.debug(getGson().toJson(response));

        Integer actualId = response.getResult().getRuleids().get(0);
        assertThat(targetId, Is.is(actualId));
    }
}

LLDRuleGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.lldrule;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.lldrule.LLDRuleGetRequest;
import com.zabbix4j.lldrule.LLDRuleGetResponse;

/**
 * @author Suguru Yajima
 */
public class LLDRuleGetTest extends ZabbixApiTestBase {

    public LLDRuleGetTest() {
        super();
    }

    @Test
    public void testGet1() throws Exception {

        ZabbixApiTestDummyLLDRule dummyLLDRule = new ZabbixApiTestDummyLLDRule(zabbixApi);
        Integer targetId = dummyLLDRule.createLLDRule();
        try {
            LLDRuleGetRequest request = new LLDRuleGetRequest();
            LLDRuleGetRequest.Params params = request.getParams();

            params.addItemId(targetId);
            params.setSelectItems(ZabbixApiParamter.QUERY.extend.name());
            params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
            params.setSelectHostPrototypes(ZabbixApiParamter.QUERY.extend.name());
            params.setSelectGraphs(ZabbixApiParamter.QUERY.extend.name());

            LLDRuleGetResponse response = zabbixApi.lldRule().get(request);
            assertNotNull(response);

            logger.debug(getGson().toJson(response));
        } finally {
            dummyLLDRule.deleteLLDRule(targetId);
        }
    }
}

LLDRuleUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.lldrule;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.hamcrest.core.Is;
import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.lldrule.LLDRuleUpdateRequest;
import com.zabbix4j.lldrule.LLDRuleUpdateResponse;

/**
 * @author Suguru Yajima
 */
public class LLDRuleUpdateTest extends ZabbixApiTestBase {

    private Integer hostId;

    public LLDRuleUpdateTest() {
        super();
    }

    @Test
    public void testUpdate1() throws Exception {
        Integer targetId = null;
        try {
            targetId = new ZabbixApiTestDummyLLDRule(zabbixApi).createLLDRule();

            LLDRuleUpdateRequest request = new LLDRuleUpdateRequest();
            LLDRuleUpdateRequest.Params params = request.getParams();

            params.setItemid(targetId);
            params.setName("LLD Rule Update");
            params.setKey_("key_udate");

            LLDRuleUpdateResponse response = zabbixApi.lldRule().update(request);
            assertNotNull(response);

            logger.debug(getGson().toJson(response));

            Integer actualId = response.getResult().getItemids().get(0);
            assertThat(targetId, Is.is(actualId));
        } finally {
            new ZabbixApiTestDummyLLDRule(zabbixApi).deleteLLDRule(targetId);
        }
    }
}

ZabbixApiTestDummyLLDRule
package cn.com.yeexun.testzabbix.zabbix4j.example.lldrule;

import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestDummyMethodBase;

import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.hostinteface.HostInterfaceGetRequest;
import com.zabbix4j.hostinteface.HostInterfaceGetResponse;
import com.zabbix4j.lldrule.LLDRuleCreateRequest;
import com.zabbix4j.lldrule.LLDRuleCreateResponse;
import com.zabbix4j.lldrule.LLDRuleDeleteRequest;
import com.zabbix4j.lldrule.LLDRuleDeleteResponse;
import com.zabbix4j.lldrule.LLDRuleObject;

/**
 * @author Suguru Yajima
 */
public class ZabbixApiTestDummyLLDRule extends ZabbixApiTestDummyMethodBase {


    private Integer hostId = 10141;

    public ZabbixApiTestDummyLLDRule(ZabbixApi zabbixApi) {
        super(zabbixApi);
    }

    public Integer createLLDRule() {
        Integer targetHostId = null;

        Integer interfaceId;
        try {
            interfaceId = getHostInterface(hostId);
        

        LLDRuleCreateRequest request = new LLDRuleCreateRequest();
        LLDRuleCreateRequest.Params params = request.getParams();
        params.setDelay(30);
        params.setHostid(hostId);
        params.setInterfaceid(interfaceId);
        params.setKey_("test");
        params.setName("LLD create");
        params.setType(LLDRuleObject.LLD_RULE_TYPE.SIMPLE_CHECK.value);

        LLDRuleCreateResponse response = zabbixApi.lldRule().create(request);
        assertNotNull(response);
        


        Integer lldRuleId = response.getResult().getItemids().get(0);
        
        return lldRuleId;
        } catch (ZabbixApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public Integer getHostInterface(Integer id) throws ZabbixApiException {

        HostInterfaceGetRequest request = new HostInterfaceGetRequest();
        HostInterfaceGetRequest.Params params = request.getParams();
        ArrayList<Integer> hostids = new ArrayList<Integer>();
        hostids.add(id);
        params.setHostids(hostids);

        HostInterfaceGetResponse response = zabbixApi.hostInterface().get(request);

        return response.getResult().get(0).getInterfaceid();
    }

    public void deleteLLDRule(Integer id) throws ZabbixApiException {

        LLDRuleDeleteRequest request = new LLDRuleDeleteRequest();
        request.addruleId(id);

        LLDRuleDeleteResponse response = zabbixApi.lldRule().delete(request);
    }
}

相关文章

网友评论

      本文标题:zabbixApi4j-LLD rule

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