forked from DataDog/Miscellany
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullmetrics_dash.py
102 lines (75 loc) · 2.54 KB
/
fullmetrics_dash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from datadog import initialize, api
import requests
import json
import time
import sys
import argparse
## Arguments to run the script
parser = argparse.ArgumentParser()
parser.add_argument("--verbosity", type=int, help="increase output verbosity, use --verbosity 1 to enable")
parser.add_argument("integration", type=str,
help="specify the integration namespace for your dashboard. Ex: mysql, ntp, system")
args = parser.parse_args()
integration = args.integration
## Keys and timeframe
api_key = "***"
app_key = "***"
timestamp = int(time.time()) - 86400
## Init
options = {
'api_key': str(api_key),
'app_key': str(app_key)
}
initialize(**options)
## Metrics endpoint
r = requests.get("https://app.datadoghq.com/api/v1/metrics?api_key="+ api_key + "&application_key=" + app_key + "&from="+str(timestamp)+"")
r.json()
metrics_list = []
print "init: ", metrics_list
for i in range(len(r.json()['metrics'])):
if integration in r.json()['metrics'][i]:
metrics_list.append(r.json()['metrics'][i])
## Resiliency: Test if metrics are available for the integration
if len(metrics_list)>0:
print "you have", len(metrics_list), "metrics available for "+ integration
else:
print "you don't have any metrics available for "+ integration + " try a longer timeframe or check the name of the integration"
exit()
## Building the dashboard
title = integration
description = "All your "+ integration + " metrics"
graphs = []
print "building dashboard: ", title
## Building each widget
for i in range(len(metrics_list)):
#DEBUG
if args.verbosity == 1:
print "building widget: ", metrics_list[i]
graphs.append({
"definition": {
"events": [],
"requests": [
{"q": "avg:" + str(metrics_list[i]) + "{$host} by {host}"} # changing unicode str to regular str
],
"viz": "timeseries"
},
"title": str(metrics_list[i])
})
template_variables = [{
"name": "host",
"prefix": "host",
"default": "*"
}]
read_only = True
#DEBUG
if args.verbosity == 1:
print "this is your graphs", graphs
try:
dashboard = api.Timeboard.create(title=title, description=description, graphs=graphs, template_variables=template_variables, read_only=read_only)
print "dashboard for " + integration + " was successfully created check it out here: http://app.datadoghq.com/dash/"+ str(dashboard['dash']['id'])
#DEBUG
if args.verbosity == 1:
print dashboard
except:
print "Something went wrong, enable debug log by running python fullmetrics_dash.py <integration> 1, current error: ", sys.exc_info()[0]
raise