Ad Widget

Collapse

How to get only active problems via API ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pigi_102
    Member
    • Sep 2021
    • 35

    #1

    How to get only active problems via API ?

    Hello all,
    I'm trying to write a script to get all the active problems from the zabbix servers, to send them as a recap one time a day.
    I would like to use the API as it should be the perfect way but I'm having trouble to distinct the RESOLVED ones.

    This, at the moment, is my python script:
    Code:
    #!/usr/bin/python3
    
    import requests
    import json
    
    # Zabbix server URL
    zabbix_url = 'https://MY_SERVER/zabbix/api_jsonrpc.php'
    
    # Your API token
    api_token = 'MY_VERY_SECURE_AUTH_KEY'
    
    # Get active problems with start date
       problem_payload = {
        "jsonrpc": "2.0",
        "method": "event.get",
        "params": {
          "output": ["eventid", "name", "severity", "acknowledged", "suppressed", "clock"], # 'clock' is the start date
          "selectHosts": ["host"], # Include hostname
          "filter": {
          "value": 1 # Filter for active problems
          },
        "sortfield": "clock",
        "sortorder": "ASC"
        },
        "auth": api_token,
        "id": 1
    }
    
    # Make the request while ignoring SSL verification
    response = requests.post(zabbix_url, json=problem_payload, verify=False)
    
    # Check if the request was successful
    if response.status_code == 200:
       active_problems = response.json().get('result', [])
    
      # Print active problems with their start dates
      for problem in active_problems:
        acknowledged_state = 'Acknowledged' if problem['acknowledged'] == 1 else 'Not Acknowledged'
        hosts = ', '.join([host['host'] for host in problem['hosts']]) if 'hosts' in problem else 'Unknown Host'
        print(f"Problem ID: {problem['eventid']}, Description: {problem['name']}, Severity: {problem['severity']}, State: {acknowledged_state}, Host: {hosts},      Start Date: {problem['clock']}")
    else:
        print(f"Error: {response.status_code} - {response.text}")
    It basically works, except that it extract every event since when we started zabbix server.
    I would like to exclude all the event that are "automagically" resolved by Zabbix, but can't find an attribute in api that I can use as a filter.

    Just for clarify I would like to "emulate" the extraction you can do from the webui when you go in:
    Monitoring-> Problems and choose "Problems" then "Extract CSV".

    I cannot even find a way to get the status of the event ( I could easly filter them by using grep )

    Does someone have had better luck than me about this ?

    ​Thanks in advance

    Pierluigi
  • ipse
    Junior Member
    • Jan 2025
    • 1

    #2
    Hello!
    Is it still actual?
    I have the same problem.

    Comment

    • skeletor
      Junior Member
      • May 2016
      • 12

      #3
      Did you tried method "problem.get" instead of "event.get"? https://www.zabbix.com/documentation...erence/problem

      Comment

      Working...