zabbix的Java API(一)
import static org.junit.Assert.*;import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import com.zabbix4j.host.HostCreateRequest;
import com.zabbix4j.host.HostCreateResponse;
import com.zabbix4j.hostinteface.HostInterfaceObject;
import com.zabbix4j.usermacro.Macro;
public>
public testCreateHost(){
super();
}
@Test
public void createHost(){
// 这两个变量数据库中必须存在(我第一次就在这儿捣鼓了半天,以为创建主机就什么都是新的,
// 这是对zabbix本身的不理解导致的)
final Integer groupId = 4;
final Integer templateId = 10081;
try {
HostCreateRequest request = new HostCreateRequest();
HostCreateRequest.Params params = request.getParams();
params.addTemplateId(templateId);
params.addGroupId(groupId);
//添加宏
List<Macro> macros = new ArrayList<Macro>();
Macro macro1 = new Macro();
macro1.setMacro("{$MACRO1}");
macro1.setValue("value1");
macros.add(macro1);
params.setMacros(macros);
//添加主机接口
HostInterfaceObject hostInterface = new HostInterfaceObject();
hostInterface.setIp("192.168.106.133");
params.addHostInterfaceObject(hostInterface);
//添加主机,和主机名
params.setHost("test host created1." + new Date().getTime());
params.setName("test host created1 name" + new Date().getTime());
HostCreateResponse response = zabbixApi.host().create(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
} catch (Exception e) {
fail(e.getMessage());
}
}
}
页:
[1]