Host group
hostgroup.create: 创建新的主机组
hostgroup.delete: 删除主机组
hostgroup..exists: 检查主机组是否存在
hostgroup..get: 检索主机组
hostgroup..getobjects: 通过过滤器检索主机组
hostgroup.isreadable: 检查主机组是否是可读的
hostgroup.iswritable: 检查主机组是否是可写的
hostgroup..massadd: 将相关对象添加到主机组中
hostgroup.massremove: 从主机组移除相关对象
hostgroup.massupdate: 从主机组中替换或移除相关对象
hostgroup.update: 更新主机组
HostGroupGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.hostgroup;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.hostgroup.HostgroupGetRequest;
import com.zabbix4j.hostgroup.HostgroupGetResponse;
/**
* Created by Suguru Yajima on 2014/05/31.
*/
public class HostGroupGetTest extends ZabbixApiTestBase {
public HostGroupGetTest() {
super();
}
@Test
public void testGet1() throws Exception {
HostgroupGetRequest request = new HostgroupGetRequest();
HostgroupGetRequest.Params params = request.getParams();
params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
params.addHostId(10105);
HostgroupGetResponse response = zabbixApi.hostgroup().get(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
}
}
HostgroupIsWritableTest
package cn.com.yeexun.testzabbix.zabbix4j.example.hostgroup;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.hostgroup.HostgroupGetRequest;
import com.zabbix4j.hostgroup.HostgroupGetResponse;
import com.zabbix4j.hostgroup.HostgroupIsWritableRequest;
import com.zabbix4j.hostgroup.HostgroupIsWritableResponse;
/**
* Created by Suguru Yajima on 2014/04/30.
*/
public class HostgroupIsWritableTest extends ZabbixApiTestBase {
public HostgroupIsWritableTest() {
super();
}
@Test
public void testIsWritable1() throws Exception {
try {
Integer targetId = getHostGroup();
HostgroupIsWritableRequest request = new HostgroupIsWritableRequest();
request.setParams(new ArrayList(Arrays.asList(new Integer[]{targetId})));
HostgroupIsWritableResponse response = zabbixApi.hostgroup().isWritable(request);
assertNotNull(response);
assertTrue(response.isResult());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
private Integer getHostGroup() throws ZabbixApiException {
HostgroupGetRequest request = new HostgroupGetRequest();
HostgroupGetRequest.Params params = request.getParams();
params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
HostgroupGetResponse getResponse = zabbixApi.hostgroup().get(request);
int targetGroupId = getResponse.getResult().get(0).getGroupid();
return targetGroupId;
}
@Test
public void testIsWritable2() throws Exception {
try {
HostgroupGetRequest getRequest = new HostgroupGetRequest();
ArrayList name = new ArrayList();
name.add("test_readable_writable_hostgroup");
getRequest.getParams().getFilter().setName(name);
HostgroupGetResponse getResponse = zabbixApi.hostgroup().get(getRequest);
int targetGroupId = getResponse.getResult().get(0).getGroupid();
// login as guest
login("guest", "");
HostgroupIsWritableRequest request = new HostgroupIsWritableRequest();
request.setParams(new ArrayList(Arrays.asList(new Integer[]{targetGroupId})));
HostgroupIsWritableResponse response = zabbixApi.hostgroup().isWritable(request);
assertNotNull(response);
assertFalse(response.isResult());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
}
HostgroupTest
/*
* 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.hostgroup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.hostgroup.HostgroupCreateRequest;
import com.zabbix4j.hostgroup.HostgroupCreateResponse;
import com.zabbix4j.hostgroup.HostgroupDeleteRequest;
import com.zabbix4j.hostgroup.HostgroupDeleteResponse;
import com.zabbix4j.hostgroup.HostgroupExistRequest;
import com.zabbix4j.hostgroup.HostgroupExistResponse;
import com.zabbix4j.hostgroup.HostgroupGetRequest;
import com.zabbix4j.hostgroup.HostgroupGetResponse;
import com.zabbix4j.hostgroup.HostgroupIsReadableRequest;
import com.zabbix4j.hostgroup.HostgroupIsReadableResponse;
import com.zabbix4j.hostgroup.HostgroupUpdateRequest;
import com.zabbix4j.hostgroup.HostgroupUpdateResponse;
/**
* Created by Suguru Yajima on 2014/04/26.
*/
public class HostgroupTest extends ZabbixApiTestBase {
private HostgroupCreateResponse hostgroupCreateResponse;
public HostgroupTest() {
super();
}
@Test
public void testGet1() {
try {
HostgroupGetRequest request = new HostgroupGetRequest();
HostgroupGetResponse response = zabbixApi.hostgroup().get(request);
assertNotNull(response);
String groupName = response.getResult().get(0).getName();
assertEquals("Templates", groupName);
} catch (ZabbixApiException e) {
fail();
}
}
@Test
public void testExist1() {
HostgroupExistRequest request = new HostgroupExistRequest();
ArrayList names = new ArrayList(Arrays.asList(new String[]{"Templates"}));
request.getParams().setName(names);
try {
HostgroupExistResponse response = zabbixApi.hostgroup().exist(request);
assertNotNull(response);
assertTrue(response.isResult());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
@Test
public void testExist2() {
HostgroupExistRequest request = new HostgroupExistRequest();
ArrayList names = new ArrayList(Arrays.asList(new String[]{"xxxxx"}));
request.getParams().setName(names);
try {
HostgroupExistResponse response = zabbixApi.hostgroup().exist(request);
assertNotNull(response);
assertFalse(response.isResult());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
@Test
public void testHostgroup() {
testCreate1();
testUpdate1();
testDelete1();
}
public void testCreate1() {
String name = "test_hostgroup_create1";
HostgroupCreateRequest request = new HostgroupCreateRequest();
request.getParams().setName(name);
try {
hostgroupCreateResponse = zabbixApi.hostgroup().create(request);
assertNotNull(hostgroupCreateResponse);
assertNotNull(hostgroupCreateResponse.getResult().getGroupids());
assertTrue(0 < hostgroupCreateResponse.getResult().getGroupids().size());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
public void testUpdate1() {
int targetGroupid = this.hostgroupCreateResponse.getResult().getGroupids().get(0);
String name = "test_hostgroup_update1";
HostgroupUpdateRequest request = new HostgroupUpdateRequest();
request.getParams().setName(name);
request.getParams().setGroupid(targetGroupid);
try {
HostgroupUpdateResponse response = zabbixApi.hostgroup().update(request);
assertNotNull(response);
assertNotNull(response.getResult().getGroupids());
int groupId = response.getResult().getGroupids().get(0);
assertEquals(targetGroupid, groupId);
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
public void testDelete1() {
int targetGroupid = this.hostgroupCreateResponse.getResult().getGroupids().get(0);
HostgroupDeleteRequest request = new HostgroupDeleteRequest();
request.setParams(new ArrayList(Arrays.asList(new Integer[]{targetGroupid})));
try {
HostgroupDeleteResponse response = zabbixApi.hostgroup().delete(request);
assertNotNull(response);
assertNotNull(response.getResult().getGroupids());
int groupId = response.getResult().getGroupids().get(0);
assertEquals(targetGroupid, groupId);
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
@Test
public void testIsReable1() {
int targetGroupid = 12;
HostgroupIsReadableRequest request = new HostgroupIsReadableRequest();
request.setParams(new ArrayList(Arrays.asList(new Integer[]{targetGroupid})));
try {
HostgroupIsReadableResponse response = zabbixApi.hostgroup().isReable(request);
assertNotNull(response);
assertTrue(response.isResult());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
@Test
public void testIsReable2() {
// login as guest user
login("guest", "");
int targetGroupid = 12;
HostgroupIsReadableRequest request = new HostgroupIsReadableRequest();
request.setParams(new ArrayList(Arrays.asList(new Integer[]{targetGroupid})));
try {
HostgroupIsReadableResponse response = zabbixApi.hostgroup().isReable(request);
assertNotNull(response);
assertFalse(response.isResult());
} catch (ZabbixApiException e) {
fail(e.getMessage());
}
}
}