Skip to content

Instantly share code, notes, and snippets.

@sysboss
Created February 9, 2018 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sysboss/d9889c07dce1a238130e6b6204f925de to your computer and use it in GitHub Desktop.
Save sysboss/d9889c07dce1a238130e6b6204f925de to your computer and use it in GitHub Desktop.
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