1. 生成服务依赖关系
#!/usr/bin/pythonimport sysimport commandsimport jsondef genDependString(ip): url="curl --user admin:admin -H 'X-Requested-By:admin' 'http://{0}:8080/api/v1/stacks/HDP/versions/2.2/services?fields=StackServices/required_services&minimal_response=true' 2> /dev/null".format(ip) (status, output) = commands.getstatusoutput(url) if not status: items = json.loads(output).get("items") content = "" for item in items: service_name = item.get("StackServices").get("service_name") require_services = item.get("StackServices").get("required_services") content += ''' "ROOT" -> "{0}"\n'''.format(service_name) if not len(require_services): content += ''' "{0}" -> "NULL"\n'''.format(service_name) for require_service in require_services: content += ''' "{0}" -> "{1}"\n'''.format(service_name, require_service) content = "digraph G {\n" + content + "}" return contentif __name__ == '__main__': ip = sys.argv[1] print genDependString(ip)
2. 在线生成依赖关系图