Last active
September 27, 2017 06:13
-
-
Save sioncojp/d24eb96d7c253c5a1ab5 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/python | |
import urllib2 | |
import sys | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
MACKEREL_API_KEY='' | |
class MackerelInventory: | |
def getData(self, path, params=''): | |
url = 'https://mackerel.io/api/v0/' + str(path) + str(params) | |
headers = { 'X-Api-Key': MACKEREL_API_KEY } | |
request = urllib2.Request(url, None, headers) | |
response = urllib2.urlopen(request) | |
return json.loads(response.read()) | |
def getGroupList(self): | |
hosts = self.getHosts() | |
print json.dumps(hosts, indent=4) | |
def getHostInfo(self, hostname): | |
hosts = self.getHosts(hostname) | |
print json.dumps(hosts['_meta']['hostvars'][hostname], indent=4) | |
def getHosts(self, hostname=''): | |
if hostname != '': | |
params = '?name=' + hostname | |
inventories = { | |
'production': { 'children': [] }, | |
'_meta': { 'hostvars': {} }, | |
} | |
services = self.getData('services')['services'] | |
for t in services: | |
if t not in inventories['production']['children']: | |
inventories['production']['children'].append(t['name']) | |
if t['name'] not in inventories: | |
inventories[t['name']] = { 'children': [] } | |
hosts = self.getData('hosts.json?') | |
for host in hosts['hosts']: | |
ipAddress = None | |
for interface in host['interfaces']: | |
if interface['name'] == 'eth0': | |
ipAddress = interface['ipAddress'] | |
break | |
if ipAddress == None: | |
continue | |
for serviceNames, roles in host['roles'].iteritems(): | |
for t in roles: | |
role_name = t + '_' +serviceNames | |
if role_name not in inventories[serviceNames]['children']: | |
inventories[serviceNames]['children'].append(role_name) | |
if role_name not in inventories: | |
inventories[role_name] = { 'hosts': [] } | |
inventories[role_name]['hosts'].append(host['name']) | |
inventories['_meta']['hostvars'][host['name']] = { | |
'ansible_ssh_host': ipAddress, | |
'ansible_ssh_port': '22', | |
} | |
return inventories | |
mackerel = MackerelInventory() | |
if len(sys.argv) == 2 and (sys.argv[1] == '--list'): | |
mackerel.getGroupList() | |
elif len(sys.argv) == 3 and (sys.argv[1] == '--host'): | |
mackerel.getHostInfo(sys.argv[2]) | |
else: | |
print "Usage: %s --list or --host <hostname>" % sys.argv[0] | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment