Create a gist now

Instantly share code, notes, and snippets.

import json
import requests
from datetime import datetime, timedelta
import serial
from time import sleep
DEVICE_PATH = "/dev/YOUR_DEVICE"
token = "YOUR TOKEN"
subject_id = "SERVICE ID FROM Server Density"
# get start and end date via datetime
start = datetime.now() - timedelta(seconds=600)
end = datetime.now()
# build a filter
filter = {
'time':"all"
}
ser = serial.Serial(DEVICE_PATH, 9600, timeout=1)
sleep(5)
ser.write('california\n')
sleep(1)
ser.write('uk\n')
sleep(1)
ser.write('moscow\n')
sleep(1)
ser.write('sydney\n')
sleep(1)
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)
}
)
for stat in api_response.json()[0]['tree']:
name = stat['name']
timing = stat['data'][0]['y']
if timing > 0.4:
print "Slow!"
if name == "USA: North California":
ser.write('california\n')
sleep(1)
if name == "UK: London (Vultr)":
ser.write('uk\n')
sleep(1)
if name == "Russia: Moscow":
ser.write('moscow\n')
sleep(1)
if name == "Australia: Sydney (Vultr)":
ser.write('sydney\n')
sleep(1)
print "{0} -> {1}".format(name, timing)
sleep(600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment