Last active
August 29, 2015 14:16
-
-
Save gkze/a7f81c646c20eb157ebb to your computer and use it in GitHub Desktop.
Consul members in JSON
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
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