Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python3.6
# This script call the prometheus target API and check the status of target's status.
# If it found any down in targets it will append failed_hosts list and print it with exit code 2
# Auther: arunlal.a
# Importing modules
import json
import requests
import collections
import sys
import socket
# Empty list
failed_hosts = []
# checking prometheus connectivity
s = socket.socket()
address = '0.0.0.0'
port = 9090
try:
s.connect((address, port))
except Exception as e:
print("Prometheus connectivity check failed %s:%d. Exception is %s" % (address, port, e))
sys.exit(2)
finally:
s.close()
# Calling Prometheus target API
req=requests.get('http://0.0.0.0:9090/api/v1/targets')
out=req.json()
out1=(out['data'])
dict=out1['activeTargets']
for i in dict:
if i['health'] == 'down':
str=i['labels']['instance']
failed_hosts.append(str.split(":")[0])
if len(failed_hosts) == 0:
print("All hosts' status are good!!")
else:
print("Following hosts have issues. You're loosing metrics. It's crutical check https://prometheus.your-domain.com/targets and fix asap!!")
for x in range(len(failed_hosts)):
print(failed_hosts[x])
sys.exit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment