Skip to content

Instantly share code, notes, and snippets.

@sacreman
Created May 11, 2016 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sacreman/abff09ab812681adf71cfe05020fee41 to your computer and use it in GitHub Desktop.
Save sacreman/abff09ab812681adf71cfe05020fee41 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from gevent import monkey
monkey.patch_all()
from gevent.pool import Pool
import requests
import uuid
settings = {
'url': 'https://app.dataloop.io/api/v1',
'org': 'yolo',
'account': '',
'key': 'redacted'
}
ping_pool = Pool(200)
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 payload(agent_name):
return {
'mac': '',
'hostname': agent_name,
'os_name': '',
'os_version': '',
'processes': [],
'container': '',
'interfaces': [],
'mode': 'SOLO',
'name': agent_name
}
def ping_agent(n, f):
settings['account'] = 'infrastructure'
url = build_url(settings, 'agents') + '/' + f + '/ping'
#print url
return requests.post(url, json=payload(n), headers=headers(), timeout=5)
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'])
ping_pool.spawn(ping_agent, agent['name'], finger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment