michaelquigley / zabbixj

A simple Zabbix framework for Java.
Apache License 2.0
43 stars 28 forks source link

can not delete through api #10

Closed strongerfly closed 7 years ago

strongerfly commented 7 years ago

API Error : {"message":"Application error.","data":"No permissions to referred object or it does not exist!","code":-32500} Request:{ "params": [ { "hostid": 10148 } ], "id": 1497096455, "jsonrpc": "2.0", "method": "host.delete", "auth": "bd7be9afa12f7a5e5e7eee08ac92e66f" }

michaelquigley commented 7 years ago

Can you verify your permissions and the id for your host?

I have an application that runs continuously, which does that exact same host.delete API call, and it works reliably.

michaelquigley commented 7 years ago

Here's a code snippet from the application that makes the host.delete API call.

    ZabbixApi zabbixApi = new ZabbixApi(preferences.getUrl());
    if(!zabbixApi.login(preferences.getUsername(), preferences.getPassword())) {
        throw new IllegalStateException("Zabbix API login failed.");
    }

    Map<String, Object> filter = new HashMap<>();
    filter.put("ip", host.getHostAddress());
    ParameterMapRequest hostInterfaceRequest = new ParameterMapRequest("hostinterface.get");
    hostInterfaceRequest.addParam("filter", filter);
    JSONObject response = zabbixApi.call(hostInterfaceRequest);
    if(response.getJSONArray("result").size() < 1) {
        return false;
    }
    JSONObject result = response.getJSONArray("result").getJSONObject(0);
    String hostId = result.get("hostid").toString();

    ParameterArrayRequest hostDeleteRequest = new ParameterArrayRequest("host.delete");
    hostDeleteRequest.addParam(hostId);
    response = zabbixApi.call(hostDeleteRequest);
strongerfly commented 7 years ago

Here's a code in eclipse public static void deleteHost(int hostId){

    try {
        hostId = createDummyHost();

    HostDeleteRequest request = new HostDeleteRequest();
    request.addParams(hostId);

    HostDeleteResponse response = ZabbixUtils.getZabbixApi().host().delete(request);

    assertNotNull(response);
    int deletedId = response.getResult().getHostids().get(0);
    System.out.println("is deleting "+deletedId);
    assertEquals(hostId, deletedId);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static int createDummyHost() throws Exception {

    HostCreateRequest request = new HostCreateRequest();
    HostCreateRequest.Params params = request.getParams();

    List<Integer> templates = new ArrayList<Integer>();
    templates.add(10116);
    params.setTemplates(templates);

    params.addGroupId(31);

    List<Macro> macros = new ArrayList<Macro>();
    Macro macro1 = new Macro();
    macro1.setMacro("{$MACRO1}");
    macro1.setValue("value1");
    macros.add(macro1);
    params.setMacros(macros);

    List<HostInterfaceObject> interfaces = new ArrayList<HostInterfaceObject>();
    HostInterfaceObject hostInterface = new HostInterfaceObject();
    hostInterface.setIp("192.168.255.200");
    interfaces.add(hostInterface);
    params.setInterfaces(interfaces);

    params.setHost("test dummy host created1");
    params.setName("test dummy host created1 name");

    HostCreateResponse response = ZabbixUtils.getZabbixApi().host().create(request);

    assertNotNull(response);
    assertNotNull(response.getResult().getHostids());
    int hostId = response.getResult().getHostids().get(0);
    assertTrue(0 < hostId);

    return hostId;

}
strongerfly commented 7 years ago

this zabbix version is 2.4.8

michaelquigley commented 7 years ago

Where are you getting the ZabbixUtils, HostDeleteRequest, HostDeleteRequest, etc. classes? Those don't come from my framework. I don't think you're using the framework that I provide. See the example I listed.

strongerfly commented 7 years ago

ok,thanks

strongerfly commented 7 years ago

Thanks .the delete way in your framework is good ,the same way has problem when I used zabbix4j .