-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgetPorts.py
100 lines (71 loc) · 2.42 KB
/
getPorts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
## ********************************************************************************
## getPorts.py
##
## Example of how to retrieve Service ports using cm-api v6 for CM v5.0
##
## Usage: getPorts.py
##
## (set CM connection properties in the script)
##
## ********************************************************************************
## ** imports *******************************
import sys
from cm_api.api_client import ApiResource
## ** CM Connection Settings ******************************
## Cloudera Manager Host and Port
cm_host = "localhost"
cm_port = "7180"
## Cloudera Manager login
cm_login = "admin"
## Cloudera Manager password
cm_password = "admin"
## Cluster Name
cluster_name = "Cluster 1"
## CM API Version
## See the chart here to get the right value: http://cloudera.github.io/cm_api/docs/releases/
## I'll default to v10 of the API for Cloudera Manager 5.4
cm_api_version = "10"
## ** function defs ***************************
def getHostNameForHostId(api, hostId):
for host in api.get_all_hosts():
if host.hostId == hostId:
return host.hostname
return "hostname not found for hostId: " + hostId
def getRoleURLs(role):
list = []
config = role.get_config('full')
for property_name in config:
if "port" in property_name:
port = config[property_name].value
if port == None:
custom_port = False
port = config[property_name].default
else:
custom_port = True
hostname = getHostNameForHostId(api, role.hostRef.hostId)
url = hostname + ":" + port
property_name = property_name.replace('_', '.')
item = role.type.ljust(20) + property_name.ljust(55) + url.ljust(40)
if custom_port:
item = item + "(custom port)"
list.append(item)
return list
## ** Connect to CM ***************************
print ""
print "Connecting to Cloudera Manager at : http://" + cm_host + ":" + cm_port
print ""
api = ApiResource(server_host=cm_host, server_port=cm_port, username=cm_login, password=cm_password, version=cm_api_version)
cluster = api.get_cluster(cluster_name)
for service in cluster.get_all_services():
print service.type + " Service " + "(" + service.displayName + ")"
print "*****************************"
list = []
for role in service.get_all_roles():
for url in getRoleURLs(role):
list.append(url)
list.sort()
for item in list:
print item
print ""
print ""