4 # Copyright (c) 2016 NEC Corporation and others
7 # This program and the accompanying materials are made available under the
8 # terms of the Eclipse Public License v1.0 which accompanies this
9 # distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
18 # response code to check the rest calls
19 RESP_GET_SUCCESS = 200
23 con_header = {"Accept": "application/json", "content-type": "application/json"}
24 authentication = ("admin", "admin")
27 def validate_cluster(ipaddress):
28 """method to check the cluster status
30 ipaddress(str): ip address of the ODL controller
32 url = "http://" + ipaddress + ":8181/jolokia/read/akka:type=Cluster"
34 resp = requests.get(url, headers=con_header, auth=authentication)
35 except requests.exceptions.RequestException:
36 print("controller is unreachable")
39 if resp.status_code == RESP_NOT_FOUND:
40 print("jolokia not installed, resp code", resp.status_code)
41 print("Problem with accessing jolokia")
44 elif resp.status_code != RESP_GET_SUCCESS:
45 print("error in getting response, resp code", resp.status_code)
48 data = json.loads(resp.content)
49 cluster_status = data["value"]["ClusterStatus"]
50 status = json.loads(cluster_status)
51 members = status["members"]
53 entity_owner_list = []
55 for member in members:
56 # spliting the ip address of the node from json object
58 # "akka.tcp://opendaylight-cluster-data@10.106.138.137:2550"
59 ip = re.search("@(.+?):", member["address"]).group(1)
60 node_status = ip + "-" + member["status"]
61 member_list.append(node_status)
63 "http://" + ip + ":8181/jolokia/read/org.opendaylight.controller:"
64 "Category=ShardManager,name=shard-manager-operational,"
65 "type=DistributedOperationalDatastore"
67 resp1 = requests.get(url1, headers=con_header, auth=authentication)
68 if resp1.status_code != RESP_GET_SUCCESS:
69 print("error in getting response for the node", ip)
70 print("response content", resp1.content)
72 data2 = json.loads(resp1.content)
73 member_role = data2["value"]["MemberName"]
74 entity_owner_list.append(ip + ":" + member_role)
75 leader = data["value"]["Leader"]
77 leaderNode = leader[leader.index("@") + 1 : leader.rindex(":")]
78 for leader_node in member_list:
79 address = leader_node.split("-")
80 if address[0] == leaderNode:
81 print("=================== Leader Node ======================\n")
83 member_list.remove(leader_node)
84 print("=================== Follower Node ====================\n")
86 list_entity_owners(ipaddress, entity_owner_list)
89 def list_entity_owners(ipaddress, entity_owner_list):
90 """method to list the entity owners
92 ipaddress(str): ip address of the ODL controller
93 entity_owner_list(list): list of member role in cluster
95 entity = ":8181/restconf/operational/entity-owners:entity-owners"
96 url = "http://" + ipaddress + entity
97 resp = requests.get(url, headers=con_header, auth=authentication)
98 if resp.status_code != RESP_GET_SUCCESS:
99 print("controller is down, resp_code", resp.status_code)
100 print("response content", resp.content)
102 data = json.loads(resp.content)
103 ovsdb = data["entity-owners"]["entity-type"]
104 print("\n\n=================== Entity Details ===================\n")
106 entities = e_type["entity"]
107 for entity in entities:
109 if len(entity["owner"]) > 0:
110 print("NODE ID", str(id[id.rindex("=") + 2 : len(id) - 2]))
111 print("OWNER", str(entity["owner"]))
112 for owner in entity_owner_list:
113 owner_role = owner.split(":")
114 if entity["owner"] == owner_role[1]:
115 print("IP Address", str(owner_role[0]))
120 if __name__ == "__main__":
121 print("*****Cluster Status******")
122 ipaddress = raw_input("Please enter ipaddress to find Leader Node : ")
123 validate_cluster(ipaddress)
126 print("Cluster checker loaded as Module")