Feb-26-2018, 06:45 AM
Dear experts,
need some help
I have written following code for retrieving server node status.. However i am facing issue with looping here...
There are two servcer server1 and server 2 defined and each will have 3 nodes in it that why I
have mentioned for x in range(0,3) in line no 22. and in this loop if all key values are = active then it should return OK if not return "node not healthy" . However this loop is not working properly, though there are no errors in program. I am not getting status for all nodes in each server.. but i am getting only active status..though one of the nodes failed in server 1 i am getting Ok status
Please suggest where its going wrong
Thanks
ASP
need some help
I have written following code for retrieving server node status.. However i am facing issue with looping here...
import requests from requests.auth import HTTPBasicAuth failed_node = " " CB_message = '!!!ALERT!!! cb node: ' message = " " def get_CB_cluster_status(mydic, PWD): current_pwd = '' #print('cb clusters: ') msg_list = [] for key1, value1 in mydic.items(): for cluster, password in PWD.items(): if cluster == key1: current_pwd = password URL = 'http://' + value1 + ':7091/pools/default' r = requests.get(url=URL, auth=HTTPBasicAuth('admin', current_pwd)) data = r.json() # print (data) dic = {} for x in range(0, 3): dic[(data['nodes'][x]['hostname'])] = (data['nodes'][x]['clusterMembership']) # print (dic) count = 0 for key, value in dic.items(): if value == 'active': count = count + 1 if count != 3: for key2, value2 in dic.items(): if value2 != 'active': key2 = (key1 + ': !!!Alert!!! ' + key2 + ' is unhealthy ') failed_node = key2 msg_text = (CB_message + failed_node + 'Failed') else: msg_text = (key1 + ': OK') msg_list.append(msg_text) MESSAGE = {'text': '\n'.join(msg_list)} response = requests.post( HOOK_URL, data=json.dumps(MESSAGE), headers={'Content-Type': 'application/json'}) if response.status_code != 200: raise ValueError( 'Request to slack returned an error %s, the response is:\n%s' % (response.status_code, response.text) ) return SLACK_CHANNEL = "#xxxxxxxxx" HOOK_URL = "https://hooks.slack.com/services/xxxxx/xxxx/xxxxxxx" IP = {'server1': 'xx.xxx.xxx.xx'} PWD = {'server1': 'xxxxx'} get_CB_cluster_status(IP, PWD) IP = {'server2': 'xx.xxx.xxx.xx'} PWD = {'server2': 'xxxxx'} get_CB_cluster_status(IP, PWD)This program is running but its not giving values for all nodes.
There are two servcer server1 and server 2 defined and each will have 3 nodes in it that why I
have mentioned for x in range(0,3) in line no 22. and in this loop if all key values are = active then it should return OK if not return "node not healthy" . However this loop is not working properly, though there are no errors in program. I am not getting status for all nodes in each server.. but i am getting only active status..though one of the nodes failed in server 1 i am getting Ok status
Please suggest where its going wrong
Thanks
ASP