Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove dummy grafana_rule hosts from Sensu
#!/usr/bin/python
#
# Removes grafana_rule_* clients from Sensu
# Copyright (c) Alexey Baikov <sysboss[at]mail.ru>
#
# This script cleans up "successful" grafana rules
# that left in Sensu clients list.
import sys
import json
import requests
# Sensu API credentials
sensu_api_endpoint = "http://sensu.fqdn.domain:4567"
sensu_username = "_api_admin_user_"
sensu_password = "_api_admin_secret_"
# Variables
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
def sensuGet( req ):
return requests.get( sensu_api_endpoint + req,
auth = (sensu_username, sensu_password),
headers = headers
)
def sensuDelete( req ):
return requests.delete( sensu_api_endpoint + req,
auth = (sensu_username, sensu_password),
headers = headers
)
def sensuRemoveHost( hostname ):
sensuDelete('/clients/' + hostname)
if __name__ == '__main__':
# Get sensu client list
sensu_clients = json.loads( sensuGet('/clients').text )
for sensu_client in sensu_clients:
# filter grafana rules
if "grafana_rule" in sensu_client["name"]:
# get rule status
status = json.loads( sensuGet('/results/'+sensu_client["name"]).text )
print("Rule: {}, Has status: {}".format(sensu_client["name"], status[0]["check"]["status"]))
# we want to remove only "successful" rules
# as they are not relevant any more
if status[0]["check"]["status"] == 0:
print("Delete!")
sensuRemoveHost( sensu_client["name"] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment