Ad Widget

Collapse

Need help with API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Russ
    Junior Member
    • Dec 2009
    • 15

    #1

    Need help with API

    I'm trying to get to grips with the API. I've no experience of JSON-RPC, so I'm starting entirely from scratch

    I'm using the JSON-RPC PHP client. I have a little test file that has the following code:

    PHP Code:
    <?
    include ('jsonRPCClient.php');
    try {
        $o = new jsonRPCClient('http://10.2.3.4/zabbix/api_jsonrpc.php');

        $a = $o->exec ("{'jsonrpc': '2.0',
        'method': 'user.authenticate',
        'params': {'user': 'admin', 'password': 'my_password'},
        'id': 1
        }");

        print_r ($a);
    } catch (Exception $e) {
        echo nl2br($e->getMessage()).'<br />'."\n";
    }
    ?>
    When I view the page, I see:

    HTML Code:
    Incorrect response id (request id: 1, response id: )
    The Apache logs contain the following:

    Code:
    [Thu Dec 17 11:47:17 2009] [error] [client 10.254.3.119] PHP Warning:  Cannot use a scalar value as an array in /var/www/html/zabbix/api/rpc/class.cjsonrpc.php on line 99
    [Thu Dec 17 11:47:17 2009] [error] [client 10.0.64.34] PHP Notice:  Undefined index:  id in /var/www/html/jsonRPCClient.php on line 151
    [Thu Dec 17 11:47:17 2009] [error] [client 10.0.64.34] PHP Notice:  Undefined index:  id in /var/www/html/jsonRPCClient.php on line 152
    Can anyone tell me where I'm going wrong? How do I authenticate my user?

    Russ
  • nelsonab
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Sep 2006
    • 1233

    #2
    Your JSON call looks to be good but this looks to be a bug similar to one we were working on in the last stages of the Beta. I'll try and have a look at the portion of the Zabbix code when I can and get back to you.
    RHCE, author of zbxapi
    Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
    Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

    Comment

    • decksta
      Junior Member
      • Dec 2009
      • 3

      #3
      I've been playing with 1.8 and it seems the host.get and item.get methods are returning nothing. I've logged in correctly and passed the auth token to subsequent requests. Interestingly (or not) the user.get works fine.

      Is this a known issue?

      (FWIW, I've tested these methods with Zabcon and they return the same thing: nothing)

      Comment

      • Russ
        Junior Member
        • Dec 2009
        • 15

        #4
        Originally posted by decksta
        I've been playing with 1.8 and it seems the host.get and item.get methods are returning nothing. I've logged in correctly and passed the auth token to subsequent requests. Interestingly (or not) the user.get works fine.
        Could you post your working code, please? It might help me get started.

        Russ

        Comment

        • nelsonab
          Senior Member
          Zabbix Certified SpecialistZabbix Certified Professional
          • Sep 2006
          • 1233

          #5
          Originally posted by Russ
          Could you post your working code, please? It might help me get started.

          Russ
          Check out the Zabcon site, the link is below. Now that my personal life has settled a little bit (just moved from California to East Coast) I'll be working on getting Zabcon up to snuff and the API, along with some documentation to help understand the API.

          Right now if you click on "browse source" on the Zabcon site and go into the ruby/api/zbx_api.rb file you will see the basics of how the API is used.
          RHCE, author of zbxapi
          Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
          Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

          Comment

          • decksta
            Junior Member
            • Dec 2009
            • 3

            #6
            Originally posted by Russ
            Could you post your working code, please? It might help me get started.

            Russ
            I have some Java code working (although not the item and host get methods) if you'd like to see that. What helped me greatly was the Zabcon source and setting the debug level to 10. You can see how the JSON requests are formed and mimic that via your preferred API (I am using JSON Java to marshall and unmarshall JSON objects).

            Also, another gotcha with authentication is that the user must be enabled for API access in Zabbix.

            Comment

            • Farley
              Junior Member
              • Sep 2009
              • 20

              #7
              Wanna test out my class?

              Russ... I'm working on a PHP Class to consume the Zabbix API, interested in being my first beta tester?

              PM me.

              Cheers,

              Comment

              • Russ
                Junior Member
                • Dec 2009
                • 15

                #8
                Originally posted by decksta
                I have some Java code working (although not the item and host get methods) if you'd like to see that. What helped me greatly was the Zabcon source and setting the debug level to 10. You can see how the JSON requests are formed and mimic that via your preferred API (I am using JSON Java to marshall and unmarshall JSON objects).

                Also, another gotcha with authentication is that the user must be enabled for API access in Zabbix.
                I may have another look at Zabcon, with the debug level increased. I tried looking at it earlier, but it didn't help. Being able to see the JSON requests might be useful.

                Originally posted by Farley
                Russ... I'm working on a PHP Class to consume the Zabbix API, interested in being my first beta tester?

                PM me.
                PM sent. Thanks

                Russ

                Comment

                • decksta
                  Junior Member
                  • Dec 2009
                  • 3

                  #9
                  So I finally made some headway. The user making API calls not only needs to be API enabled but also a superadmin for the host.get and item.get type requests to return anything.

                  The docs on all this are nonexistent so good luck!~

                  Comment

                  • Farley
                    Junior Member
                    • Sep 2009
                    • 20

                    #10
                    Posted my API...

                    The API I was talking about is now posted for public consumption at the URL below. Even if you don't want to use it, view the code, it's relatively simple to consume the Zabbix API with a recent version of PHP.

                    Enjoy!

                    Comment

                    • fjrial
                      Senior Member
                      • Feb 2010
                      • 140

                      #11
                      Anyone finds the solution for that piece of code (the one from the beginning of the thread) to work?

                      Comment

                      • yaap
                        Junior Member
                        • Aug 2009
                        • 20

                        #12
                        Originally posted by fjrial
                        Anyone finds the solution for that piece of code (the one from the beginning of the thread) to work?
                        Did you try add apostrophes to ID value ?

                        $a = $o->exec ("{'jsonrpc': '2.0',
                        'method': 'user.authenticate',
                        'params': {'user': 'admin', 'password': 'my_password'},
                        'id': '1'
                        }");

                        Comment

                        • nelsonab
                          Senior Member
                          Zabbix Certified SpecialistZabbix Certified Professional
                          • Sep 2006
                          • 1233

                          #13
                          Code:
                          {
                          "auth":null,
                          "method":"user.authenticate",
                          "id":0,
                          "params":{"password":"pass","user":"me"},
                          "jsonrpc":"2.0"
                          }
                          The above was taken from a debug statement from Zabcon. Try adding a blank authentication token to your statement. As for placing the id value inside tick marks, doing so will make the API end interpret the value as a string not an integer.
                          RHCE, author of zbxapi
                          Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
                          Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

                          Comment

                          • fjrial
                            Senior Member
                            • Feb 2010
                            • 140

                            #14
                            I tried with all those changes, but with no success.

                            I'm using zabbix 1.8.1 and I'm developing a third party app with PHP which uses some functionality from zabbix and I need to use the api.

                            Using jsonRPCClient, and verified all requirements.

                            Could this be the problem: http://www.zabbix.com/forum/showthread.php?p=59515 ?

                            Thanks for your help

                            Comment

                            • gpmidi
                              Member
                              • Aug 2009
                              • 62

                              #15
                              HTTP Auth

                              Are you using HTTP authentication or LDAP authentication on the Zabbix server? If so ZBX-2078 may be the problem. A more detailed description of the exact nature of the problem is in this forum post.
                              Last edited by gpmidi; 11-03-2010, 16:29. Reason: Added more details

                              Comment

                              Working...