Created
April 21, 2016 13:19
-
-
Save sacreman/da9b4e4ab9d1e49fc2a2d4d79bccf3a9 to your computer and use it in GitHub Desktop.
This file contains 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 python | |
from gevent import monkey, socket | |
monkey.patch_all() | |
from gevent.pool import Pool | |
import requests | |
import uuid | |
settings = { | |
'url': 'https://app.dataloop.io/api/v1', | |
'org': '', | |
'account': '', | |
'key': '' | |
} | |
metric_pool = Pool(1000) | |
graphite_server = 'graphite.dataloop.io' | |
graphite_port = 2003 | |
metrics = ['base.cpu', | |
'base.memory', | |
'base.swap', | |
'base.load_1_min', | |
'base.load_fractional', | |
'base.net_download', | |
'base.net_upload'] | |
landing_account = 'infrastructure' | |
UUID_HASH = uuid.UUID('12345678123456781234567812345678') | |
def headers(): | |
return { | |
"Content-type": "application/json", | |
"Authorization": "Bearer " + settings['key'] | |
} | |
def hash_id(id): | |
return str(uuid.uuid5(UUID_HASH, id)) | |
def build_url(settings, endpoint='', orglevel=False, accountlevel=False): | |
url = settings['url'] | |
org = settings['org'] | |
account = settings['account'] | |
if orglevel: | |
return url + '/orgs' | |
if accountlevel: | |
return url + '/orgs/' + org + '/accounts' | |
return url + '/orgs/' + org + '/accounts/' + account + '/' + endpoint | |
def get_agent_series(a, f, m): | |
settings['account'] = a | |
url = build_url(settings, 'metrics') + '/' + m + '/series?source=' + f + '&resolution=' + str(30) + '&period=' + str(60) | |
return requests.get(url, headers=headers()).json() | |
def send_metrics(s, f): | |
for m in metrics: | |
sock = socket.create_connection((graphite_server, graphite_port)) | |
metric_path = hash_id(f) + '.' + m | |
metric_value = get_agent_series(s, f, m)[0]['points'][0]['avg'] | |
message = "%s %d" % (metric_path, metric_value) | |
sock.sendall(message) | |
accounts = requests.get(build_url(settings, accountlevel=True), headers=headers()).json() | |
for account in accounts: | |
if account['name'] != landing_account: | |
settings['account'] = account['name'] | |
agents = requests.get(build_url(settings, 'agents'), headers=headers()).json() | |
for agent in agents: | |
finger = hash_id(agent['id']) | |
metric_pool.spawn(send_metrics, account['name'], agent['id']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment