Ad Widget

Collapse

PhpZabbixApi - PHP library for Zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbarton
    Junior Member
    • Aug 2012
    • 15

    #1

    PhpZabbixApi - PHP library for Zabbix

    Hi

    I'm working with Zabbix for a few days now and I'm really impressed!

    But I was a little annoyed, that there is no existing PHP library for the API (or they were just not geek-friendly).

    So I've decided to create a new PHP library for the Zabbix API:



    The library is automatically compiled by a builder, which is generating the library directly from the Zabbix PHP front-end classes. Both (the builder and the library) are written in PHP.

    Of course the builder can be used to compile a library for other programming languages (e.g. ObjectivC, Java, C++).

    It would be great to get some feedbacks and if it's usable.

    Cheers
    Domi
  • Gunsthall
    Junior Member
    Zabbix Certified Specialist
    • Jul 2011
    • 15

    #2
    Thanks for your effort and welcome to the Zabbix community.

    I will take a look at your implementation when I´m back from holidays and let you know if I find any problem.

    Cheers!

    Comment

    • dbarton
      Junior Member
      • Aug 2012
      • 15

      #3
      Hi Gunsthall

      Thank you for your reply. I really appreciate it!
      Let me know if you find any bugs or if you've an idea how to improve it.

      Thank you very much!

      Cheers
      Domi

      Comment

      • LeoCombes
        Member
        • Mar 2011
        • 32

        #4
        Hi Dbarton.
        I try to test your "PhpZabbixApi Library", put ZabbixApi.class.php, ZabbixApiAbstract.class.php and test.php, all in same directory.
        In test.php I write one of yours examples:

        Code:
         <?php
        // load ZabbixApi
        require 'ZabbixApi.class.php';
        try {
            // connect to Zabbix API
            $api = new ZappixApi('http://zabbix.local/api_jsonrpc.php', 'zabbix', 'admin');
        } catch(Exception $e) {
        
            // Exception in ZabbixApi catched
            echo $e->getMessage();
        }
        ?>
        ...and receive this error:
        PHP Fatal error: Class 'ZappixApi' not found in /var/www/test/PhpZabbixApi_Library/test.php on line 16
        The problem must be silly, but I can not realize what is wrong.

        Comment

        • dbarton
          Junior Member
          • Aug 2012
          • 15

          #5
          Hi

          Oops this is my fault. It's a typo in the examples (Zappix instead of Zabbix), just replace:

          PHP Code:
          new ZappixApi 
          with

          PHP Code:
          new ZabbixApi 
          Of course you've to point the URL (zabbix.local/zabbix) to your zabbix server/front-end.

          Hope this helps. If you've any further questions (or bugs), don't hesitate in contacting me!

          Thank you for testing the API

          Cheers
          Domi

          Comment

          • LeoCombes
            Member
            • Mar 2011
            • 32

            #6
            Works fine now!

            Thanks.

            Comment

            • LeoCombes
              Member
              • Mar 2011
              • 32

              #7
              Hi dbarton!
              I try add a host with this. Previously I see that groupid 6 and template 10086 exists.

              Code:
                  $hosts = $api->hostCreate(array(
                      'host' => 'modem_345',
                      'name' => 'modem_345',
                      'interfaces' => array(
                          'useip' => 1,
                          'dns' => "",
                          'ip' => '172.31.2.34',
                          'type' => 2,
                          'main' => 1,
                          'port' => 0),
                      'groups' => array(
                          'groupid' => 6),
                      'templates' => array(
                           'templateid' => 10086)
                  ));
              I get the following error:
              API error -32500: No permissions to referred object or it does not exist!
              it does not work at all, is supposed to work?

              Comment

              • dbarton
                Junior Member
                • Aug 2012
                • 15

                #8
                Hi

                Are you sure you've enough permissions?

                Cheers
                Domi

                Comment

                • LeoCombes
                  Member
                  • Mar 2011
                  • 32

                  #9
                  Originally posted by dbarton
                  Hi

                  Are you sure you've enough permissions?

                  Cheers
                  Domi
                  Yes, admin has full permissions.
                  You can test if this works in some of your servers?

                  Comment

                  • dbarton
                    Junior Member
                    • Aug 2012
                    • 15

                    #10
                    Originally posted by LeoCombes
                    Yes, admin has full permissions.
                    You can test if this works in some of your servers?
                    Hi

                    Weird, isn't working for me either. I've done the same thing as you've done and followed the API documentation:

                    PHP Code:
                        $hosts $api->hostCreate(array(
                            
                    'host' => 'Linux001',
                            
                    'interfaces' => array(
                                
                    'type' => 1,
                                
                    'main' => 1,
                                
                    'useip' => 1,
                                
                    'ip' => '192.168.3.1',
                                
                    'dns' => "",
                                
                    'port' => 10050),
                            
                    'groups' => array(
                                
                    'groupid' => 1),
                        )); 
                    This was the request generated by the class:
                    Code:
                    {
                        "jsonrpc": "2.0",
                        "method": "host.create",
                        "params": {
                            "host": "Linux001",
                            "interfaces": {
                                "type": 1,
                                "main": 1,
                                "useip": 1,
                                "ip": "192.168.3.1",
                                "dns": "",
                                "port": 10050
                            },
                            "groups": {
                                "groupid": 1
                            }
                        },
                        "auth": "7e643849c1ee6c1508e10a3987d18602",
                        "id": "13466844035773"
                    }
                    And this was the response:
                    Code:
                    API error -32500: No permissions to referred object or it does not exist!
                    I see no differences between the request official documentation and the request we've sent. So IMHO this could be a bug in the official API.

                    Do you want to open a bug report, or should I do it?

                    Cheers
                    Domi

                    EDIT: bugreport opened https://support.zabbix.com/browse/ZBX-5576
                    Last edited by dbarton; 12-09-2012, 10:19. Reason: bugreport opened

                    Comment

                    • LeoCombes
                      Member
                      • Mar 2011
                      • 32

                      #11
                      Originally posted by dbarton
                      I see no differences between the request official documentation and the request we've sent. So IMHO this could be a bug in the official API.

                      Do you want to open a bug report, or should I do it?

                      Cheers
                      Domi
                      Thanks!

                      I think that it would be better if you could open a bug report, because of your API knowledge in order to explian the situation in a better way.

                      Comment

                      • Piepel
                        Junior Member
                        • Jan 2012
                        • 5

                        #12
                        I have a same problem!

                        Comment

                        • Yello
                          Senior Member
                          • Apr 2011
                          • 309

                          #13
                          Hi,
                          Is there a minimum configuration that the api will accept? Try that. If it works build up the request until it breaks.


                          Regards,
                          David

                          Comment

                          • dbarton
                            Junior Member
                            • Aug 2012
                            • 15

                            #14
                            Originally posted by Yello
                            Hi,
                            Is there a minimum configuration that the api will accept? Try that. If it works build up the request until it breaks.


                            Regards,
                            David
                            didn't work either... I've also added a DNS name (since there was a bug in 1.9, so that DNS was a required value).

                            I've opened a bug report: https://support.zabbix.com/browse/ZBX-5576

                            Comment

                            • dbarton
                              Junior Member
                              • Aug 2012
                              • 15

                              #15
                              alright there's an error in the groups definition.
                              groups has to be an array and not an object!

                              change this:
                              PHP Code:
                                  $hosts $api->hostCreate(array(
                                      
                              'host' => 'modem_345',
                                      
                              'name' => 'modem_345',
                                      
                              'interfaces' => array(
                                          
                              'useip' => 1,
                                          
                              'dns' => "",
                                          
                              'ip' => '172.31.2.34',
                                          
                              'type' => 2,
                                          
                              'main' => 1,
                                          
                              'port' => 0),
                                      
                              'groups' => array(
                                          
                              'groupid' => 6),
                                      
                              'templates' => array(
                                           
                              'templateid' => 10086)
                                  )); 
                              to this:
                              PHP Code:
                                  $hosts $api->hostCreate(array(
                                      
                              'host' => 'modem_345',
                                      
                              'name' => 'modem_345',
                                      
                              'interfaces' => array(
                                          
                              'useip' => 1,
                                          
                              'dns' => "",
                                          
                              'ip' => '172.31.2.34',
                                          
                              'type' => 2,
                                          
                              'main' => 1,
                                          
                              'port' => 0),
                                      
                              'groups' => array(array(
                                          
                              'groupid' => 6)),
                                      
                              'templates' => array(
                                           
                              'templateid' => 10086)
                                  )); 
                              cheers
                              domi

                              Comment

                              Working...