Skip to content

Instantly share code, notes, and snippets.

@alienth
Last active November 20, 2015 20:50
Show Gist options
  • Save alienth/a77546f7eee07ae8f9cf to your computer and use it in GitHub Desktop.
Save alienth/a77546f7eee07ae8f9cf to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import re
import requests
import time
import sys
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
def toHex(l):
return ''.join([format(int(x) & 0xff, '02x') for x in l])
host = 'localhost'
port = '4242'
def lookupUID(host, port, uid, uid_type):
request_string = 'http://' + host + ':' + port + '/api/uid/uidmeta?uid=' + uid + '&type=' + uid_type
print(request_string)
r = requests.get(request_string)
if r.status_code == 200:
return r.json()['name']
print(r.status_code)
p = re.compile(r'key=\[[^]]+\]')
d = re.compile(r'-?\d+')
while True:
line = sys.stdin.readline()
if line:
m = p.findall(line)
for key in [ d.findall(x) for x in list(set(m)) ]:
print "hex"
print [toHex(x) for x in key]
metric = lookupUID(host, port, toHex(key[0:3]), 'metric')
tags = [toHex(x) for x in chunks(key[7:], 3)]
tagk = [lookupUID(host, port, x, 'tagk') for x in tags[::2]]
tagv = [lookupUID(host, port, x, 'tagv') for x in tags[1::2]]
print(key, metric, tagk, tagv, tags)
if not m:
sys.stdout.write(line)
sys.stdout.flush()
else:
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment