zabbixApi4j-IT service

IT service

service.addependencies: 在IT服务之间添加依赖项
service.addtimes: 添加服务时间
service.create: 创建新的IT服务
service.delete: 删除IT服务
service.deletedependencies: 删除IT服务之间的依赖关系
service.deletetimes: 删除服务时间
service.get: 检索IT服务
service.isreadable: 检查IT服务是否是可读的
service.iswritable: 检查IT服务是否是可写的
service.update: 更新IT服务

zabbixApi4j-IT service_第1张图片
image.png


DummyITService
package cn.com.yeexun.testzabbix.zabbix4j.example.itservice;

import java.util.Date;

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

import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.itservice.ITServiceCreateRequest;
import com.zabbix4j.itservice.ITServiceCreateResponse;
import com.zabbix4j.itservice.ITServiceDeleteRequest;
import com.zabbix4j.itservice.ITServiceDeleteResponse;
import com.zabbix4j.itservice.ITServiceObject;

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

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

    public Integer create() throws ZabbixApiException {

        ITServiceCreateRequest request = new ITServiceCreateRequest();
        ITServiceCreateRequest.Params params = request.getParams();
        params.setName("test.itservice.create." + new Date().getTime());
        params.setAlgorithm(ITServiceObject.ALGORITHM.LEAST_ONE_CHILD_PROBLEM.value);
        params.setShowsla(ITServiceObject.SLA.CALCULATE.value);
        params.setGoodsla(99.99f);
        params.setSortorder(1);

        ITServiceCreateResponse response = zabbixApi.itservice().create(request);

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

        return actualId;
    }

    public void delete(Integer targetId) throws ZabbixApiException {

        ITServiceDeleteRequest request = new ITServiceDeleteRequest();
        request.addServiceId(targetId);

        ITServiceDeleteResponse response = zabbixApi.itservice().delete(request);
    }
}


ITServiceCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.itservice;

import static org.junit.Assert.assertNotNull;

import java.util.Date;

import org.junit.Test;

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

import com.zabbix4j.itservice.ITServiceCreateRequest;
import com.zabbix4j.itservice.ITServiceCreateResponse;
import com.zabbix4j.itservice.ITServiceObject;

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


    public ITServiceCreateTest() {
        super();
    }

    @Test
    public void testCreate1() throws Exception {
        ITServiceCreateRequest request = new ITServiceCreateRequest();
        ITServiceCreateRequest.Params params = request.getParams();
        params.setName("test.itservice.create." + new Date().getTime());
        params.setAlgorithm(ITServiceObject.ALGORITHM.LEAST_ONE_CHILD_PROBLEM.value);
        params.setShowsla(ITServiceObject.SLA.CALCULATE.value);
        params.setGoodsla(99.99f);
        params.setSortorder(1);

        ITServiceCreateResponse response = zabbixApi.itservice().create(request);
        assertNotNull(response);

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

        Integer actualId = response.getResult().getServiceids().get(0);
        assertNotNull(actualId);
    }
}


ITServiceDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.itservice;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;

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

import com.zabbix4j.itservice.ITServiceDeleteRequest;
import com.zabbix4j.itservice.ITServiceDeleteResponse;

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

    public ITServiceDeleteTest() {
        super();
    }

    @Test
    public void testDelete1() throws Exception {

        DummyITService dummy = new DummyITService(zabbixApi);
        Integer targetId = dummy.create();

        ITServiceDeleteRequest request = new ITServiceDeleteRequest();
        request.addServiceId(targetId);

        ITServiceDeleteResponse response = zabbixApi.itservice().delete(request);
        assertNotNull(response);

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

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

        assertThat(targetId, is(actualId));
    }
}


ITServiceGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.itservice;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;

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

import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.itservice.ITServiceGetRequest;
import com.zabbix4j.itservice.ITServiceGetResponse;

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

    public ITServiceGetTest() {
        super();
    }

    @Test
    public void testGet() throws Exception {

        // thsi service has parent and child serivce
        final Integer serviceId = 6;

        ITServiceGetRequest request = new ITServiceGetRequest();
        ITServiceGetRequest.Params params = request.getParams();
        params.addSerivceId(serviceId);

        params.setSelectAlarms(ZabbixApiParamter.QUERY.extend.name());
        params.setSelectDependencies(ZabbixApiParamter.QUERY.extend.name());
        params.setSelectParent(ZabbixApiParamter.QUERY.extend.name());
        params.setSelectParentDependencies(ZabbixApiParamter.QUERY.extend.name());
        params.setSelectTimes(ZabbixApiParamter.QUERY.extend.name());
        params.setSelectTrigger(ZabbixApiParamter.QUERY.extend.name());


        ITServiceGetResponse response = zabbixApi.itservice().get(request);
        assertNotNull(response);

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

        ITServiceGetResponse.Result result = response.getResult().get(0);
        Integer actualId = result.getServiceid();

        assertThat(serviceId, is(actualId));
    }
}


ITServiceUpdateTest
  package cn.com.yeexun.testzabbix.zabbix4j.example.itservice;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;

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

import com.zabbix4j.itservice.ITServiceObject;
import com.zabbix4j.itservice.ITServiceUpdateRequest;
import com.zabbix4j.itservice.ITServiceUpdateResponse;

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

    public ITServiceUpdateTest() {
        super();
    }

    @Test
    public void testUpdate1() throws Exception {

        DummyITService dummy = new DummyITService(zabbixApi);
        Integer targetId = dummy.create();

        try {
            ITServiceUpdateRequest request = new ITServiceUpdateRequest();
            ITServiceUpdateRequest.Params params = request.getParams();
            params.setServiceid(targetId);
            params.setName("updated");
            params.setAlgorithm(ITServiceObject.ALGORITHM.ALL_CHILDREN_PROBLEM.value);
            params.setShowsla(ITServiceObject.SLA.DO_NOT_CALCULATE.value);
            params.setGoodsla(50.00f);
            params.setSortorder(2);

            ITServiceUpdateResponse response = zabbixApi.itservice().update(request);
            assertNotNull(response);

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

            Integer actualId = response.getResult().getServiceids().get(0);
            assertThat(targetId, is(actualId));
        } finally {
            dummy.delete(targetId);
        }
    }
}

你可能感兴趣的:(zabbixApi4j-IT service)