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

    #1

    Zabbix 1.8 API - PHP Class - Release 1.0

    Hey guys...

    For some clients I've needed to interface with Zabbix for a long time from specific web applications, and now that 1.8 is out with an API it's possible and EASY!

    I've written a PHP class that will simplify your use of the Zabbix API, so you can just call the class and directly communicate with Zabbix. So now you can implement Zabbix into any other systems, like other monitoring systems, management interfaces, or even write some utilities to collect data from Zabbix with this API to generate reports.

    For now I have left the class a basic passthrough class, as I have not implemented the full objects for each Zabbix object/method because they are very fluid right now and will be changing a lot in coming releases. So I won't waste time (yet) writing classes to validate methods and data structures. The Zabbix API will do this (sorta) for you. This makes the current iteration of this class future-proof as long as they keep the same JSON-RPC methodology for their API.

    Anyway... check out the example and download it here...



    Your feedback here or on my site is encouraged and appreciated. Especially if you find some bugs. Cheers!
  • Twirrim
    Junior Member
    • Nov 2009
    • 12

    #2
    Very useful, thanks for this

    Comment

    • Farley
      Junior Member
      • Sep 2009
      • 20

      #3
      Originally posted by Twirrim
      Very useful, thanks for this
      Twirrim, if you use it please post any examples, feedback, or links to where you're using it at. I'd love to know who is using it.

      Cheers!

      Comment

      • Twirrim
        Junior Member
        • Nov 2009
        • 12

        #4
        Originally posted by Farley
        Twirrim, if you use it please post any examples, feedback, or links to where you're using it at. I'd love to know who is using it.

        Cheers!
        Will do. At the moment I'm just experimenting with it, particularly given a few bugs in 1.8 that leave me reluctant to roll it out just yet. We have an in house web based customer management & ticketing system, and I'm hoping to leverage it to enable us to do automated provisioning of monitoring (all except the agent), and create a separate locked down interface for customers where they can enable and disable various bits of monitoring through very simple buttons.

        Comment

        • Farley
          Junior Member
          • Sep 2009
          • 20

          #5
          Originally posted by Twirrim
          Will do. At the moment I'm just experimenting with it, particularly given a few bugs in 1.8 that leave me reluctant to roll it out just yet. We have an in house web based customer management & ticketing system, and I'm hoping to leverage it to enable us to do automated provisioning of monitoring (all except the agent), and create a separate locked down interface for customers where they can enable and disable various bits of monitoring through very simple buttons.
          Yeah, we're also holding back on a full 1.8 upgrade, awaiting some bug fixes most of which are already in the trunk, just awaiting a full release. I have it setup in a few dev and test environments though.

          And yeah your use-case is exactly mine and why I created the class and am very excited about the API... some customer-facing portals that could use a little bit more information and integration into the server's health and monitoring system.

          Cheers!

          Comment

          • Tory
            Junior Member
            • Apr 2010
            • 1

            #6
            Thanks for the API wrapper! however, we're having some issues with disabling/deleting hosts..

            we can create hosts just fine, however, we cannot delete or disable a host with the API. when we try to delete, we get the following:
            Code:
            disableHost(10097)
            error was: 
            Array
            (
                [cde] => -32500
                [message] => Application error.
                [data] => [ CHost::update ] You do not have enough rights for operation
            )
            disableHost() is our function (that gets passed the host's ID {10097 in this case}):
            Code:
            function disableHost($hostid)
            {
            	$DEBUG = true;
            
            	if ($DEBUG) echo __FUNCTION__."($hostid)<br/>";
            
            	$result = ZabbixAPI::query('host', 'update', array('hostids' => array(array('hostid' => $hostid)), 'status' => 1));
            	if ($result === false) 
            	{
            		if($DEBUG) echo "error was: ".print_r(ZabbixAPI::getLastError(), true);
            		return false;
            	}
            	return true;
            }
            we've verified that the user has all the proper permissions, but no joy.

            The error we get when we try to delete a host is such:

            Code:
            deleteHost(10097)
            Host not deleted, reason: 
            Array
            (
                [cde] => -32500
                [message] => Application error.
                [data] => [ CHost::delete ] Host does not exist
            )
            Code:
            function deleteHost($hostid)
            {
            	$DEBUG = true;
            
            	if ($DEBUG) echo __FUNCTION__."($hostid)<br/>";
            
            	$result = ZabbixAPI::query('host', 'delete', array('hostids' => array(array('hostid' => $hostid))));
            	if ($result === false) 
            	{
            		if($DEBUG) echo "Host not deleted, reason: ". print_r(ZabbixAPI::getLastError(), true);
            		return false;
            	}
            	return true;
            }
            is there something we're missing with our functions? (missing an extra parameter when calling ZabbixAPI::query()?)

            thanks again!

            Comment

            • MarCn
              Junior Member
              • Apr 2010
              • 1

              #7
              Originally posted by Farley
              Hey guys...

              For some clients I've needed to interface with Zabbix for a long time from specific web applications, and now that 1.8 is out with an API it's possible and EASY!

              I've written a PHP class that will simplify your use of the Zabbix API, so you can just call the class and directly communicate with Zabbix. So now you can implement Zabbix into any other buy seroquell systems, like other monitoring systems, management interfaces, or even write some utilities to collect data from Zabbix with this API to generate reports.

              For now I have left the class a basic passthrough class, as I have not implemented the full objects for each Zabbix object/method because they are very fluid right now and will be changing a lot in buy acomplia coming releases. So I won't waste time (yet) writing classes to validate methods and data structures. The Zabbix API will do this (sorta) for you. This makes the current iteration of this class future-proof as long as they keep the same JSON-RPC methodology for their API.

              Anyway... check out the example and download it here...



              Your feedback here or on my site is encouraged and appreciated. Especially if you find some bugs. Cheers!
              thanks, really very usefull will inform if any bugs will be found
              Last edited by MarCn; 16-04-2010, 09:27.

              Comment

              • lndgoncalves
                Junior Member
                • Apr 2010
                • 29

                #8
                where I can download the code?
                because the site is off

                Comment

                • obrienmd
                  Junior Member
                  • Jul 2010
                  • 26

                  #9
                  When I use the following code after loading the PHP library:
                  ZabbixAPI::fetch_array('event','get',array())

                  And print_r the results, I get "Array ( [0] => )"

                  The execution time of the fetch_array is LONG, and CPU is pegged (apache process - we run mod_php).

                  Comment

                  • lstewart
                    Junior Member
                    • Feb 2009
                    • 11

                    #10
                    PHP API unavailable from andrewfarley.com

                    It seems the site hosting http://andrewfarley.com/php/zabbix-1...php-class-v1-0 is currently down. I'm interested in getting the PHP API.

                    We have our own database of servers within a web portal (written in PHP) used by staff. I'm trying to work out a way we can put some checkboxes in that web portal to include or remove a server and various templates in Zabbix automagically instead of having to go into the Zabbix GUI and configure them manually each time. I'll probably end up pre-defining the templates and just let staff choose what templates to use for a host vs. rewrite a whole template administration system. I'm weak on using PHP classes. I'm pretty good with functions but I'm not up to classes yet.

                    Is the PHP API by Andrew the way to go here?

                    Comment

                    • danrog
                      Senior Member
                      • Sep 2009
                      • 164

                      #11
                      We use it for all our automation of Zabbix, so I might be a little biased :-)

                      Looks like someone already posted it here

                      Comment

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

                        #12
                        If someone wants to take this over I can give them access to my SVN repository to keep it up to date and some Wiki space for documentation. After all that was one of my goals for red-tux.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

                        • walterheck
                          Senior Member
                          • Jul 2009
                          • 153

                          #13
                          Andrew is not dead, he's just offline and I don't know why his site is offline. I know him personally though, so I'll notify him that his site is down. Just have a little patience please
                          Free and Open Source Zabbix Templates Repository | Hosted Zabbix @ Tribily (http://tribily.com)

                          Comment

                          • Farley
                            Junior Member
                            • Sep 2009
                            • 20

                            #14
                            Yeah sorry!

                            Sorry guys, the vm I'm on is a bit flaky and needs a reboot once in awhile. It's back up now...

                            I'm moving this project into either sourceforge or github so everyone can easily get it and contribute to it. Anyone have any preferences on either of those?

                            Cheers,

                            - Farley

                            Comment

                            • lstewart
                              Junior Member
                              • Feb 2009
                              • 11

                              #15
                              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.

                              Comment

                              Working...