Ad Widget

Collapse

API message: JSON-rpc version is not specified (in a bash script)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spider
    Junior Member
    • Sep 2014
    • 4

    #1

    API message: JSON-rpc version is not specified (in a bash script)

    Hello!

    I have a little problem with the Zabbix api.

    This is the bash script:


    #!/bin/bash
    USER='Admin'
    PASS='zabbix'
    ZABBIX_SERVER='127.0.0.1'
    API='http://127.0.0.1/zabbix/api_jsonrpc.php'

    # Authenticate with Zabbix API
    authenticate()
    {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"user.login\",\"params\":{\"us er\":\""${USER}"\",\"password\":\""${PASS}"\"},\"a uth\": null,\"id\":0}" $API`
    }
    AUTH_TOKEN=`echo $(authenticate)|jq -r .result`
    echo "Auth token:"$AUTH_TOKEN
    # Get This Host HostId:
    HOST_NAME=test2

    create_host()
    {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.create\",\"params\":{\"h ost\":\""$HOST_NAME"\",\"interfaces\": [{\"type\": 1,\"main\": 1,\"useip\": 1,\"ip\": \"192.168.3.2\",\"dns\": \$
    }
    RESPONSE=$(create_host)
    echo ${RESPONSE}


    Output message when i run this script:
    Auth token:51338083b2dee2475f8450ccddc6d308
    {"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request.","data":"JSON-rpc version is not specified."},"id":null}

    I tested a host remove, and it's worked.
    The authentication is good.
    The JSON-rpc version is specified in the script...
    I have no idea what's wrong with this script.

    Please help!
    Thanks!
  • spider
    Junior Member
    • Sep 2014
    • 4

    #2
    Solved!

    Solved!

    These are working: (user.login, host.get, host.delete, host.create)

    #!/bin/bash
    USER='username'
    PASS='password'
    ZABBIX_SERVER='127.0.0.1'
    API='http://127.0.0.1/zabbix/api_jsonrpc.php'

    # Authenticate with Zabbix API
    authenticate()
    {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"user.login\",\"params\":{\"us er\":\""${USER}"\",\"password\":\""${PASS}"\"},\"a uth\": null,\"id\":0}" $API`
    }
    AUTH_TOKEN=`echo $(authenticate)|jq -r .result`
    #echo "Auth token:"$AUTH_TOKEN

    HOST_NAME=test1

    get_hostid()
    {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.get\",\"params\":{\"outp ut\":\"extend\",\"filter\":{\"host\":[\""$HOST_NAME"\"]}},\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`
    }
    #host_id=`echo $(get_hostid)|jq -r .result[0].hostid`
    #echo $host_id


    #HOSTID=$host_id

    remove_host()
    {
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\":\"host.delete\",\"params\":[\""${HOSTID}"\"],\"auth\":\""${AUTH_TOKEN}"\",\"id\":0}" $API`
    }
    #RESPONSE=$(remove_host)
    #echo ${RESPONSE}

    create_host()
    {
    # good1 #echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\": \"host.create\",\"params\":{\"host\":\""${HOST_NAM E}"\",\"interfaces\": [{\"type\": 1,\"main\": 1,\"useip\": 1,\"ip\": \"192.168.3.1\",\"dns\": \"\",\"port\": \"10050\"}],\"groups\": [{\"groupid\": \"15\"}],\"templates\": [{\"templateid\": \"10105\"}],\"inventory_mode\": 0,\"inventory\": {\"macaddress_a\": \"01234\",\"macaddress_b\": \"56768\"}},\"auth\": \""${AUTH_TOKEN}"\",\"id\": 1}" $API`
    # good2 #echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\": \"host.create\",\"params\":{\"host\":\""${HOST_NAM E}"\",\"interfaces\": [{\"type\": 1,\"main\": 1,\"useip\": 0,\"ip\": \"\",\"dns\": \""${HOST_NAME}"\",\"port\": \"10050\"}],\"groups\": [{\"groupid\": \"15\"}],\"templates\": [{\"templateid\": \"10105\"}],\"inventory_mode\": 0,\"inventory\": {\"macaddress_a\": \"01234\",\"macaddress_b\": \"56768\"}},\"auth\": \""${AUTH_TOKEN}"\",\"id\": 1}" $API`
    echo `curl -s -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\",\"method\": \"host.create\",\"params\":{\"host\":\""${HOST_NAM E}"\",\"interfaces\": [{\"type\": 1,\"main\": 1,\"useip\": 0,\"ip\": \"\",\"dns\": \""${HOST_NAME}"\",\"port\": \"10050\"}],\"groups\": [{\"groupid\": \"15\"}],\"templates\": [{\"templateid\": \"10105\"}]},\"auth\": \""${AUTH_TOKEN}"\",\"id\": 1}" $API`
    }
    RESPONSE=$(create_host)
    echo ${RESPONSE}

    Comment

    Working...