Created
October 28, 2015 21:07
-
-
Save ckolos/2a835e95588e94b772bc to your computer and use it in GitHub Desktop.
DataDog_Metric_hourly_query.py
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
#!/usr/bin/env python2 | |
# File: DataDog_Metric_hourly_query.py | |
# Chris Kolosiwsky | |
# Last Modified: Wed Oct 28 16:02:13 CDT 2015 | |
# License: GNU Public License (http://www.gnu.org/copyleft/gpl.html) | |
from datadog import initialize, api | |
from time import strptime, mktime, strftime, localtime | |
from os import getenv | |
import json, argparse, sys | |
try: | |
DD_API_KEY = getenv('DD_API_KEY') | |
except: | |
print 'You must export your DD_API_KEY prior to running this script' | |
sys.exit(1) | |
try: | |
DD_APP_KEY = getenv('DD_APP_KEY') | |
except: | |
print 'You must export your DD_APP_KEY prior to running this script' | |
sys.exit(1) | |
options = { | |
'api_key': DD_API_KEY, | |
'app_key': DD_APP_KEY | |
} | |
parser = argparse.ArgumentParser(description='Fetch 1 days worth of data from dd with per second resolution by hour') | |
parser.add_argument('--startday', help='use YYYYMMDD format', required="True") | |
parser.add_argument('--endday', help='use YYYYMMDD format', required="True") | |
parser.add_argument('--query', help='Ex: avg:system.cpu.user{*}; find this in a graph\'s json view on dd', required="True") | |
parser.add_argument('--debug', help="dump extra info during runtime", required=False) | |
parser.add_argument('--output', help="write query output to a file", required=False, type=argparse.FileType('w'), default=sys.stdout) | |
args = parser.parse_args() | |
if args.debug: | |
print "Received the following args:" | |
for k,v in vars(args).items(): | |
print k,v | |
initialize(**options) | |
startday=strptime(args.startday, "%Y%m%d") | |
endday=strptime(args.endday, "%Y%m%d") | |
start_sec=mktime(startday) | |
end_sec=mktime(endday)+86400 | |
if args.debug: | |
print "startday:", startday | |
print "endday:", endday | |
print "start_sec:", start_sec | |
print "end_sec:", end_sec | |
start = float(start_sec) | |
total = 0 | |
while start < end_sec: | |
end = start + 3600.0 | |
if args.debug: print "Querying for period starting",strftime("%c", localtime(start)),"and ending", strftime("%c", localtime(end)) | |
query = args.query | |
results = api.Metric.query(start=start - 3600, end=end, query=query) | |
if args.debug: print json.dumps(results,indent=2, separators=(',', ': ')) | |
for item in results['series'][0]['pointlist'][0:-1]: | |
query_output=str(item[0:2]) | |
args.output.write(query_output) | |
args.output.write("\n") | |
start = end | |
args.output.close() |
personal preference, but I usually expect debug output to be on stderr instead of stdout.
if end = start + 3600.0
on line 55 you probably shouldn't also do api.Metric.query(start=start - 3600, ...
on line 58
lines 61-63 could be just args.output.write("%s\n" % item[0:2])
everything else looks good if it works
edit: s/&/%/
ah good catch on 58; I was just starting to look at that funny
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pull per second stats from dd for each hour of the given start/stop period