Ad Widget

Collapse

Zabbix HTTP Agent API request from my zabbix server - need another set of eyes.​

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chamlee@att.net
    Junior Member
    • Mar 2025
    • 9

    #1

    Zabbix HTTP Agent API request from my zabbix server - need another set of eyes.​

    I am trying to get the Zabbix HTTP Agent item configured to pull API data from my Zabbix server through my proxy.

    1. I have written a script in python to use the Zabbix API and it returns the expected data. Along with a json formatted response this is the script output "Number of problems with tags 'device=camera' and 'status=offline' and 'technician=belair': 22"

    import requests
    import json
    # Replace with your Zabbix server URL and credentials
    zabbix_url = "http://myserver.yisd.net/zabbix/api_jsonrpc.php"
    api_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx xxxxxxxxxxxxxxx"
    # Headers for the request
    headers = {
    'Content-Type': 'application/json-rpc',
    'Authorization': f'Bearer {api_token}'
    }
    # Data for the request
    data = {
    'jsonrpc': '2.0',
    'method': 'problem.get',
    'params': {
    'output': 'extend',
    'selectTags': 'extend',
    'tags': [
    {'tag': 'status', 'value': 'offline'},
    {'tag': 'technician', 'value': 'delvalle'},
    {'tag': 'device', 'value': 'camera'}
    ]
    },
    'id': 1
    }
    # Send the request
    response = requests.post(zabbix_url, headers=headers, json=data)
    # Check for errors
    if response.status_code != 200:
    print(f"Error: {response.status_code}")
    # print(response.json())
    else:
    response_json = response.json()
    if 'error' in response_json:
    print(f"API Error: {response_json['error']}")
    else:
    problems = response_json.get('result', [])
    print(f"Number of problems with tags 'status=offline' and 'technician=belair': {len(problems)}")
    # print(response.json())

    2. However if I take the same json response body from my python script and use it for my HTTP Agent item it only returns this result {}. Here is my complete json response body being used

    {
    "jsonrpc": "2.0",
    "method": "problem.get",
    "params": {
    "output": "extend",
    "countOutput": "true",
    "selectTags": "extend",
    "tags": [
    {"tag": "status", "value": "offline"},
    {"tag": "technician", "value": "delvalle"},
    {"tag": "device", "value": "camera"}
    ]
    }
    }

    Screenshots of my HTTP Agent configuration

    Click image for larger version

Name:	Screenshot 2025-03-31 093846.png
Views:	299
Size:	32.7 KB
ID:	501222

    Click image for larger version

Name:	Screenshot 2025-03-31 100607.png
Views:	300
Size:	35.9 KB
ID:	501221

    This is the result using the HTTP Agent item test button

    Click image for larger version

Name:	Screenshot 2025-03-31 101645.png
Views:	456
Size:	22.0 KB
ID:	501220


    3. I have tried performing an API request using curl for something very basic and for my failing json request body on the same proxy server

    This curl version of my request returns an error


    curl --header "Content-Type: application/json" --request POST --data '{ "jsonrpc": "2.0", "method": "problem.get", "params": { "username": "zabbixapi_ro", "password": "xxxxxxxxxxxxxx", "output": "extend", "countOutput": "true", "selectTags": "extend", "tags": [{"tag": "status", "value": "offline"}, "tag": "technician", "value": "delvalle"},{"tag": "device", "value": "camera"}]},"id":1}' "http://zabbix1.yisd.net/zabbix/api_jsonrpc.php"

    Returns this error
    {"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":"Invalid JSON. An error occurred on the server while parsing the JSON text."},"id":null}

    This curl command on my proxy returns the expected result

    curl --header "Content-Type: application/json" --request POST --data '{"jsonrpc": "2.0", "method": "user.login", "params": {"username": "zabbixapi_ro", "password": "xxxxxxxxxxxxxxx"}, "id": 1}' "http://myserver.yisd.net/zabbix/api_jsonrpc.php"

    Good Result
    {"jsonrpc":"2.0","result":"xxxxxxxxxxxxxxxxxxxx xxx xxxxxxxxx","id":1}zabbix@zabbixproxy2

    This has led me to believe that I am missing some configuration requirement in the HTTP Agent configuration page. I have tried every possible variation from documentation and so at this point I am hoping just another set of eyes will be helpful.​​
  • mrnobody
    Member
    • Oct 2024
    • 61

    #2
    If i understand right, your result would be "22", right? So, try change Type of information from Texto to Numeric

    Comment

    • chamlee@att.net
      Junior Member
      • Mar 2025
      • 9

      #3
      Correct answer should be 22. If I change the type of information field to "Numeric (unsigned)" or "Numeric (float)" I get this after the test.

      Click image for larger version

