Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Fetch stats about a service's response time from Server Density.
import json
import requests
from datetime import datetime, timedelta
import serial
from time import sleep
from copy import deepcopy
DEVICE_PATH = "/dev/tty.usbmodem1421"
token = "YOUR_TOKEN"
subject_id = "SERVICE_ID"
# get start and end date via datetime
start = datetime.now() - timedelta(seconds=600)
end = datetime.now()
# build a filter
filter = {
'time':"all"
}
try:
ser = serial.Serial(DEVICE_PATH, 9600, timeout=1)
except:
ser = serial.Serial("/dev/ARUDINO_DEVICE", 9600, timeout=1)
sleep(5)
lookup_table = {
"Australia: Sydney (Vultr)": "sydney ",
"Chile: Vina del Mar": "vinadelmar ",
"Germany: Frankfurt (Amazon)": "frankfurt ",
"Italy: Milan": "milan ",
"Japan: Tokyo (Amazon)": "toyko ",
"UK: London (Vultr)": "london ",
"South Africa: Johannesburg": "johannesburg ",
"Russia: Moscow": "moscow ",
"Sweden: Stockholm": "stockholm ",
"USA: North California": "northcalifornia "
}
while (True):
api_response = requests.get(
'https://api.serverdensity.io/metrics/graphs/{0}'.format(subject_id),
params={
'token': token,
'start' : start.isoformat(),
'end': end.isoformat(),
'filter': json.dumps(filter)
}
)
actors = deepcopy(lookup_table)
for stat in api_response.json()[0]['tree']:
name = stat['name']
timing = stat['data'][0]['y']
print name
serial_command = lookup_table[name]
if timing > 0.4:
print "Slow!"
serial_command += "slow\n"
else:
serial_command += "up\n"
print "writing " + serial_command
ser.write(serial_command)
sleep(1)
del actors[name]
print "{0} -> {1}".format(name, timing)
for actor, name in actors.iteritems():
serial_command = lookup_table[actor] + "down\n"
ser.write(serial_command)
sleep(600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment