Ad Widget

Collapse

API: get hostsgroups that belongs to a usergroup

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjrial
    Senior Member
    • Feb 2010
    • 140

    #1

    API: get hostsgroups that belongs to a usergroup

    Hi:

    I'm trying to get the hostgroups (or hosts) that belongs to a certain usergroup.. through the api, of course..

    I have a userid and usergroupids, and I need to get the hostgroups or hosts that belongs (user has read/write permissions) to those userid/usergroupids

    Reading the documentation, I couldn't find the attribute in the get method in order to filter ..

    Can someone help me?
    thanks
    Last edited by fjrial; 09-03-2011, 10:03. Reason: Marked as solved
  • fjrial
    Senior Member
    • Feb 2010
    • 140

    #2
    In order to clarify my situation:

    To avoid security risks giving a account in zabbix per customer; we only set one account with api_privileges. Every client has a zabbix user but for security, it's disabled, not allowed to login in zabbix.

    The api_account performs the login through the api. It has to get hosts that belongs to a particular customer.

    So, with user api_account we perform the get users method, and filter the users obtained by the email. So we have the userid in zabbix that belongs to the customer. And now we have to perform get hosts method but filtering with that userid.

    Any ideas?
    Thank you

    Comment

    • fjrial
      Senior Member
      • Feb 2010
      • 140

      #3
      resolved

      Hi:

      After reading the api implementation, I realized that there is no attribute in host get method in order to do what I want, so I modified the method get for host, and added a new parameter in the options array in order to filter by userid.

      So the call to host.get method in PHP would be:
      PHP Code:
           $hosts=ZabbixAPI::fetch_array('host','get',array('extendoutput'=>1,'userid'=>$userid)); 
      So here it comes the zabbix modified get method in class.chost.php
      Added an option to array $def_options like:
      PHP Code:
      $def_options = array(
                              
      'nodeids'                                       => null,
                              
      'groupids'                                      => null,
                              
      'hostids'                                       => null,
      //next line is the important one
                              
      'userid'                                        => null,
                              ..... 
      And after the line
      PHP Code:
      $options zbx_array_merge($def_options$options); 
      I have to add next lines:
      PHP Code:
                      if(!is_null($options['userid'])){
                              
      $userid=$options['userid'];
                      } 
      Hope it helps someone, and also, if someone finds a problem with these changes, please, comment in this thread.

      Cheers

      Comment

      • fjrial
        Senior Member
        • Feb 2010
        • 140

        #4
        Well.. embarrassing... seems to need some additional change (comment the USER_DETAILS var en get method), but this last change, make zabbix web frontend, complains about USER_DETAILS unset..

        Hope to find a fix soon.

        FIX:
        No need to comment USER_DETAILS var.. just change in class.chost.php
        PHP Code:
        //if we set the call with userid, overwrite $USER_DETAILS values, and user_type and userid values accordingly.

              
        if(!is_null($options['userid'])){
                                
        $USER_DETAILS['userid']=$options['userid'];
                                
        $USER_DETAILS['type']=1;
                                
        $user_type $USER_DETAILS['type'];
                                
        $userid $USER_DETAILS['userid'];
                        } 
        Last edited by fjrial; 09-03-2011, 10:14.

        Comment

        • Aly
          ZABBIX developer
          • May 2007
          • 1126

          #5
          Indeed.. there is now possible way to achieve what you want, probably should be implemented. You may fill a feature request on our tracker: https://support.zabbix.com/browse/ZBXNEXT
          Zabbix | ex GUI developer

          Comment

          • fjrial
            Senior Member
            • Feb 2010
            • 140

            #6
            For me, it was quite easy to implement the change in the host get method.. it's documented in this thread..

            It's not the same functionality as the title says, but I can achieve the same behaviour: get the hosts with read/write permissions for a specific userid

            Do you think that adding this feature request to the tracker would be useful for other users?? if you think so, I'll do it..

            Thanks in advance.

            Comment

            • fjrial
              Senior Member
              • Feb 2010
              • 140

              #7
              Hi:
              in previous code, I always set user_type=1 (zabbix_user).. so, get hosts methods always check for rights table in mysql, to look for hosts granted,

              but if your user_type==3 (zabbix_super_admin), rights table may be empty, but, because you are super admin, you have all hosts granted, so we need to setup this parameter in order to get hosts returns all hosts ..


              for previous code:
              PHP Code:
                              if(!is_null($options['userid'])){
                                      
              $USER_DETAILS['userid']=$options['userid'];
                                       
              $users CUser::get(array('userids' => $options['userid'],  'extendoutput' => 1)); 
                                               foreach(
              $users as $unum => $user)
                                                      
              $USER_DETAILS['type']=$user['type'];
                                      
              $user_type $USER_DETAILS['type'];
                                      
              $userid $USER_DETAILS['userid'];
                              } 

              Comment

              • Yello
                Senior Member
                • Apr 2011
                • 309

                #8
                Hi,
                I think you should add this to the tracker if not already done. I think the nature of the request may spawn other dependent or coincident feature implementations.

                It is probably more correct to think of what you've been looking to do as part of a set of features that are currently absent from the api.


                Regards,
                David

                Comment

                • fjrial
                  Senior Member
                  • Feb 2010
                  • 140

                  #9
                  Hi David,

                  You´re right, and I thought that I submitted this, but checking my activity in JIRA, I realized that I didn´t submitted... upps .. my fault

                  So, I´ll submit the issue today afternoon/tomorrow morning.. and will post here the issue link to jira.

                  Cheers and thanks for the remainder.
                  Javi Rial.

                  Originally posted by Yello
                  Hi,
                  I think you should add this to the tracker if not already done. I think the nature of the request may spawn other dependent or coincident feature implementations.

                  It is probably more correct to think of what you've been looking to do as part of a set of features that are currently absent from the api.


                  Regards,
                  David

                  Comment

                  • zalex_ua
                    Senior Member
                    Zabbix Certified Trainer
                    Zabbix Certified SpecialistZabbix Certified Professional
                    • Oct 2009
                    • 1286

                    #10
                    created report is https://support.zabbix.com/browse/ZBXNEXT-1384

                    Comment

                    Working...