Ad Widget

Collapse

Display number of hosts in a group

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bostone
    Junior Member
    • Sep 2023
    • 2

    #1

    Display number of hosts in a group

    I am very new to Zabbix. So far I enjoy what I can do with it. It seems to fit my use case better than other things I have tried.
    Now I am having one issue that I was not able to solve and did not find a final solution for.
    I sounds so simple that I cannot believe this is not possible:

    I have a host group and I want to display the current number number of hosts assigned to that group in a dashboard widget. I can display as simple as: Hostgroupname: No. of hosts.

    Is is that really not possible or am I missing something?
  • PeterZielony
    Senior Member
    • Nov 2022
    • 146

    #2
    You can have api call to count (as a item) to zabbix itself and you can have widget

    selectHosts


    Hiring in the UK? Drop a message

    Comment


    • bostone
      bostone commented
      Editing a comment
      Thank you for your response.
      Actually I wanted to avoid the API as I am not familiar with it.
      Nevertheless I tried to set it up but it is not working.

      I am running into the issue mentioned here: https://support.zabbix.com/browse/ZB...mment-tabpanel

      I tried to make it work by setting SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 and as this did not work I tried RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] . Neither of it are working.

      Without the header Authorization: Bearer I can access the API and get the version succesfully.
  • PeterZielony
    Senior Member
    • Nov 2022
    • 146

    #3
    Originally posted by bostone
    Thank you for your response.
    Actually I wanted to avoid the API as I am not familiar with it.
    Nevertheless I tried to set it up but it is not working.

    I am running into the issue mentioned here: https://support.zabbix.com/browse/ZB...mment-tabpanel

    I tried to make it work by setting SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 and as this did not work I tried RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] . Neither of it are working.

    Without the header Authorization: Bearer I can access the API and get the version successfully.​
    that's another issue. I'm using zbx 6.2 where I can use the auth property in the request body

    Click image for larger version  Name:	image.png Views:	1 Size:	52.6 KB ID:	470269
    *Then you have to have preprocessing to actually count items.(or other items that will count them)
    *screenshot might actually be wrong - i didn't test it. example has headers in query field ;D

    Website says:
    "Note that the "auth" property is deprecated. It will be removed in future releases" - however, i don't know if has been removed from the latest or not. If you can get data w/o bearer .. then use that for now.





    another way is to take it from DB itself - same as the item and attach a graph to it.
    Last edited by PeterZielony; 13-09-2023, 12:07.

    Hiring in the UK? Drop a message

    Comment


    • soumya
      soumya commented
      Editing a comment
      Hi @PeterZielony,
      You can use below query to get the count of hosts of certain host group. You need to update groupid (here it's mentioned 7) to as per yours.

      Code :-

      {
      "jsonrpc": "2.0",
      "method": "host.get",
      "params": {
      "output": [
      "host", "groups"
      ],
      "groupids": "7",
      "countOutput": "selectHosts"
      },
      "auth": "{{you_auth_token}}",
      "id": 1
      }

      Output:-

      {
      "jsonrpc": "2.0",
      "result": "2",
      "id": 1
      }


      Hope this might help.
      Last edited by soumya; 06-01-2024, 13:42.
  • bostone
    Junior Member
    • Sep 2023
    • 2

    #4
    Great. Thanks for that bit, that's very interesting.
    However I did not want to use the "auth" property for it being deprecated.​
    So I decided to do it a bit more hacky style.

    I am using basic auth additionally to secure my installation.
    I found out that this creates an additional Authorization header, that Zabbix does not want.

    So first thing was to make sure there is only one authorization header, by creating a single authorization header that included the Basic Auth and the Bearer Auth. This can be done by simply separating them by comma (Basic <Token>, Bearer <Token>
    Then I hacked the core files:

    In /usr/share/zabbix/include/classes/core/CHttpRequest.php I modifed the function getAuthBearerValue() into this:

    Code:
        public function getAuthBearerValue() {
            $auth = $this->header('AUTHORIZATION');
    
    # Adding for Basic Auth
    $pos = strpos($auth,ZBX_API_HEADER_AUTHENTICATE_PREFIX);
    #echo $pos;
    
            if (is_string($auth) && substr($auth, $pos, 7) === ZBX_API_HEADER_AUTHENTICATE_PREFIX) {
                return substr($auth, $pos+7);
            }
    
            return null;
        }
    ​
    Now it will find the Bearer part in the authorization header (given the order Basic, Bearer in the authorization header) and use this to authorize. It is working for me.

    So now I am able to get the total host count in one host group and display it as Widget on the dashboard. It is great.
    Thanks a lot for your input.
    Last edited by bostone; 14-09-2023, 06:31.

    Comment

    Working...