Ad Widget

Collapse

Error when adding host using Zabbix API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Judge_Dredd
    Junior Member
    • Feb 2023
    • 17

    #1

    Error when adding host using Zabbix API

    Hi All,
    I am currently trying to automate the process of installing the agent on servers as well as adding the Host to Zabbix at the same time. I have come a little unstuck when I run the host.create command. The PowerShell code is as follows (lost it's formatting in the copying into this thread):
    $request = @{
    jsonrpc = "2.0"
    method = "host.create"
    params = @{
    host = $hostName
    interfaces = @{
    type = 1 # 1 = agent
    main = 1
    useip = 1
    ip = $hostIP
    dns = $fqdn
    port = 10050 # This is the default Zabbix agent port
    }
    groups = @{
    groupid = "35"
    }
    templates = @{
    templateid = "10081"
    }
    }
    auth = $authToken
    id = 2
    } | ConvertTo-Json



    The ConvertTo-Json creates the following Json:
    {
    "method": "host.create",
    "params": {
    "host": "CHC-G2-VDA12",
    "groups": {
    "groupid": "35"
    },
    "templates": {
    "templateid": "10584"
    },
    "interfaces": {
    "port": 10050,
    "dns": "CHC-G2-VDA12.hot.co.nz",
    "ip": "10.151.103.51",
    "useip": 1,
    "type": 1,
    "main": 1
    }
    },
    "id": 2,
    "jsonrpc": "2.0",
    "auth": "902ec9f6929bbd2628ad6d0c3015026e"
    }​



    I then run the following:
    $response = Invoke-RestMethod -Uri $apiEndpoint -Method Post -Body $request -ContentType "application/json-rpc"

    The following is the error returned in $response
    jsonrpc error
    @{code=-32500; message=Application error.; data=No permissions to referred object or it does not exist!}

    Does anyone have any ideas on what could be causing this?

    Thanks in advance
    Cheers
    Marc​
  • Judge_Dredd
    Junior Member
    • Feb 2023
    • 17

    #2
    Okay - so I finally discovered that, yes, the message does give me a clue and after digging around in the permissions, the role in question did not have host.create permission however the error I get now is the following:

    {code=-32602; message=Invalid params.; data=Incorrect arguments passed to function.}

    This appears after passing the following JSON to the zabbix server:

    {
    "method": "host.create",
    "params": {
    "host": "HOTDCORDS01",
    "groups": {
    "groupid": "35"
    },
    "templates": {
    "templateid": "10081"
    },
    "interfaces": {
    "port": 10050,
    "dns": "HOTDCORDS01.hot.co.nz",
    "ip": null,
    "useip": 1,
    "type": 1,
    "main": 1
    }
    },
    "id": 2,
    "jsonrpc": "2.0",
    "auth": "e88a841d7636017f839b2ae55b3ee4c5"
    }


    I am not a coder so I have no idea how the JSON should actually look. Anyone recognise what may cause the above error

    Thank in advance

    Cheers
    Marc​

    Comment

    • Judge_Dredd
      Junior Member
      • Feb 2023
      • 17

      #3
      But yet again, answered my own question by actually reading the code. The issue here was that "ip": null isn't going to work because I have stated "useip": 1 in the next line. The powershell command I was using didn't seem to be overly reliable in always returning the IP address so found a more reliable way.

      Comment

      Working...