Company•September 9, 2014
Using the OpsCenter API with Python

# These statements apply to all following snippets as well.
import requests
import time
opsc_url = '10.0.0.1:8888'
meta = requests.get("http://{url}/meta".format(url=opsc_url)).json()
print("OpsCenter Version: {version}".format(version=meta['version']))
clusterconf = requests.get("http://{url}/cluster-configs".format(url=opsc_url)).json()
for cluster in clusterconf.keys():
print("\nJMX Port for cluster '{cluster_id}': {jmx_port}".format(cluster_id=cluster,
jmx_port=clusterconf[cluster]['jmx']['port']))
nodes = requests.get("http://{url}/{cluster_id}/nodes".format(url=opsc_url, cluster_id=cluster)).json()
for idx, node in enumerate(nodes):
print("\tIP for node {index}: {node_ip}".format(index=idx, node_ip=node['node_ip']))
print("\nRestarting cluster {cluster_id}".format(cluster_id=cluster))
restart_reqid = requests.post("http://{url}/{cluster_id}/ops/restart".format(url=opsc_url, cluster_id=cluster),
data='{"sleep": 10, "drain_first": false}').json()
print("Restart Request Id: {req_id}".format(req_id=restart_reqid))
while True:
restart_status = requests.get(
"http://{url}/request/{req_id}/status".format(url=opsc_url, req_id=restart_reqid)).json()
if restart_status['state'] != 'running':
break
time.sleep(2)
if restart_status['state'] == 'success':
print("Cluster restarted.")
else:
print("Cluster restart error?")