美文网首页
zabbixApi4j-Trigger

zabbixApi4j-Trigger

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

Trigger

trigger.addependencies: 添加新的触发器依赖项
trigger.create: 创建新的触发器
trigger.delete: 删除触发器
trigger.deletedependencies: 删除触发器依赖
trigger.exists: 检查触发器是否存在
trigger.get: 检索触发器
trigger.getobjects: 通过过滤器检索触发器
trigger.isreadable: 检查触发器是否是可读的
trigger.iswritable: 检查触发器是否是可写的
trigger.update: 更新触发器

image.png
TriggerCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.trigger;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;

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

import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.trigger.TriggerCreateRequest;
import com.zabbix4j.trigger.TriggerCreateResponse;
import com.zabbix4j.trigger.TriggerDeleteRequest;
import com.zabbix4j.trigger.TriggerDeleteResponse;

/**
 * Created by Suguru Yajima on 2014/05/10.
 */
public class TriggerCreateTest extends ZabbixApiTestBase {
    public TriggerCreateTest() {
        super();
    }

    @Test
    public void testCreate1() throws Exception {

        TriggerCreateRequest request = new TriggerCreateRequest();
        TriggerCreateRequest.Params params = request.getParams();
        params.setComments("trigger create comment");
        params.setDescription("triggger create description");
        params.setExpression("{test host created1:agent.ping.last()}=1");

        TriggerCreateResponse response = zabbixApi.trigger().create(request);

        assertNotNull(response);
        Integer triggerId = response.getResult().getTriggerids().get(0);
        assertNotNull(triggerId);

        deleteTestTrigger(triggerId);
    }

    private void deleteTestTrigger(Integer triggerid) throws ZabbixApiException {

        TriggerDeleteRequest request = new TriggerDeleteRequest();
        request.getParams().add(triggerid);

        TriggerDeleteResponse response = zabbixApi.trigger().delete(request);
    }
}

TriggerDeteleTest
package cn.com.yeexun.testzabbix.zabbix4j.example.trigger;

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.ZabbixApiException;
import com.zabbix4j.trigger.TriggerCreateRequest;
import com.zabbix4j.trigger.TriggerCreateResponse;
import com.zabbix4j.trigger.TriggerDeleteRequest;
import com.zabbix4j.trigger.TriggerDeleteResponse;

/**
 * Created by Suguru Yajima on 2014/05/12.
 */
public class TriggerDeteleTest extends ZabbixApiTestBase {

    public TriggerDeteleTest() {
        super();
    }

    @Test
    public void testDelete1() throws Exception {

        Integer expectedId = createDummyTrigger();

        TriggerDeleteRequest request = new TriggerDeleteRequest();
        request.getParams().add(expectedId);

        TriggerDeleteResponse response = zabbixApi.trigger().delete(request);
        assertNotNull(response);

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

        assertEquals(expectedId, actualId);
    }

    private Integer createDummyTrigger() throws ZabbixApiException {

        TriggerCreateRequest request = new TriggerCreateRequest();
        TriggerCreateRequest.Params params = request.getParams();
        params.setComments("trigger delete comment");
        params.setDescription("triggger delete description");
        params.setExpression("{test host created1:vm.memory.size[total].last()}=0");

        TriggerCreateResponse response = zabbixApi.trigger().create(request);

        Integer triggerId = response.getResult().getTriggerids().get(0);

        return triggerId;
    }
}

TriggerGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.trigger;

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.ZabbixApiException;
import com.zabbix4j.trigger.TriggerCreateRequest;
import com.zabbix4j.trigger.TriggerCreateResponse;
import com.zabbix4j.trigger.TriggerDeleteRequest;
import com.zabbix4j.trigger.TriggerDeleteResponse;
import com.zabbix4j.trigger.TriggerGetRequest;
import com.zabbix4j.trigger.TriggerGetResponse;

/**
 * Created by Suguru Yajima on 2014/05/13.
 */
public class TriggerGetTest extends ZabbixApiTestBase {

    public TriggerGetTest() {
        super();
    }

    @Test
    public void testGet1() throws Exception {

        Integer triggerId = createDummyTrigger();

        TriggerGetRequest request = new TriggerGetRequest();
        TriggerGetRequest.Params params = request.getParams();
        params.addTriggerid(triggerId);

        TriggerGetResponse response = zabbixApi.trigger().get(request);

        deleteDummyTrigger(triggerId);

        assertNotNull(response);

        TriggerGetResponse.Result result = response.getResult().get(0);

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

        String comment = result.getComments();

        assertEquals("trigger get comment", comment);
    }

    private Integer createDummyTrigger() throws ZabbixApiException {

        TriggerCreateRequest request = new TriggerCreateRequest();
        TriggerCreateRequest.Params params = request.getParams();
        params.setComments("trigger get comment");
        params.setDescription("triggger get description");
        params.setExpression("{test host created1:system.swap.size[,total].last()}=0");

        TriggerCreateResponse response = zabbixApi.trigger().create(request);

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

    private void deleteDummyTrigger(Integer triggerid) throws ZabbixApiException {

        TriggerDeleteRequest request = new TriggerDeleteRequest();
        request.getParams().add(triggerid);

        TriggerDeleteResponse response = zabbixApi.trigger().delete(request);
    }
}

TriggerUpdateTest
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Suguru Yajima
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.com.yeexun.testzabbix.zabbix4j.example.trigger;

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.ZabbixApiException;
import com.zabbix4j.trigger.TriggerCreateRequest;
import com.zabbix4j.trigger.TriggerCreateResponse;
import com.zabbix4j.trigger.TriggerUpdateRequest;
import com.zabbix4j.trigger.TriggerUpdateResponse;

/**
 * Created by Suguru Yajima on 2014/05/12.
 */
public class TriggerUpdateTest extends ZabbixApiTestBase {

    public TriggerUpdateTest() {
        super();
    }

    @Test
    public void testUpdate1() throws Exception {

        Integer expectedid = createDummyTrigger();

        TriggerUpdateRequest request = new TriggerUpdateRequest();
        TriggerUpdateRequest.Params params = request.getParams();
        params.setTriggerid(expectedid);
        params.setDescription("Trigger Updated");
        params.setComments("Trigger Update Test");

        TriggerUpdateResponse response = zabbixApi.trigger().update(request);

        assertNotNull(response);

        Integer actualid = response.getResult().getTriggerids().get(0);
        assertEquals(expectedid, actualid);
    }

    private Integer createDummyTrigger() throws ZabbixApiException {

        TriggerCreateRequest request = new TriggerCreateRequest();
        TriggerCreateRequest.Params params = request.getParams();
        params.setComments("trigger udpate comment");
        params.setDescription("triggger udpate description");
        params.setExpression("{test host created1:vm.memory.size[available].last()}>0");

        TriggerCreateResponse response = zabbixApi.trigger().create(request);

        Integer triggerId = response.getResult().getTriggerids().get(0);

        return triggerId;
    }
}

相关文章

网友评论

      本文标题:zabbixApi4j-Trigger

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