zabbixApi4j-Item

Item

item.create: 创建新项目
item.delete: 删除项目
item.exists: 检查项目是否存在
item.get: 检索项目
item.getobjects: 通过过滤器检索项目
item.isreadable: 检查项目是否是可读的
item.iswritable: 检查项目是否是可写的
item.update: 更新项目

zabbixApi4j-Item_第1张图片
image.png


ItemCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.item;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;

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

import com.google.gson.Gson;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.item.ItemCreateRequest;
import com.zabbix4j.item.ItemCreateResponse;

/**
 * Created by Suguru Yajima on 2014/05/07.
 */
public class ItemCreateTest extends ZabbixApiTestBase {

    public ItemCreateTest() {
        super();
    }

    @Test
    public void testCreate1() throws Exception {

        ItemCreateRequest request = new ItemCreateRequest();
        ItemCreateRequest.Params params = request.getParams();
        params.setName("Test Item");
        params.setDelay(60);
        params.setHostid(10109);
        params.setKey_("agent.hostname");
        params.setValue_type(ZabbixApiParamter.ITEM_VALUE_TYPE.CHARACTOR.value);
        params.setType(0);
        params.setInterfaceid(6);

        ItemCreateResponse response = zabbixApi.item().create(request);

        Gson gson = getGson();
        logger.debug(gson.toJson(response));

        assertNotNull(response);

        assertNotNull(response.getResult().getItemids());

        assertNotNull(response.getResult().getItemids().get(0));
    }
}


ItemDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.item;

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.ZabbixApiException;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.item.ItemCreateRequest;
import com.zabbix4j.item.ItemCreateResponse;
import com.zabbix4j.item.ItemDeleteRequest;
import com.zabbix4j.item.ItemDeleteResponse;

/**
 * Created by Suguru Yajima on 2014/05/09.
 */
public class ItemDeleteTest extends ZabbixApiTestBase {

    public ItemDeleteTest() {
        super();
    }

    @Test
    public void testDelete1() throws Exception {

        Integer expectedItemid = createItem();
        ItemDeleteRequest request = new ItemDeleteRequest();
        ArrayList itemids = new ArrayList();
        itemids.add(expectedItemid);
        request.setParams(itemids);

        ItemDeleteResponse response = zabbixApi.item().delete(request);

        assertNotNull(response);

        Integer actualItemid = response.getResult().getItemids().get(0);

        assertEquals(expectedItemid, actualItemid);
    }

    private Integer createItem() throws ZabbixApiException {

        ItemCreateRequest request = new ItemCreateRequest();
        ItemCreateRequest.Params params = request.getParams();
        params.setName("Test Item for Delete");
        params.setDelay(60);
        params.setHostid(10109);
        params.setKey_("agent.ping");
        params.setValue_type(ZabbixApiParamter.ITEM_VALUE_TYPE.CHARACTOR.value);
        params.setType(0);
        params.setInterfaceid(6);

        ItemCreateResponse response = zabbixApi.item().create(request);

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


ItemGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.item;

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

import org.junit.Test;

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

import com.google.gson.Gson;
import com.zabbix4j.item.ItemGetRequest;
import com.zabbix4j.item.ItemGetResponse;

/**
 * Created by Suguru Yajima on 2014/05/02.
 */
public class ItemGetTest extends ZabbixApiTestBase {

    public ItemGetTest() {
        super();
    }

    @Test
    public void testGet1() throws Exception {

        Integer itemId = 23667;

        ItemGetRequest request = new ItemGetRequest();
        ItemGetRequest.Params params = request.getParams();
        params.addItemId(itemId);

        ItemGetResponse response = zabbixApi.item().get(request);

        Gson gson = getGson();
        logger.debug(gson.toJson(response));

        assertNotNull(response);

        ItemGetResponse.Result result = response.getResult().get(0);
        assertNotNull(result);

        assertEquals(itemId, result.getItemid());
    }
}


ItemUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.item;

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

import org.junit.Test;

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

import com.zabbix4j.item.ItemUpdateRequest;
import com.zabbix4j.item.ItemUpdateResponse;

/**
 * Created by Suguru Yajima on 2014/05/09.
 */
public class ItemUpdateTest extends ZabbixApiTestBase {


    public ItemUpdateTest() {
        super();
    }

    @Test
    public void testUpdate1() throws Exception {

        Integer itemid = 23674;
        ItemUpdateRequest request = new ItemUpdateRequest();
        ItemUpdateRequest.Params params = request.getParams();
        params.setItemid(itemid);
        params.setName("Test Item Updated");
        params.setHistory(7);
        params.setTrends(365);

        ItemUpdateResponse response = zabbixApi.item().update(request);

        assertNotNull(response);
        Integer actualItemId = response.getResult().getItemids().get(0);
        assertEquals(itemid, actualItemId);
    }
}

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