Ad Widget

Collapse

API Post assistance.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cslaughter
    Junior Member
    • Aug 2012
    • 7

    #1

    API Post assistance.

    I am trying to convert an ols script from Zabbix 1.8 to 2.X and am having some trouble.

    The command below works on 1.8, but generated an error on 2.0.2
    Note that the $AUTH_TOKEN has been populated in both cases ensuring this is not an authentication issue.
    Code:
    API='http://#.#.#.#/zabbix/api_jsonrpc.php'
    curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API
    The error it returns on 2.0.2
    Code:
    HTTP/1.1 200 OK
    Date: Mon, 12 Nov 2012 20:09:35 GMT
    Server: Apache/2.2.22 (Ubuntu)
    X-Powered-By: PHP/5.3.10-1ubuntu3.4
    Content-Length: 159
    Content-Type: application/json
    
    {"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}
    Anyone have any ideas what's missing?
  • Pavels
    Member
    • Oct 2011
    • 83

    #2
    That's strange, it works fine when I try it. Are you sure the authentication token is correct?

    Code:
    curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"auth\": \"5c16ae15d4a3747d2488fcd4fc66b9c3\", \"id\": 0}" http://localhost/zabbix/trunk-public/frontends/php/api_jsonrpc.php
    
    HTTP/1.1 200 OK
    Date: Tue, 13 Nov 2012 08:15:01 GMT
    Server: Apache/2.2.21 (Unix) DAV/2 mod_ssl/2.2.21 OpenSSL/1.0.0c PHP/5.3.8 mod_apreq2-20090110/2.7.1 mod_perl/2.0.5 Perl/v5.10.1
    X-Powered-By: PHP/5.3.8
    Content-Length: 251
    Content-Type: application/json
    
    {"jsonrpc":"2.0","result":[{"groupid":"5","name":"Discovered hosts","internal":"1"},{"groupid":"2","name":"Linux servers","internal":"0"},{"groupid":"1","name":"Templates","internal":"0"},{"groupid":"4","name":"Zabbix servers","internal":"0"}],"id":0}

    Comment

    • cslaughter
      Junior Member
      • Aug 2012
      • 7

      #3
      Originally posted by Pavels
      That's strange, it works fine when I try it. Are you sure the authentication token is correct?
      Positive.

      Originally, the script failed to capture the token and I had to modify it. Looks like the response from a 2.0.2 and a 1.8 server are different. See below

      Zabbix 1.8 (Successfull) Authentication Responce
      Code:
      carl@WCG-cslaughter-lt:~$ curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$ZABBIX_PASS\", \"user\": \"$ZABBIX_USER\"}, \"jsonrpc\": \"2.0\", \"method\": \"user.authenticate\",\"auth\": \"\", \"id\": 0}" $API
      HTTP/1.1 200 OK
      Date: Tue, 13 Nov 2012 16:22:09 GMT
      Server: Apache/2.2.14 (Ubuntu)
      X-Powered-By: PHP/5.3.2-1ubuntu4.18
      Set-Cookie: zbx_sessionid=5b5ff0aa064ed11934fb19da530e897d
      Set-Cookie: zbx_sessionid=d8ba48269ece4ac42598213e5b46fbcf
      Set-Cookie: zbx_sessionid=deleted; expires=Mon, 14-Nov-2011 16:22:08 GMT
      Vary: Accept-Encoding
      Content-Length: 68
      Content-Type: text/html
      
      {"jsonrpc":"2.0","result":"5b5ff0aa064ed11934fb19da530e897d","id":0}
      Zabbix 2.0.2 (Successfull) Authentication Responce
      Code:
      carl@WCG-cslaughter-lt:~$ curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$ZABBIX_PASS\", \"user\": \"$ZABBIX_USER\"}, \"jsonrpc\": \"2.0\", \"method\": \"user.authenticate\",\"auth\": \"\", \"id\": 0}" $API
      HTTP/1.1 200 OK
      Date: Tue, 13 Nov 2012 16:26:07 GMT
      Server: Apache/2.2.22 (Ubuntu)
      X-Powered-By: PHP/5.3.10-1ubuntu3.4
      Content-Length: 68
      Content-Type: application/json
      
      {"jsonrpc":"2.0","result":"0a193a29f6ff7cfe05dedc305088fdda","id":0}
      From these examples you can see that there a number of differences. Mainly the absence of the "Set-Cookie: zbx_sessionid=" lines. Because of this, I had to update my script and change how to parsed out the authentication token. However in the end I am positive I am getting the correct token.
      For reference I am using the following command to parse the token from both 1.8 and 2.0.2 servers.
      Code:
      curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$ZABBIX_PASS\", \"user\": \"$ZABBIX_USER\"}, \"jsonrpc\": \"2.0\", \"method\": \"user.authenticate\",\"auth\": \"\", \"id\": 0}" $API | awk -F \" '{print $8 }'
      I can not recall where I found the script I am using, but I will include it here so you can see what I am working with.

      (NOTE: This has been modified from the Authors original script.)

      Code:
      #!/bin/bash
      
      #==============================================================
      # Zabbix-Create - Automated Agent/Host Install - (c) Travis Mathis - Millicorp 2011
      # Version 2
      # Change Log: 6/20/11 Finished Host Creation
      #==============================================================
      
      # Check to ensure I am running as root
      
      #!/bin/bash
      # Init
      #FILE="/tmp/out.$$"
      #GREP="/bin/grep"
      #....
      # Make sure only root can run our script
      #if [[ $EUID -ne 0 ]]; then
      #   echo "This script must be run as root" 1>&2
      #   exit 1
      #fi
      # ...
      
      # VARIABLES
      HOSTNAME=''
      SERVER=''
      IP=''
      API=''
      
      # CONSTANT VARIABLES
      ERROR='0'
      ZABBIX_USER='admin'
      ZABBIX_PASS='zabbix'
      SERVER='10.0.5.29'
      
      # Set the correct path for api_jsonrpc.php file
      # 1.8 API
      #API='http://10.0.5.29/zabbix/api_jsonrpc.php'
      
      # 2.0 API
      API='http://10.2.0.210/api_jsonrpc.php'
      
      
      # Request the Hostname of the server
      echo ""
      read -p "What is the HOSTNAME of the server? " RESP2
        HOSTNAME="$RESP2"
      
      # Request IPADDRESS of the server
      echo ""
      read -p "What is the IP ADDRESS of the server? " RESP3
        IP="$RESP3"
      
      # Install zabbix-agent
      # Create Zabbix agent install process here.
      
      
      # start zabbix agent
      # service zabbix-agentd start
      
      # Authenticate with Zabbix API
      authenticate() {
      curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"password\": \"$ZABBIX_PASS\", \"user\": \"$ZABBIX_USER\"}, \"jsonrpc\": \"2.0\", \"method\": \"user.authenticate\",\"auth\": \"\", \"id\": 0}" $API | grep -Eo 'Set-Cookie: zbx_sessionid=.+' | head -n 1 | cut -d '=' -f 2 | tr -d '\r'
      }
      AUTH_TOKEN=$(authenticate)
      
      # Give user HostGroup option list to choose from
      
      get_host_groups() {
        curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"name\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^name/ {print $2}' | sed 's/\(^"\|"$\)//g'
      }
      
      HOST_GROUPS=$(get_host_groups)
      
      echo ""
      echo "Please select a HOSTGROUP"
      echo ""
      select HOST in $HOST_GROUPS
      do
      echo ""
      break;
      done
      
      
      # Give user Template option list to choose from
      get_templates() {
        curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"extend\", \"sortfield\": \"template\"}, \"jsonrpc\": \"2.0\", \"method\": \"template.get\",\"auth\": \"$AUTH_TOKEN\", \"id\": 0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^host"/ {print $2}' | sed 's/\(^"\|"$\)//g'
      }
      
      
      TEMPLATES=$(get_templates)
      
      echo ""
      echo "Please select a TEMPLATE"
      echo ""
      select TEMP in $TEMPLATES
      do
      echo ""
      break;
      done
      
      # Get Host_Group and Template ID's for host creation
      get_host_group_id() {
       curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"hostgroup.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"name\":[\"$HOST\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API | sed -e 's/[{}]/''/g' | sed -e 's/[""]/''/g' | grep -Eo groupid:[0-9]* | cut -d":" -f2;
      }
      
      get_template_id() {
        curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"template.get\",\"params\":{\"output\": \"extend\",\"filter\":{\"host\":[\"$TEMP\"]}},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API | sed -e 's/[{}]/''/g' | awk -v RS=',"' -F: '/^templateid/ {print $2}' | sed 's/\(^"\|"$\)//g' | sed -e 's/["]]/''/g'
      }
      
      HOSTGROUPID=$(get_host_group_id)
      TEMPLATEID=$(get_template_id)
      
      # Create Host
      create_host() {
        curl -i -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\":\"2.0\",\"method\":\"host.create\",\"params\":{\"host\":\"$HOSTNAME\",\"ip\":\"$IP\",\"port\":10050,\"useip\":1,\"groups\":[{\"groupid\":$HOSTGROUPID}],\"templates\":[{\"templateid\":$TEMPLATEID}]},\"auth\":\"$AUTH_TOKEN\",\"id\":0}" $API
      }
      
      FINAL_RESULT=$(create_host)
      
      # For Debug only
      
      # echo $FINAL_RESULT
      # echo "curl -i -X POST -H 'Content-Type: application/json-rpc' -d \"{\"jsonrpc\":\"2.0\",\"method\":\"host.create\",\"params\":{\"host\":\"$HOSTNAME\",\"ip\":\"$IP\",\"port\":10050,\"useip\":1,\"groups\":[{\"groupid\":$HOSTGROUPID}],\"templates\":[{\"templateid\":$TEMPLATEID}]},\"auth\":\"$AUTH_TOKEN\",\"id\":0}\" $API"

      Comment

      • cslaughter
        Junior Member
        • Aug 2012
        • 7

        #4
        So I took another approach and employed a python script to see if I could make any headway. I am still not any closer, but it is now even more apparent that there is a significant change in the API. I did a host get on both the 1.8 and the 2.0 server and here are the results respectively.

        1.8
        Code:
        10: json_obj: {'params': {}, 'jsonrpc': '2.0', 'method': 'APIInfo.version', 'auth': u'b6356cbc5093958ee804aa340b8e35cd', 'id': 1}
        10: Response Body: {u'jsonrpc': u'2.0', u'result': u'1.1', u'id': 1}
        [B]Zabbix API Version: 1.1[/B]
        10: json_obj: {'params': {'extendoutput': True, 'monitored_hosts': True}, 'jsonrpc': '2.0', 'method': '[B]host.get[/B]', 'auth': u'b6356cbc5093958ee804aa340b8e35cd', 'id': 3}
        20: Sending: {"params": {"extendoutput": true, "monitored_hosts": true}, "jsonrpc": "2.0", "method": "host.get", "auth": "b6356cbc5093958ee804aa340b8e35cd", "id": 3}
        10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
        20: Response Code: 200
        10: Response Body: {u'jsonrpc': u'2.0', u'result': [{u'available': u'2', u'maintenance_type': u'0', u'ipmi_errors_from': u'0', u'ip': u'127.0.0.1', u'ipmi_username': u'', u'snmp_disable_until': u'0', u'ipmi_authtype': u'0', u'ipmi_disable_until': u'0', u'useipmi': u'0', u'lastaccess': u'0', u'port': u'10050', u'snmp_error': u'', u'outbytes': u'0', u'ipmi_privilege': u'2', u'inbytes': u'0', u'useip': u'1', u'maintenanceid': u'0', u'ipmi_ip': u'', u'dns': u'zabbix.local.dom', u'status': u'0', u'snmp_available': u'0', u'host': u'Zabbix Server', u'disable_until': u'1345587650', u'ipmi_password': u'', u'ipmi_available': u'0', u'maintenance_status': u'0', u'snmp_error...(the remaining output has been omitted for brevity.)
        and from 2.0
        Code:
        20: Sending: {"params": {}, "jsonrpc": "2.0", "method": "APIInfo.version", "auth": "3ae7d1a8c62860cf1aa15a074dbf6f13", "id": 1}
        10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
        20: Response Code: 200
        10: Response Body: {u'jsonrpc': u'2.0', u'result': u'[B]1.4[/B]', u'id': 1}
        [B]Zabbix API Version: 1.4[/B]
        
        
        20: Sending: {"params": {"extendoutput": true, "monitored_hosts": true}, "jsonrpc": "2.0", "method": "[B]host.get[/B]", "auth": "3ae7d1a8c62860cf1aa15a074dbf6f13", "id": 3}
        10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
        20: Response Code: 200
        10: Response Body: {u'jsonrpc': u'2.0', u'result': [{u'hostid': u'10084'}, {u'hostid': u'10085'}, {u'hostid': u'10087'}, {u'hostid': u'10088'}, {u'hostid': u'10089'}, {u'hostid': u'10090'}, {u'hostid': u'10091'}, {u'hostid': u'10092'}, {u'hostid': u'10093'}, {u'hostid': u'10094'}, {u'hostid': u'10095'}, {u'hostid': u'10096'}, {u'hostid': u'10097'}, {u'hostid': u'10098'}, {u'hostid': u'10099'}, {u'hostid': u'10100'}, {u'hostid': u'10102'}, {u'hostid': u'10103'}, {u'hostid': u'10104'}, {u'hostid': u'10105'}, {u'hostid': u'10106'}, {u'hostid': u'10107'}, {u'hostid': u'10108'}, {u'hostid': u'10109'}, {u'hostid': u'10110'}, {u'hostid': u'10111'}, {u'hostid': u'10112'}, {u'hostid': u'10113'}, {u'hostid': u'10114'}, {u'hostid': u'10115'}, {u'hostid': u'10116'}, {u'hostid': u'10117'}, {u'hostid': u'10118'}, {u'hostid': u'10119'}, {u'hostid': u'10120'}, {u'hostid': u'10121'}, {u'hostid': u'10122'}, {u'hostid': u'10123'}, {u'hostid': u'10124'}, {u'hostid': u'10125'}, {u'hostid': u'10126'}, {u'hostid': u'10127'}, {u'hostid': u'10128'}, {u'hostid': u'10129'}, {u'hostid': u'10130'}, {u'hostid': u'10131'}, {u'hostid': u'10132'}, {u'hostid': u'10133'}, {u'hostid': u'10134'}], u'id': 3}
        Traceback (most recent call last):
        In this case the new API only returns the hostid instead of the full host record.

        So it looks like I have a little more digging to do before I get this figured out...

        Anyone have any thoughts?

        Comment

        • Pavels
          Member
          • Oct 2011
          • 83

          #5
          Yes, there are quite a few differences between the 1.8 and 2.0 APIs. One of them is that the "extendoutput" parameter is no longer supported in 2.0. You can use either "output": "extend" or pass an array of property names that you need.

          Refer to the 2.0 manual for more details
          http://www.zabbix.com/documentation/...x/api/host/get

          Comment

          • cslaughter
            Junior Member
            • Aug 2012
            • 7

            #6
            Originally posted by Pavels
            Yes, there are quite a few differences between the 1.8 and 2.0 APIs. One of them is that the "extendoutput" parameter is no longer supported in 2.0. You can use either "output": "extend" or pass an array of property names that you need.

            Refer to the 2.0 manual for more details
            http://www.zabbix.com/documentation/...x/api/host/get
            I was afraid of that. Looks like I am stuck manually updating zabbix for now. I am no programmer and having looked at the scripts for hours, I still have no idea on exactly what to change to make it work.. Hopefully someone will eventually have a few working examples I can work from sometime in the future.

            Thank you for your input.

            carl

            Comment

            Working...