Ad Widget

Collapse

[Zabbix API] Getting all hosts that start with "FOO-"

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cesq
    Junior Member
    • Mar 2022
    • 5

    #1

    [Zabbix API] Getting all hosts that start with "FOO-"

    I am trying to retrieve all hosts that start with a certain sequence of characters via Zabbix API. Here's the relevant part of the code (python)

    Code:
    r = requests.post(ZABBIX_API_URL,
                      json={
                          "jsonrpc": "2.0",
                          "method": "host.get",
                          "params": {
                              "filter": {
                                    "host": [
                                          "FOO-*"
                                  ]
                              }
                          },
                          "id": 2,
                          "auth": AUTHTOKEN
                      })


    I'm following the template from the docs -> https://www.zabbix.com/documentation...rence/host/get Retrieving data by name

    I am trying to use the '*' character to indicate that whatever comes after the first couple of characters can be anything. Sadly, this approach doesn't seem to work. I didn't find anything helpful in the docs regarding this issue, so I am coming here to ask if this is even possible?

    Before someone suggests, yes this is the only way I want to do this, I don't want to sort by tags, templates, host groups etc..

    My zabbix version is 6.0
  • Answer selected by cesq at 15-07-2024, 16:09.
    Markku
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Sep 2018
    • 1781

    In "Python wording" search needs a dictionary, not a list, that's the reason for the syntax error.

    Markku

    Comment

    • PeterZielony
      Senior Member
      • Nov 2022
      • 146

      #2
      Try search instead of filter

      Hiring in the UK? Drop a message

      Comment

      • cesq
        Junior Member
        • Mar 2022
        • 5

        #3
        PeterZielony, thank you for the suggestion. I'm trying to figure out the correct syntax and have some problems.

        From the docs regarding the search option
        Accepts an array, where the keys are property names, and the values are strings to search for.
        It's only logical in my opinion that it should look something like this:

        Code:
        r = requests.post(ZABBIX_API_URL,
                          json={
                              "jsonrpc": "2.0",
                              "method": "host.get",
                              "params": {
                                  "output": ["hostid"],
                                  "search": [
                                      "host": [
                                          "FOO-*"
                                      ],
                                  ],
                                
                                },
                              "id": 2,
                              "auth": AUTHTOKEN
                              }
                          )
        This gave me a Python SyntaxError, so I tried another way:

        Code:
        r = requests.post(ZABBIX_API_URL,
                          json={
                              "jsonrpc": "2.0",
                              "method": "host.get",
                              "params": {
                                  "search": [
                                      "FOO-*"
                                  ],
                                
                                },
                              "id": 2,
                              "auth": AUTHTOKEN
                              }
                          )​
        This doesn't produce any errors, but I still don't get the expected output, I get every single host on my server.

        I must be doing something wrong but I am at loss as to what. Since the docs don't give any example of how to use the 'search' function, other than the few lines explaining the theory.

        Comment

        • Markku
          Senior Member
          Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
          • Sep 2018
          • 1781

          #4
          In "Python wording" search needs a dictionary, not a list, that's the reason for the syntax error.

          Markku

          Comment

          • cesq
            Junior Member
            • Mar 2022
            • 5

            #5
            Markku Ah, I understand. That was the issue it seems, now it works correctly. Thank you all for the help! Posting the correct syntax below.

            Note: It looks like the '*' syntax is not accepted, so you can't do "FOO-*", you have to do just "FOO-", it will return all names that contain the phrase. In my case it works but it's something to keep in mind.


            Code:
            r = requests.post(ZABBIX_API_URL,
                              json={
                                  "jsonrpc": "2.0",
                                  "method": "host.get",
                                  "params": {
                                      "search": {
                                            "host": "FOO-"
                                        }
                                    
                                    },
                                  "id": 2,
                                  "auth": AUTHTOKEN
                                  }
                              )

            Comment

            Working...