Name:	Screenshot 2025-04-01 140308.png
Views:	284
Size:	29.2 KB
ID:	501297

      Comment

      • chamlee@att.net
        Junior Member
        • Mar 2025
        • 9

        #4
        If I change the "Retrieve Mode" in the HTTP Agent GUI to "Body and Headers" I get all the header values but still no body values

        Click image for larger version

Name:	Screenshot 2025-04-01 150434.png
Views:	290
Size:	37.7 KB
ID:	501299

        Comment

        • chamlee@att.net
          Junior Member
          • Mar 2025
          • 9

          #5
          All of the Zabbix examples\documentation seems to contains"id": 1 line in their json request body examples

          I have noticed that this json request body with the line "id": 1
          {
          "jsonrpc": "2.0",
          "method": "problem.get",
          "params": {
          "output": "extend",
          "countOutput": "true",
          "selectTags": "extend",
          "tags": [
          {"tag": "status", "value": "offline"},
          {"tag": "technician", "value": "delvalle"},
          {"tag": "device", "value": "camera"}
          ]
          },
          "id": 1
          }

          Gives me this error
          {"body":{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Not authorized."},"id":1}}

          ....but this json request body without "id": 1
          {
          "jsonrpc": "2.0",
          "method": "problem.get",
          "params": {
          "output": "extend",
          "countOutput": "true",
          "selectTags": "extend",
          "tags": [
          {"tag": "status", "value": "offline"},
          {"tag": "technician", "value": "delvalle"},
          {"tag": "device", "value": "camera"}
          ]
          }
          }

          Gives me this result
          {}

          Comment

          • Brambo
            Senior Member
            • Jul 2023
            • 245

            #6
            What I don't get is why you set Basic HTTP auth, when you already connect with a bearer token?
            I would first broaden the request and not sure but if you filter with tags you also must specify how you filter, exist, doesn't exist, equals, doesn't equals etc.
            You are now missing the operator.
            In your first post in section 3: "tags": [{"tag": "status", "value": "offline"}, "tag": "technician", "value": "delvalle"},{"tag": "device", "value": "camera"}]}
            The second tag is invalid json. (missing opening { )
            Take a look at https://www.zabbix.com/documentation...ce/problem/get on how to work with the API

            Comment

            • chamlee@att.net
              Junior Member
              • Mar 2025
              • 9

              #7
              That Basic HTTP auth was just over configuring as a result of trying tooooo many combinations. I have taken out the Authorization Bearer portion and left the Basic HTTP auth option. I have corrected the "{" issue with the tag. What is the most basic request body I could put in just enough to test the other settings to verify that they are working. Right now I am thinking there is something wrong in my request body that filters out all my data, even though the same request body works fine in my python script.

              Comment

              • chamlee@att.net
                Junior Member
                • Mar 2025
                • 9

                #8
                I had not noticied the operator option before, odd that none of the examples utilize it. I reworked the response body with the operator option but still only get {} as a response.

                Here is my new version. I also simplified the tag option to just one tag in case I was over filtering somehow.
                {
                "jsonrpc": "2.0",
                "method": "problem.get",
                "params": {
                "output": "extend",
                "countOutput": "true",
                "selectTags": "extend",
                "tags": [
                { "tag": "technician", "value": "delvalle", "operator": "4" }
                ]
                }
                }

                Comment

                • Brambo
                  Senior Member
                  • Jul 2023
                  • 245

                  #9
                  Did you read the link I posted? If so, do you see tags as a parameter?
                  selectTags is one, tags isn't. selectTags: "Return a tags property with the problem tags. Output format: [{"tag": "<tag>", "value": "<value>"}, ...]."
                  There is a filter parameter
                  As it seems you want to filter on a certain technicians name. Maybe your approach should be: get userID > use this user ID and the " Retrieving problems acknowledged by specified user" example.
                  I don't use this part of the API the example in the manual should be enough.

                  Comment

                  Working...