Script
script.create: 创建新脚本
script.delete: 删除脚本
script.execute: 执行脚本
script.get: 检索脚本
script.getscriptsbyhosts: 为主机检索脚本
script.update: 更新脚本
DummyScript
package cn.com.yeexun.testzabbix.zabbix4j.example.script;
import java.util.Date;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestDummyMethodBase;
import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.script.ScriptCreateRequest;
import com.zabbix4j.script.ScriptCreateResponse;
import com.zabbix4j.script.ScriptDeleteRequest;
import com.zabbix4j.script.ScriptDeleteResponse;
import com.zabbix4j.script.ScriptObject;
/**
* @author Suguru Yajima
*/
public class DummyScript extends ZabbixApiTestDummyMethodBase {
public DummyScript(ZabbixApi zabbixApi) {
super(zabbixApi);
}
public Integer create() throws ZabbixApiException {
ScriptCreateRequest request = new ScriptCreateRequest();
ScriptObject obj = new ScriptObject();
obj.setName("test.script.create." + new Date().getTime());
obj.setCommand("reboot server 1");
obj.setHost_access(ScriptObject.HOST_PERMISSION.WRITE.value);
obj.setConfirmation("Are you sure you would like to reboot the server?");
request.addScriptObject(obj);
ScriptCreateResponse response = zabbixApi.script().create(request);
Integer actualId = response.getResult().getScriptids().get(0);
return actualId;
}
public void delete(Integer targetId) throws ZabbixApiException {
ScriptDeleteRequest request = new ScriptDeleteRequest();
request.addScriptId(targetId);
ScriptDeleteResponse response = zabbixApi.script().delete(request);
}
}
ScriptCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.script;
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.script.ScriptCreateRequest;
import com.zabbix4j.script.ScriptCreateResponse;
import com.zabbix4j.script.ScriptObject;
/**
* @author Suguru Yajima
*/
public class ScriptCreateTest extends ZabbixApiTestBase {
public ScriptCreateTest() {
super();
}
@Test
public void testCreate() throws Exception {
ScriptCreateRequest request = new ScriptCreateRequest();
ScriptObject obj = new ScriptObject();
obj.setName("test.script.create." + new Date().getTime());
obj.setCommand("reboot server 1");
obj.setHost_access(ScriptObject.HOST_PERMISSION.WRITE.value);
obj.setConfirmation("Are you sure you would like to reboot the server?");
request.addScriptObject(obj);
ScriptCreateResponse response = zabbixApi.script().create(request);
assertNotNull(response);
Integer actualId = response.getResult().getScriptids().get(0);
assertNotNull(actualId);
}
}
ScriptDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.script;
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.script.ScriptDeleteRequest;
import com.zabbix4j.script.ScriptDeleteResponse;
/**
* @author Suguru Yajima
*/
public class ScriptDeleteTest extends ZabbixApiTestBase {
public ScriptDeleteTest() {
super();
}
@Test
public void testDelete() throws Exception {
DummyScript dummyScript = new DummyScript(zabbixApi);
Integer targetId = dummyScript.create();
ScriptDeleteRequest request = new ScriptDeleteRequest();
request.addScriptId(targetId);
ScriptDeleteResponse response = zabbixApi.script().delete(request);
assertNotNull(response);
Integer actualId = response.getResult().getScriptids().get(0);
assertThat(actualId, is(targetId));
}
}
ScriptGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.script;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.hamcrest.core.Is;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.script.ScriptGetRequest;
import com.zabbix4j.script.ScriptGetResponse;
/**
* @author Suguru Yajima
*/
public class ScriptGetTest extends ZabbixApiTestBase {
public ScriptGetTest() {
super();
}
@Test
public void testGet() throws Exception {
final Integer targetId = 7;
ScriptGetRequest request = new ScriptGetRequest();
ScriptGetRequest.Params params = request.getParams();
params.addScriptId(targetId);
params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());
params.setSelectGroups(ZabbixApiParamter.QUERY.extend.name());
ScriptGetResponse response = zabbixApi.script().get(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
ScriptGetResponse.Result result = response.getResult().get(0);
assertThat(result.getScriptid(), Is.is(targetId));
assertThat(result.getDescription(), Is.is("test.script.get"));
}
}
ScriptUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.script;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.hamcrest.core.Is;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.script.ScriptObject;
import com.zabbix4j.script.ScriptUpdateRequest;
import com.zabbix4j.script.ScriptUpdateResponse;
/**
* @author Suguru Yajima
*/
public class ScriptUpdateTest extends ZabbixApiTestBase {
public ScriptUpdateTest() {
super();
}
@Test
public void testUpdate() throws Exception {
DummyScript dummyScript = new DummyScript(zabbixApi);
Integer targetId = dummyScript.create();
try {
ScriptUpdateRequest request = new ScriptUpdateRequest();
ScriptObject obj = new ScriptObject();
obj.setScriptid(targetId);
obj.setCommand("/bin/ping -c 10 {HOST.CONN} 2>&1");
request.addScriptObjrct(obj);
ScriptUpdateResponse response = zabbixApi.script().update(request);
assertNotNull(response);
Integer actualId = response.getResult().getScriptids().get(0);
assertThat(actualId, Is.is(targetId));
} finally {
dummyScript.delete(targetId);
}
}
}