zabbixApi4j-Host prototype

Host prototype

hostprototype.create: 创建新主机原型
hostprototype.delete: 删除主机原型
hostprototype.get: 检索主机原型
hostprototype.isreadable: 检查主机原型是否是可读的
hostprototype.iswritable: 检查主机原型是否是可写的
hostprototype.update: 更新主机原型

image.png


HostPrototypeCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.hostprototype;

import org.junit.Test;

import com.zabbix4j.hostprototype.GroupLinkObject;
import com.zabbix4j.hostprototype.GroupPrototypeObject;
import com.zabbix4j.hostprototype.HostPrototypeCreateRequest;
import com.zabbix4j.hostprototype.HostPrototypeCreateResponse;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import static org.junit.Assert.assertNotNull;

/**
 * Created by Suguru Yajima on 2014/06/05.
 */
public class HostPrototypeCreateTest extends ZabbixApiTestBase {

    private final int TEST_GROUP_ID = 20;

    public HostPrototypeCreateTest() {
        super();
    }

    @Test
    public void testCreate1() throws Exception {

        ZabbixApiTestDummyLLDRule dummyLLD = new ZabbixApiTestDummyLLDRule(zabbixApi);
        Integer lldRuleID = dummyLLD.createLLDRule();
        try {
            HostPrototypeCreateRequest request = new HostPrototypeCreateRequest();
            HostPrototypeCreateRequest.Params params = request.getParams();
            params.setRuleid(lldRuleID);
            GroupLinkObject linkObj = new GroupLinkObject();
            linkObj.setGroupid(TEST_GROUP_ID);
            params.addGroupLink(linkObj);

            GroupPrototypeObject typeObj = new GroupPrototypeObject();
            typeObj.setName("{#HV.NAME}");
            params.addGroupPrototype(typeObj);

            params.setHost("{#HV.NAME}");

            HostPrototypeCreateResponse response = zabbixApi.hostPrototype().create(request);
            assertNotNull(response);

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

            Integer actualId = response.getResult().getHostids().get(0);
            assertNotNull(actualId);
        } finally {

            dummyLLD.deleteLLDRule(lldRuleID);
        }
    }
}


HostPrototypeDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.hostprototype;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;

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

import com.zabbix4j.hostprototype.HostPrototypeDeleteRequest;
import com.zabbix4j.hostprototype.HostPrototypeDeleteResponse;

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

    public HostPrototypeDeleteTest() {
        super();
    }

    @Test
    public void testDelete1() throws Exception {

        ZabbixApiTestDummyHostPrototype dummy = new ZabbixApiTestDummyHostPrototype(zabbixApi);
        try {
            Integer targetId = dummy.createHost();

            HostPrototypeDeleteRequest request = new HostPrototypeDeleteRequest();
            request.addHostId(targetId);

            HostPrototypeDeleteResponse response = zabbixApi.hostPrototype().delete(request);
            assertNotNull(response);

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

        }
    }
}


HostPrototypeUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.hostprototype;

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

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

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

import com.zabbix4j.hostprototype.GroupPrototypeObject;
import com.zabbix4j.hostprototype.HostPrototypeUpdateRequest;
import com.zabbix4j.hostprototype.HostPrototypeUpdateResponse;

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

    public HostPrototypeUpdateTest() {
        super();
    }

    @Test
    public void testTest1() throws Exception {
        Integer targetId = null;
        ZabbixApiTestDummyHostPrototype dummy = new ZabbixApiTestDummyHostPrototype(zabbixApi);

        try {
            targetId = dummy.createHost();

            HostPrototypeUpdateRequest request = new HostPrototypeUpdateRequest();
            HostPrototypeUpdateRequest.Params params = request.getParams();
            params.setHostid(targetId);
            params.setHost("host.prototype.update");
            params.setName("host prototype update");

            GroupPrototypeObject typeObj = new GroupPrototypeObject();
            typeObj.setName("host prototype update");
            params.addGroupPrototype(typeObj);

            params.setHost("192.168.0.1");

            HostPrototypeUpdateResponse response = zabbixApi.hostPrototype().update(request);
            assertNotNull(response);

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

            Integer actualId = response.getResult().getHostids().get(0);
            assertThat(targetId, Is.is(actualId));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dummy.deleteHostPrototype(targetId);
            fail();
        }
    }
}


ZabbixApiTestDummyHostPrototype
package cn.com.yeexun.testzabbix.zabbix4j.example.hostprototype;

import java.util.Random;

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

import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.hostprototype.GroupLinkObject;
import com.zabbix4j.hostprototype.GroupPrototypeObject;
import com.zabbix4j.hostprototype.HostPrototypeCreateRequest;
import com.zabbix4j.hostprototype.HostPrototypeCreateResponse;
import com.zabbix4j.hostprototype.HostPrototypeDeleteRequest;
import com.zabbix4j.hostprototype.HostPrototypeDeleteResponse;

/**
 * @author Suguru Yajima
 */
public class ZabbixApiTestDummyHostPrototype extends ZabbixApiTestDummyMethodBase {
    private final int TEST_GROUP_ID = 20;
    private ZabbixApiTestBase dummyLLD;
    private Integer lldRuleID = 23796;


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

    public Integer createHost() throws ZabbixApiException {

        try {

            HostPrototypeCreateRequest request = new HostPrototypeCreateRequest();
            HostPrototypeCreateRequest.Params params = request.getParams();
            params.setRuleid(lldRuleID);
            GroupLinkObject linkObj = new GroupLinkObject();
            linkObj.setGroupid(TEST_GROUP_ID);
            params.addGroupLink(linkObj);

            GroupPrototypeObject typeObj = new GroupPrototypeObject();
            typeObj.setName("host prototype name" + new Random().nextInt());
            params.addGroupPrototype(typeObj);

            params.setHost("{#HV.NAME}");

            HostPrototypeCreateResponse response = zabbixApi.hostPrototype().create(request);

            Integer actualId = response.getResult().getHostids().get(0);

            return actualId;
        } finally {

        }
    }

    public void deleteHostPrototype(Integer id) throws ZabbixApiException {
        if (id == null) {
            return;
        }
        HostPrototypeDeleteRequest request = new HostPrototypeDeleteRequest();
        request.addHostId(id);

        HostPrototypeDeleteResponse response = zabbixApi.hostPrototype().delete(request);
    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • Zabbix简介 Zabbix官方网站Zabbix中文文档 本文系统环境是CentOS7x86_64, Zabbi...
    Zhang21阅读 8,075评论 0 37
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,413评论 0 6
  • 慧,是个任劳任怨的母亲,在X小城的一家中心医院上班,是一名护士。 20年前,慧嫁给了那个对他好的男人。当年结婚的时...
    文斌先生阅读 375评论 0 0
  • Linux系统中,有时候普通用户有些事情是不能做的,除非是root用户才能做到。这时就需要临时切换到root身份来...
    StarShift阅读 302评论 0 0