Ad Widget

Collapse

Zabbix API (php) logout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elofland
    Junior Member
    • May 2011
    • 2

    #1

    Zabbix API (php) logout

    I've successfully used the API (with PHP) to gather some data and present this in a web page that is then inserted into a screen. Looks awesome, but I need more information - from another remote Zabbix server...

    Here's some sample code to illustrate:
    PHP Code:
    <?php
    require_once('include/config.inc.php');
    require_once(
    "zabbix_php_api_v1_0/ZabbixAPI.class.php");
    ZabbixAPI::debugEnabled(TRUE);
    // This logs into Zabbix, and returns false if it fails
    ZabbixAPI::login('http://zabbix1.com/zabbix/','api','password')
        or die(
    'Unable to login: '.print_r(ZabbixAPI::getLastError(),true));

    $version ZabbixAPI::fetch_string('apiinfo','version')
       or die(
    'Unable to get Zabbix Version: '.print_r(ZabbixAPI::getLastError(),true));
    echo 
    "Server running Zabbix API Version: $version\n<br>";

    // second server

    ZabbixAPI::login('http://zabbix2.com/zabbix/','api','password')
        or die(
    'Unable to login: '.print_r(ZabbixAPI::getLastError(),true));

    $version ZabbixAPI::fetch_string('apiinfo','version')
       or die(
    'Unable to get Zabbix Version: '.print_r(ZabbixAPI::getLastError(),true));
    echo 
    "Server running Zabbix API Version: $version\n<br>";
    ?>
    This is what the result is on our servers:
    HTML Code:
    Server running Zabbix API Version: 1.3 
    Unable to login: Array ( [code] => -32602 [message] => Invalid params. [data] => Not authorized )
    I've been trying to figure out how to logout, I'm not new to PHP, but I'm not very good with OO PHP and I can't seem to create a function to logout in the ZabbixAPI.class.php

    Help?
    advTHANKSance,
    Eric
    Last edited by elofland; 05-05-2011, 22:13.
  • nelsonab
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Sep 2006
    • 1233

    #2
    Not authorized
    Check the permissions of the user on the second server.
    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

    • elofland
      Junior Member
      • May 2011
      • 2

      #3
      Thanks Nelsonab, I checked the permissions of the remote user before the test by manually logging in on the remote server.

      Eric

      Comment

      • simon123
        Junior Member
        • Oct 2010
        • 22

        #4
        I have the same issue over here.

        You can use the php-api to login and get the data, but once you login to a second Zabbix server in the same PHP page, you get the JSON response below:

        Got response from API: ({"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Not authorized"},"id":1})

        Comment

        • simon123
          Junior Member
          • Oct 2010
          • 22

          #5
          Found it

          The $auth_hash should be set to NULL before opening a new connection:
          This is my working logout function for the php API:

          PHP Code:
              public static function logout(){
                  
          self::$instance->auth_hash=NULL;
              } 

          Comment

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

            #6
            Originally posted by simon123
            The $auth_hash should be set to NULL before opening a new connection:
            This is my working logout function for the php API:

            PHP Code:
                public static function logout(){
                    
            self::$instance->auth_hash=NULL;
                } 
            Except, that's not a true logout. The Authorization hash previously used is still valid until it times out inside Zabbix.
            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

            • simon123
              Junior Member
              • Oct 2010
              • 22

              #7
              Originally posted by nelsonab
              Except, that's not a true logout. The Authorization hash previously used is still valid until it times out inside Zabbix.
              Agreed, but it does make the PHP-wrapper script work with multiple Zabbix nodes in the same php-page.

              Comment

              • jordan.emersive
                Junior Member
                • Jul 2013
                • 4

                #8
                eventget Filter not working/help

                Hey There,

                I know this is an old thread, but I have some questions about the PHP API that was built (Love it btw). Thanks to this API I have been able to build a rather awesome wallboard for my company! I can provide some screenshots if you are interested in seeing what I have done...

                I have run into a problem with the eventget function and the 'filter' option. These are currently my parameters:


                PHP Code:
                $eventrec $api->eventGet(array(
                                
                'output' => 'extend',
                                
                'acknowledged' => '0',
                                
                'selectTriggers' => 'extend',
                                
                'selectHosts' => 'extend',
                                
                'object' => '0',
                                
                'source' => '0',
                                
                'select_acknowledges' => 'extend',
                                
                'select_alerts' => 'extend',
                                
                'value' => '0',
                                
                'preservekeys' =>  '1',
                                
                'filter' => array('host' => 'Emersive-AKL DynDNS),
                                '
                sortfield' => 'eventid',
                                '
                sortorder' => 'DESC',
                                '
                limit' => '600'
                            )); 

                The problem I am having, is that the filter option does not appear to be adhered to, it just gets ignored! I am suspecting this is because the 'Host' key resides inside another array (Hosts). I am unsure how I am suppose to correctly reference this key. I am able to get the filter to work for such keys as 'eventid' 'source' 'clock' and 'value' but these keys all reside in main array.

                Is there something I am doing wrong, or will this API not function to filter out keys within returned arrays?

                Any help is greatly appreciated

                Comment

                Working...