#! / usr /bin/ env python # # Program: endeca_node_mgr.py # Version: 1.0.0 # Date: 2013-04-14 # By: Richard Pardue ## # Note: Requires requests 1.0.0 => pip install requests # Pyton 2.7.3 # import requests import json import sys # Testing values # mypool = 'MouserCom_Endeca' # Pool Name # mynode = '192.168.2.211:15000' # Is active - Testing # myoption = 'active' # Options <active>.<drain>.<status> ## mypool = sys.argv[ 1 ] mynode = sys.argv[ 2 ] myoption = sys.argv[ 3 ] if myoption == "" : print "Usage: <Pool Name> <Node:Port> <Option: status, drain, active>" exit() myun = ' admin ' mypw = 'admin' url = ' https:// <stingray:9070>/api/tm/1.0/config/active/pools/' + mypool; jsontype = { 'content-type' : 'application/ json ' } client = requests.Session() client.auth = (myun, mypw) client.verify = False try : # Do the HTTP GET to get the lists of pools. We are only putting this client.get within a try # because if there is no error connecting on this one there shouldn't be an error connecting # on later client.get so that would be an unexpected exception. response = client.get(url) except requests.exceptions.ConnectionError: print "Error: Unable to connect to " + url sys.exit( 1 ) data = json.loads(response.content) if response.status_code == 200 : current_active_nodes = data[ 'properties' ][ 'basic' ][ 'nodes' ]; current_draining_nodes = data[ 'properties' ][ 'basic' ][ 'draining' ] current_disabled_nodes = data[ 'properties' ][ 'basic' ][ 'disabled' ] tmp_active_nodes = [] tmp_draining_nodes = [] tmp_disabled_nodes = [] current_node_status = "" for n, node in enumerate(current_active_nodes): tmp_active_nodes.append(node) if node == mynode: current_node_status = "active" for n, node in enumerate(current_draining_nodes): tmp_draining_nodes.append(node) if node == mynode: current_node_status = "draining" for n, node in enumerate(current_disabled_nodes): tmp_disabled_nodes.append(node) if node == mynode: current_node_status = "disabled" # Returns the current status of a node in a pool if myoption == "status" : print current_node_status exit() # Set the node of a pool to draining if not disables if myoption == "drain" : if current_node_status == "draining" : print "Error: Node is already set to drain" exit() if current_node_status == "disabled" : print "Error: can not drain node, it is disabled" else : tmp_draining_nodes.append(mynode) update_pool = { 'properties' : { 'basic' : { 'draining' : tmp_draining_nodes } } } response = client.put(url, data = json.dumps(update_pool), headers = jsontype) if response.status_code == 200 : print "draining" exit() else : print "Error: Return Code = " + response.status_code exit() # Set the node of a pool to active if not disables if myoption == "active" : if current_node_status == "active" : print "Error: Node is already set to active" exit() if current_node_status == "disabled" : print "Error: can not drain node, it is disabled" else : tmp_draining_nodes.remove(mynode) update_pool = { 'properties' : { 'basic' : { 'draining' : tmp_draining_nodes } } } response = client.put(url, data = json.dumps(update_pool), headers = jsontype) if response.status_code == 200 : print "active" exit() else : print "Error: Return Code = " + response.status_code exit() else : print "Error: No option set"
... View more