Ad Widget

Collapse

Zabbix auto remove active agent if agent unreachable for more than day

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PratapSingh
    Junior Member
    • Apr 2016
    • 29

    #1

    Zabbix auto remove active agent if agent unreachable for more than day

    Hi ,

    I have almost every host monitored using active agent. Hence Auto Remove discovery rule is not working for me as suggested in document. Can someone point me to the right direction to enable auto remove for active agent if those are unreachable for more than 24 hours?

    I looked at forum thread as well but facing some issue as the code seems to be not working with the latest zabbix api.

    Any help would be appreciated.
  • PratapSingh
    Junior Member
    • Apr 2016
    • 29

    #2
    Can somebody tell me what exactly I have to look into api response to find out down/unresponsive agent/host? Also all the hosts are being monitored using active agent so I am finding very hard to find api response data in this case. However I have script ready to delete the host by host id but I am not sure what parameter to look into api response to find out unresponsive/down hosts from zabbix api.

    Any clue/help would be appreciated.

    Comment

    • PratapSingh
      Junior Member
      • Apr 2016
      • 29

      #3
      Not sure if I have written a perfect code yet but I run a cron now with below code and it removes hosts for which trigger value is "Zabbix agent on {HOST.NAME} is unreachable for 5 minutes"

      Code:
      #!/usr/bin/python
      def removehost():
          print "in removehost function"
          import time
          from zabbix_api import ZabbixAPI, Already_Exists
          z = ZabbixAPI(server="https://zabbix.example.com/zabbix")
          z.login("exampleuser", "exampleuser")
          for trigger in z.trigger.get({"output": [ "triggerid", "description", "priority" ], "filter": { "value": 1 }, "sortfield": "priority", "sortorder": "DESC"}):
              if trigger["description"] == 'Zabbix agent on {HOST.NAME} is unreachable for 5 minutes':
                  trigmsg = z.trigger.get({"triggerids": trigger["triggerid"], "selectHosts": "extend"})
                  for tm in trigmsg:
                      for l in tm['hosts']:
                          print l['name'], l['hostid']
                          print "Will kill host " + l['hostid'] + " " + l['host'] + trigger["description"]
                          sendmail(l['hostid'], l['host'], trigger["description"])
                          z.host.delete( [int(l['hostid'])] )
      
      def sendmail(hostid, host, triggermessage):
          print "in sendmail function"
          import smtplib
          SERVER = "localhost"
          FROM = "[email protected]"
          TO = ["[email protected]"] # must be a list
          SUBJECT = "Host removed from Zabbix %s" % host
          TEXT = "Host is not responding!! " + "Will kill host " + hostid + " "  + host + "  Reason: " + triggermessage
          # Prepare actual message
          message = """Subject: %s
      
          %s
          """ % (SUBJECT, TEXT)
          print message
          # Send the mail
          server = smtplib.SMTP(SERVER)
          server.sendmail(FROM, TO, message)
          server.quit()
      
      if __name__ == "__main__":
          removehost()

      Comment

      Working...