Skip to content

Instantly share code, notes, and snippets.

@gkze
Last active August 29, 2015 14:16
Show Gist options
  • Save gkze/a7f81c646c20eb157ebb to your computer and use it in GitHub Desktop.
Save gkze/a7f81c646c20eb157ebb to your computer and use it in GitHub Desktop.
Consul members in JSON
import json
import re
import subprocess
def get_data():
patt = re.compile("[^\s]+")
p = subprocess.Popen(['consul', 'members', '-detailed'], shell=False, stdout=subprocess.PIPE)
d, _ = p.communicate()
return json.dumps(list(map(lambda i: {'name': i[0], 'ip': i[1].split(':')[0], 'port': i[1].split(':')[1], 'status': i[2], 'tags': dict(map(lambda item: item.split('='), i[3].split(',')))}, filter(lambda x: x, map(lambda e: patt.findall(e), d.decode().split('\n')[1:])))))
def main():
print(get_data())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment