Ad Widget

Collapse

Help with host.create via API using bash and curl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ledgewatcher
    Junior Member
    • Apr 2016
    • 2

    #1

    Help with host.create via API using bash and curl

    Hi I have recently implemented a basic Zabbix installation at my company as a POC and they now wish me to take it further. I found an old v3 script I have used in the past and expected it to work with some revision.

    I want automate creating new hosts in Zabbix, initially to only assign them a template and hostgroup. I have hit an issue I cannot seem to work. I have tried escaping double quotes which I expected to be the issue.
    I hope someone in the community would be able to assist.

    I am using a bash script with curl to post the data request.
    Curl is RHEL 7 v7.29. I have stripped out all variables whilst trying to debug.

    The error that is always returned from the API using host.create is ;
    Code:
    "jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":"Invalid JSON. An error occurred on the server while parsing the JSON text."},"id":null}
    I am able to use a variation of the code to gain an auth token or create hostgroups, so the API is working, but host.create does not.
    The auth token I am using to debug is temporarily for the zabbix admin user, to be sure it's not a permission issue (sub'd in output for 123456)
    I have also tried placing the JSON in a file and using the curl --data @file option with the same result.

    Code:
    create=$(/usr/bin/curl -s -X POST -H 'Content-Type:application/json' http://zabbix/zabbix/api_jsonrpc.php -d " {
         \"jsonrpc\": \"2.0\",
         \"method\": \"host.create\",
         \"params\": {
         \"host\": \"testlinux\",
         \"interfaces\": [
           {
            \"type\": 1,
            \"main\": 1,
            \"useip\": 1,
            \"ip\": \"172.20.1.1\",
            \"dns\": \"\",
            \"port\": \"10050\"
            }
            ],
            \"groups\": [
           { \"groupid\": \"49\" }
           ],
            \"templates\": [
           { \"templateid\": \"10081\" }
           ],
           },
            \"auth\": \"123456\",
            \"id\": 1
           }"
           )
    
    echo $create
    Thanks in advance.
Working...