Skip to content

Instantly share code, notes, and snippets.

@mapledyne
Created March 29, 2018 16:00
Show Gist options
  • Save mapledyne/5396e04c18f990ab16753dc8672bbb41 to your computer and use it in GitHub Desktop.
Save mapledyne/5396e04c18f990ab16753dc8672bbb41 to your computer and use it in GitHub Desktop.
Uberdog: get data dog status and display for Ubersicht
"""
Uberdog returns a small clip of HTML for of Datadog monitors.
This script gets the Datadog status of monitors and creates a small clip of
HTML which can then be displayed by Ubersicht. This widget leverages
FontAwesome since they make great web icons.
Three environment variables to be aware of:
* DATADOG_API: Your DataDog API key
* UBERDOG_APP: The APP key specific to this app_key
* UBERDOG_INCLUDE_OK: Set to 'True' if you want the list to display monitors
that aren't alerting (Status = 'OK'). Defaults to False.
"""
import datadog
import os
# These are the various monitor states from Datadog, except 'Unknown' which
# is used for any other state that comes back (being, then, an unknown state :)
status_icons = {
"Alert": "<i class='fa fa-times-circle' style='color:red'></i>",
"Warn": "<i class='fa fa-exclamation-circle' style='color:yellow'></i>",
"No Data": "<i class='fa fa-clock-o' style='color:yellow'></i>",
"OK": "<i class='fa fa-check-circle' style='color:green'></i>",
"Unknown": "<i class='fa fa-question-circle' style='color:yellow'></i>"
}
options = {
'api_key': os.environ['DATADOG_API'],
'app_key': os.environ['UBERDOG_APP']
}
include_ok = False
try:
if os.environ['UBERDOG_INCLUDE_OK'].upper() == 'TRUE':
include_ok = True
except KeyError:
# KeyError just means that this var wasn't set, so leaving at default
# seems pretty legit:
pass
datadog.initialize(**options)
monitors = datadog.api.Monitor.get_all()
print("Datadog Monitors:<BR>")
alerts = 0
total = 0
warn = 0
for monitor in monitors:
state = monitor['overall_state']
total += 1
if state == 'Alert':
alerts += 1
if state == "Warn":
alerts += 1
if state == 'OK' and not include_ok:
continue
icon = status_icons.get(monitor['overall_state'], status_icons['Unknown'])
print("{} {}<BR>".format(icon, monitor['name']))
print("Of {} total monitors, {} are alerting and {} are warning.".format(total, alerts, warn)) # noqa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment