Discovery rule: 此类设计用于处理网络发现规则。
drule.create: 创建新的发现规则
drule.delete: 删除发现规则
drule.exists: 检查发现规则是否存在
drule.get: 检索发现规则
drule.isreadable: 检查发现规则是否可读
drule.iswritable: 检查发现规则是否可写
drule.update: 更新发现规则
DRuleCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.discoveryrule;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.discoveryrule.DCheck;
import com.zabbix4j.discoveryrule.DRuleCreateRequest;
import com.zabbix4j.discoveryrule.DRuleCreateResponse;
/**
* Created by Suguru Yajima on 2014/05/26.
*/
public class DRuleCreateTest extends ZabbixApiTestBase {
public DRuleCreateTest() {
super();
}
@Test
public void testCreate1() throws Exception {
DRuleCreateRequest request = new DRuleCreateRequest();
DRuleCreateRequest.Params params = request.getParams();
params.setIprange("127.0.0.1");
params.setName("dicovery test at localhost");
DCheck dcheck = new DCheck();
dcheck.setKey_("key_");
dcheck.setPorts(10050);
dcheck.setType(9);
dcheck.setUniq(0);
params.addCheck(dcheck);
DRuleCreateResponse response = zabbixApi.discoveryRule().create(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
}
}
DRuleDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.discoveryrule;
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.discoveryrule.DCheck;
import com.zabbix4j.discoveryrule.DRuleCreateRequest;
import com.zabbix4j.discoveryrule.DRuleCreateResponse;
import com.zabbix4j.discoveryrule.DRuleDeleteRequest;
import com.zabbix4j.discoveryrule.DRuleDeleteResponse;
/**
* Created by Suguru Yajima on 2014/05/26.
*/
public class DRuleDeleteTest extends ZabbixApiTestBase {
public DRuleDeleteTest() {
super();
}
@Test
public void testDelete1() throws Exception {
Integer druleId = createDummy();
DRuleDeleteRequest request = new DRuleDeleteRequest();
request.addDRuleId(druleId);
DRuleDeleteResponse response = zabbixApi.discoveryRule().delete(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
Integer acutalId = response.getResult().getDruleids().get(0);
assertEquals(druleId, acutalId);
}
private Integer createDummy() throws ZabbixApiException {
DRuleCreateRequest request = new DRuleCreateRequest();
DRuleCreateRequest.Params params = request.getParams();
params.setIprange("127.0.0.1");
params.setName("dicovery test at localhost");
DCheck dcheck = new DCheck();
dcheck.setKey_("key_");
dcheck.setPorts(10050);
dcheck.setType(9);
dcheck.setUniq(0);
params.addCheck(dcheck);
DRuleCreateResponse response = zabbixApi.discoveryRule().create(request);
return response.getResult().getDruleids().get(0);
}
}
DRuleGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.discoveryrule;
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.discoveryrule.DCheck;
import com.zabbix4j.discoveryrule.DRuleCreateRequest;
import com.zabbix4j.discoveryrule.DRuleCreateResponse;
import com.zabbix4j.discoveryrule.DRuleDeleteRequest;
import com.zabbix4j.discoveryrule.DRuleDeleteResponse;
import com.zabbix4j.discoveryrule.DRuleGetRequest;
import com.zabbix4j.discoveryrule.DRuleGetResponse;
import com.zabbix4j.discoveryrule.DiscoveryRuleObject;
/**
* Created by Suguru Yajima on 2014/05/27.
*/
public class DRuleGetTest extends ZabbixApiTestBase {
public DRuleGetTest() {
super();
}
@Test
public void testGet1() throws Exception {
Integer targetId = createDummy();
DRuleGetRequest request = new DRuleGetRequest();
DRuleGetRequest.Params params = request.getParams();
params.addDRuleId(targetId);
DRuleGetResponse response = zabbixApi.discoveryRule().get(request);
assertNotNull(response);
deleteDummy(targetId);
DiscoveryRuleObject ruleObject = response.getResult().get(0);
assertNotNull(ruleObject);
assertEquals("dicovery get test at localhost", ruleObject.getName());
assertEquals("127.0.0.1", ruleObject.getIprange());
}
private Integer createDummy() throws ZabbixApiException {
DRuleCreateRequest request = new DRuleCreateRequest();
DRuleCreateRequest.Params params = request.getParams();
params.setIprange("127.0.0.1");
params.setName("dicovery get test at localhost");
DCheck dcheck = new DCheck();
dcheck.setKey_("key_");
dcheck.setPorts(10050);
dcheck.setType(9);
dcheck.setUniq(0);
params.addCheck(dcheck);
DRuleCreateResponse response = zabbixApi.discoveryRule().create(request);
return response.getResult().getDruleids().get(0);
}
private void deleteDummy(Integer id) throws ZabbixApiException {
DRuleDeleteRequest request = new DRuleDeleteRequest();
request.addDRuleId(id);
DRuleDeleteResponse response = zabbixApi.discoveryRule().delete(request);
}
}
DRuleUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.discoveryrule;
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.discoveryrule.DCheck;
import com.zabbix4j.discoveryrule.DRuleCreateRequest;
import com.zabbix4j.discoveryrule.DRuleCreateResponse;
import com.zabbix4j.discoveryrule.DRuleDeleteRequest;
import com.zabbix4j.discoveryrule.DRuleDeleteResponse;
import com.zabbix4j.discoveryrule.DRuleUpdateRequest;
import com.zabbix4j.discoveryrule.DRuleUpdateResponse;
/**
* Created by Suguru Yajima on 2014/05/27.
*/
public class DRuleUpdateTest extends ZabbixApiTestBase {
public DRuleUpdateTest() {
super();
}
@Test
public void testUpdate1() throws Exception {
Integer targetId = createDummy();
DRuleUpdateRequest request = new DRuleUpdateRequest();
DRuleUpdateRequest.Params params = request.getParams();
params.setDruleid(targetId);
params.setName("update success");
params.setIprange("192.168.0.1");
DCheck dcheck = new DCheck();
dcheck.setKey_("key_");
dcheck.setPorts(10051);
dcheck.setType(9);
dcheck.setUniq(0);
params.addCheck(dcheck);
DRuleUpdateResponse response = zabbixApi.discoveryRule().update(request);
assertNotNull(response);
deleteDummy(targetId);
Integer actualId = response.getResult().getDruleids().get(0);
assertEquals(targetId, actualId);
}
private Integer createDummy() throws ZabbixApiException {
DRuleCreateRequest request = new DRuleCreateRequest();
DRuleCreateRequest.Params params = request.getParams();
params.setIprange("127.0.0.1");
params.setName("dicovery update test at localhost");
DCheck dcheck = new DCheck();
dcheck.setKey_("key_");
dcheck.setPorts(10050);
dcheck.setType(9);
dcheck.setUniq(0);
params.addCheck(dcheck);
DRuleCreateResponse response = zabbixApi.discoveryRule().create(request);
return response.getResult().getDruleids().get(0);
}
private void deleteDummy(Integer id) throws ZabbixApiException {
DRuleDeleteRequest request = new DRuleDeleteRequest();
request.addDRuleId(id);
DRuleDeleteResponse response = zabbixApi.discoveryRule().delete(request);
}
}