Ad Widget

Collapse

Create "Filters" with API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • exajul
    Junior Member
    • May 2025
    • 3

    #1

    Create "Filters" with API

    I am currently testing out the API and unfortunately I cannot find an option to do the following task with the API.

    Under Monitoring -> Hosts there's the possibility to filter for specific hosts. I can also save the Filter with the "Save as" button (I can also select if I want to show the number of records).
    Click image for larger version

Name:	image.png
Views:	241
Size:	48.6 KB
ID:	503527
    Afterwards I can access the filter again.
    Click image for larger version

Name:	image.png
Views:	199
Size:	4.0 KB
ID:	503528
    So is there also a way to do this via API?​
  • Observer365
    Junior Member
    • May 2025
    • 7

    #2
    Hello,
    If got the issue correctly, you want to get total objects from the specific filter configuration. It looks that you need to use "host.get" method with output option "output":["hostid"] and than count all ID`s.
    Check API documentation https://www.zabbix.com/documentation...rence/host/get and https://www.zabbix.com/documentation/7.0/en/manual/api

    As examples it could be done with a bash script that POSTs to Zabbix server, transforms result to json, counts the lines and subtracts non relevant json lines:

    Code:
    expr $(curl --request POST \
                  --url 'http://<ZABBIX SERVER HOST>/api_jsonrpc.php' \
                  --header 'Authorization: Bearer <AUTORIZATION TOKEN>' \
                  --header 'Content-Type: application/json-rpc' \
                  --data '{"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid"],"filter":{"host":["Zabbix server"]}},"id":1}' | \
                  jq | wc -l) - 8
    or second example with Python "zabbix_utils" library: https://github.com/zabbix/python-zabbix-utils

    Code:
    from zabbix_utils import ZabbixAPI
    api = ZabbixAPI(url="http://<ZABBIX SERVER URL>/")
    api.login(user="Admin", password="zabbix")
    filter = api.host.get(
        output = "hostid",
        filter = {"host":["Zabbix server"]}
    )
    print(len(filter))
    api.logout()

    Comment


    • exajul
      exajul commented
      Editing a comment
      Would be nice if that was the workaround. But unfortunately I don't only want the host count. I want to create the filter.
      I am searching more like a function that does something like: filter.update or filter.create :/
  • cyber
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Dec 2006
    • 4811

    #3
    I think OP wants to create filters over API. But there doesnt seem to be such method... I cannot even pinpoint, where those are saved..

    Comment

    • Observer365
      Junior Member
      • May 2025
      • 7

      #4
      Yes, it looks that there not direct filter method. So for getting required hosts you need use "get" method as: host.get, hostgroup.get. In fact they quite flexible and provide filtering possibilities. As example for host.get you can get list of hosts by name, by tag or by inventory. But how you organize the request it's all about you. It could be automatically generated from script or predefined files saved in json format as in example from the documentation:



      #cyber, What you mean about saving? Do you mean requests output? You can save it in a file, or variable and proceed it in next operations.​​

      Comment

      • cyber
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Dec 2006
        • 4811

        #5
        No I meant where those filters are saved, what you can create in UI.. and save... somewhere.. Those, what are in first post on a picture..

        Comment

        Working...