Ad Widget

Collapse

Zabbix 1.8 API - PHP Class - Release 1.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Farley
    Junior Member
    • Sep 2009
    • 20

    #16
    Originally posted by lstewart
    Thanks a lot Danrog for posting the .zip while Andrew's VM was down. Gave me a head start over the weekend at least.

    Thanks for writing this by the way Andrew, it is really awesome. Using the API's by themselves without the help of ZabbixAPI.class.php was largely impossible for me but now I'm able to create hosts bound to templates. This is great. I'll get into it more this week. I'll have to work on editing existing hosts, deleting hosts and pulling some status information out of zabbix. All of this appears to be possible so its just a matter of figuring it out.

    Thanks again everyone.
    No problem! A few people have requested I make it less abstract/generic to make it easier to begin using it. Such as making a user class, and a host class, etc. The API is such a moving target right now that I didn't want to spend a ton of time doing that just yet. The new version of the Zabbix API should be coming out with 1.8.3 which should have most of the issues resolved from the previous releases and perhaps I'll (along with others!) will start working on making another level wrapper above this one to make it easier for people to use.

    Also, by popular demand I'm moving the code to github shortly, so it'll be on there soon and from then on.

    Cheers!

    Comment

    • lstewart
      Junior Member
      • Feb 2009
      • 11

      #17
      Not sure this is the best place to put this but I can think of no better at the moment. I'm experiencing some strange behavior with the API that's probably by design and I just don't know the right way to do it.

      I have two issues actually.

      --Issue 1--
      When updating a host and removing some previously selected templates it removes the templates from the host in Zabbix but leaves the "Items" and "Triggers" created by that template.

      Basically to update a host I have all the the hosts information (changes and data that's not changed) in the $_POST[] array from the form.

      I do the update like this:
      PHP Code:
      ZabbixAPI::query('host','update'$_POST)
        or die(
      '<br><br>Unable to update host: '.print_r(ZabbixAPI::getLastError(),true));
      header ("Location: ".$_REQUEST[PHPSELF]."?oper=edithost&sname=".$_POST["host"]."&msg=Last edit successful"); 
      The edit is successfull which I was so happy to see but again it leaves the 'items' created by a template previously linked there while removing the template link itself.

      The $_POST[] array contains information like this:
      Code:
      _POST["hostid"]	10070
      _POST["oper"]	updatehost
      _POST["host"]	no1.domainname.com
      _POST["status"]	0
      _POST["useip"]	1
      _POST["ip"]	207.228.228.4
      _POST["dns"]	no value
      _POST["port"]	10050
      _POST["templates"]	  Array
                                           (
                                               [0] => 10049
                                               [1] => 10055
                                               [2] => 10056
                                               [3] => 10057
                                               [4] => 10058
                                           )
      Again - When the "templates" element is changed it works but the "items" from that template are still on the host in Zabbix.

      example:
      Previous to removing the "SSH port 22" template one might see this:
      Code:
      SSH port 22:SSH server is running 	Triggers (1) 	ssh 	300 	7 	365 	Simple check 	Active 	-
      After moving the "SSH port 22" template you'd see this:
      Code:
      SSH server is running 	Triggers (1) 	ssh 	300 	7 	365 	Simple check 	Active 	-
      Am I removing templates incorrectly? Is there a parameter for the update 'command' in the API that I'm missing?

      --Issue 2--
      The way I'm checking to see what templates are linked to a host is fishy. It's probably totally wrong but seems to work:

      While building a <select></select> list I pass the current templateid, hostid and host to this function. It does some freaky magic and returns "selected" if "$stemp[0][hostid] == $hostid".

      PHP Code:
      function TempSelected($tempid,$hostid,$sname) {
        
      $stemp ZabbixAPI::fetch_array('host''get', array(templateids => array($tempid), filter => array(host => $sname)))
           or die(
      'Unable to get hosts: '.print_r(ZabbixAPI::getLastError(),true));
        if (
      $stemp[0][hostid] == $hostid) { return "selected"; }

      This way, in my foreach loop, listing all available templates, it inserts a "selected" into those that the host is actually using vs. not selecting the ones it doesn't use. I hope I've explained that well enough to understand.

      Is this the right way to do it?

      Thanks for any and all advice anyone can provide. I'm getting somewhere with this which is awesome.

      Comment

      • btriem
        Member
        • Apr 2006
        • 30

        #18
        updated api

        Hey Andrew,

        Was just curious if you still have plans to move the API to github or sourceforge. I would prefer github over sourceforge myself. One of the modifications I would love to see in your class is the method of getting graph images using curl, as done in this class for mobile zabbix: https://github.com/mattiasgeniar/MoZ...ass_zabbix.php. I would be happy to contribute to your class once you find a home for it.

        Look forward to your thoughts.

        Brad

        Comment

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

          #19
          btriem, Zabcon supports the downloading of item graphs from the command line. You will need the following custom command to do it: http://trac.red-tux.net/browser/trun...x_get_graph.rb

          You will also need to configure your zabcon.conf file, the example file is here:

          you will need to set the line custom_commands to be the path of your custom command file.

          If you need any help drop me an email or find me in the Zabbix IRC channel on freenode. nelsonab (at) red-tux (dot) net
          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

          • Farley
            Junior Member
            • Sep 2009
            • 20

            #20
            Sorry I haven't spent as much time evolving the API as I would like. I migrated it to github...

            Fork it at: https://github.com/AndrewFarley/PHP-Zabbix-API

            Cheers!

            Originally posted by btriem
            Hey Andrew,

            Was just curious if you still have plans to move the API to github or sourceforge. I would prefer github over sourceforge myself. One of the modifications I would love to see in your class is the method of getting graph images using curl, as done in this class for mobile zabbix: https://github.com/mattiasgeniar/MoZ...ass_zabbix.php. I would be happy to contribute to your class once you find a home for it.

            Look forward to your thoughts.

            Brad

            Comment

            • nleaman
              Member
              • May 2010
              • 35

              #21
              missing groups array from extended host info

              Anyone know why when I do a ZabbixAPI::fetch_array('host','get',array('extendo utput'=>1)) it doesn't return the groups array? I can get the groups array with an api call via a command line curl so I know it should be there.

              Comment

              • nleaman
                Member
                • May 2010
                • 35

                #22
                err... never mind. I forgot to add 'select_groups'=>1 in the parameters

                Comment

                • Tristan
                  Senior Member
                  • Feb 2008
                  • 110

                  #23
                  2 issues

                  Hello,

                  great work for both the API and the php api class!

                  i have 2 questions:
                  i'm playing with the PHP api and i'm trying to give a usergroup read/write rights to a host group. the syntax:
                  PHP Code:
                  ZabbixAPI::query('usergroup','massAdd',array('usrgrpids'=>38,rights=>array('permisssion'=>3,'id'=>13)))
                      or die(
                  'Could not add user rights to hostgroup: '.print_r(ZabbixAPI::getLastError(),true)); 
                  but it isn't working. There is no error on the screen. I think there is someting wrong with the multidimensional array. Has anybody a clue?

                  second question:
                  i'm also trying to add a action with a operation. the syntax:
                  PHP Code:
                  ZabbixAPI::query('action','create',
                   array(
                  'name'=>"Action",
                          
                  'eventsource'=>0,
                          
                  'evaltype'=>3,
                          
                  'status'=>1,
                         
                  'esc_period'=>3600,
                         
                  'def_shortdata'=>'{TRIGGER.NAME}: {STATUS}',
                         
                  'def_longdata'=>'{TRIGGER.NAME}: {STATUS}',
                         
                  'recovery_msg'=>0,
                         
                  conditions=>array('conditiontype'=>3,
                                             
                  'operator'=>2,
                                             
                  'value'=>test,
                                         ),       
                          
                  operations=>array('operationtype'=>0,
                                   
                  'object'=>0,
                                   
                  'default_msg'=>1,
                                   
                  'opmediatypes'=>0,    
                          
                  opmediatypes=>array('mediatypeid'=>1
                                  
                  ),
                                  ),
                          ))  
                  or die(
                  'Could not add user to group: '.print_r(ZabbixAPI::getLastError(),true)); 
                  but this one also errors and there are a lot of errors ;-). Has somebody a clue?

                  Comment

                  • dbarton
                    Junior Member
                    • Aug 2012
                    • 15

                    #24
                    Hi

                    I've noticed that the existing PHP class is no longer maintained (just wanted to use it by myself), so I've decided this weekend to write a new PHP library.

                    The library is automatically generated by the origin sources / classes of the Zabbix API, so it's "always" up2date!

                    Check it out: http://zabbixapi.confirm.ch

                    Cheers
                    Domi

                    Comment

                    • kayjay
                      Member
                      • Jun 2010
                      • 43

                      #25
                      I agree - great work for both the API and the php api class!

                      I've hit a fundamental problem with some php api functions - it may just be me misunderstanding. An example of this is in 'host','create' when adding a host that is a member of two groups.

                      Creating a host with one group member looks like this:

                      ZabbixAPI::query('host','create',
                      array('host'=>"MyHost",
                      'ip'=>"1.2.3.4",
                      'port'=>10050,
                      'useip'=>1,
                      'status'=>1,
                      'groups'=>array('groupid'=>1),
                      'templates'=>array('templateid'=>999) ) )

                      But what to do with two groups - at first you may try this:

                      ZabbixAPI::query('host','create',
                      array('host'=>"MyHost",
                      'ip'=>"1.2.3.4",
                      'port'=>10050,
                      'useip'=>1,
                      'status'=>1,
                      'groups'=>array('groupid'=>1,'groupid'=>2),
                      'templates'=>array('templateid'=>999) ) )

                      Clearly this cannot be correct because the groups associative array has two members with the same name - only one will be seen. Have I missed something

                      Comment

                      • Pavels
                        Member
                        • Oct 2011
                        • 83

                        #26
                        kayjay, you can pass host groups either as a single object or an array of objects. Multiple host groups would look like this:

                        Code:
                        ZabbixAPI::query('host','create',
                        array('host'=>"MyHost", 
                        'ip'=>"1.2.3.4", 
                        'port'=>10050, 
                        'useip'=>1, 
                        'status'=>1,
                        'groups'=>array(
                          array('groupid'=>1),
                          array('groupid'=>2)
                        ),
                        'templates'=>array('templateid'=>999) ) )

                        Comment

                        • kayjay
                          Member
                          • Jun 2010
                          • 43

                          #27
                          Bingo!

                          I thought I had already tried this but obviously not

                          Thanks

                          Comment

                          Working...