-
-
Save ryanmaclean/d5f577278671a4526091e778003ed461 to your computer and use it in GitHub Desktop.
How to create a custom dashboard with the Datadog API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dogapi import dog_http_api as api | |
##### Parameters ##### | |
# Credentials | |
api.api_key = 'my_api_key' | |
api.application_key = 'my_application_key' | |
# Dashboard information | |
title = "Test Quentin via API" | |
description = "Dashboard created via the API" | |
# Metrics | |
METRICS = { | |
"SYSTEM_METRICS": """ | |
system.cpu.system | |
system.mem.free | |
""" | |
,"ES_COUNTERS": """ | |
elasticsearch.docs.count | |
elasticsearch.docs.deleted | |
elasticsearch.flush.total | |
elasticsearch.flush.total.time | |
elasticsearch.indexing.delete.total | |
elasticsearch.indexing.index.total | |
elasticsearch.merges.total | |
elasticsearch.merges.total.docs | |
elasticsearch.merges.total.size | |
elasticsearch.merges.total.time | |
elasticsearch.number_of_data_nodes | |
elasticsearch.number_of_nodes | |
elasticsearch.process.open_fd | |
elasticsearch.refresh.total | |
elasticsearch.refresh.total.time | |
elasticsearch.search.fetch.total | |
elasticsearch.search.query.total | |
elasticsearch.store.size | |
""" | |
,"ES_GAUGES": """ | |
elasticsearch.active_primary_shards | |
elasticsearch.active_shards | |
elasticsearch.cluster_status | |
elasticsearch.get.missing.total | |
elasticsearch.get.time | |
elasticsearch.get.total | |
elasticsearch.http.current_open | |
elasticsearch.http.total_opened | |
elasticsearch.indexing.delete.current | |
elasticsearch.indexing.delete.time | |
elasticsearch.indexing.index.current | |
elasticsearch.indexing.index.time | |
elasticsearch.initializing_shards | |
elasticsearch.merges.current | |
elasticsearch.merges.current.docs | |
elasticsearch.merges.current.size | |
elasticsearch.relocating_shards | |
elasticsearch.search.fetch.current | |
elasticsearch.search.fetch.time | |
elasticsearch.search.query.current | |
elasticsearch.search.query.time | |
elasticsearch.thread_pool.bulk.active | |
elasticsearch.thread_pool.bulk.queue | |
elasticsearch.thread_pool.bulk.threads | |
elasticsearch.thread_pool.flush.active | |
elasticsearch.thread_pool.flush.queue | |
elasticsearch.thread_pool.flush.threads | |
elasticsearch.thread_pool.generic.active | |
""" | |
} | |
# Hosts to display on each graph | |
HOSTS = """ | |
i-18c1087e | |
i-8163f6a1 | |
i-ad6734d0 | |
i-471cba3f | |
""" | |
def define_metric(metric, type="gauge"): | |
if type == "counter": | |
rate = True | |
else: | |
rate = False | |
return { | |
"name": metric, | |
"rate": rate | |
} | |
def define_graph(metric): | |
query = metric['name'] | |
if len(HOSTS) == 0: | |
HOSTS.append('*') | |
requests = [] | |
hosts = HOSTS.split('\n') | |
hosts = filter(None, hosts) | |
for h in hosts: | |
if h == '*': | |
q = query + "{*}" | |
else: | |
q = query + "{host:" + h.strip() + "}" | |
if metric['rate'] is True: | |
q = "rate(" + q + ")" | |
requests.append({"q": q}) | |
return { | |
"definition": { | |
"events": [], | |
"requests": requests, | |
"viz": "timeseries" | |
}, | |
"title": metric['name'] | |
} | |
# Generate graphs | |
graphs = [] | |
for key in METRICS: | |
if key.find("GAUGE") != -1: | |
type = "gauge" | |
else: | |
type = "counter" | |
metrics = METRICS[key].strip('\t').split('\n') | |
metrics = filter(None, metrics) | |
print metrics | |
for m in metrics: | |
metric = define_metric(m.strip(), type) | |
graph = define_graph(metric) | |
graphs.append(graph) | |
# Let's send the API request to create the dashboard | |
api.create_dashboard(title, description, graphs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment