Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop issue
#1
Dear experts,

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
Reply


Messages In This Thread
Loop issue - by onenessboy - Feb-26-2018, 06:45 AM
RE: Loop issue - by Gribouillis - Feb-26-2018, 07:58 AM
RE: Loop issue - by onenessboy - Feb-26-2018, 08:56 AM
RE: Loop issue - by Gribouillis - Feb-26-2018, 09:53 AM
RE: Loop issue - by onenessboy - Feb-26-2018, 12:13 PM
RE: Loop issue - by onenessboy - Feb-26-2018, 03:22 PM
RE: Loop issue - by Gribouillis - Feb-26-2018, 03:55 PM
RE: Loop issue - by onenessboy - Feb-26-2018, 06:32 PM
RE: Loop issue - by Gribouillis - Feb-26-2018, 07:02 PM
RE: Loop issue - by onenessboy - Feb-27-2018, 05:39 AM
RE: Loop issue - by Gribouillis - Feb-27-2018, 07:16 AM
RE: Loop issue - by onenessboy - Mar-03-2018, 08:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  For List Loop Issue Galdain 2 2,165 Dec-31-2019, 04:53 AM
Last Post: Galdain
  issue with updating list every iteration of a loop ftrillaudp 2 3,196 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  while loop issue, help please? itmustbebunnies 6 3,182 Oct-25-2018, 07:22 AM
Last Post: itmustbebunnies
  for-loop issue digysol 2 2,170 Oct-24-2018, 06:12 PM
Last Post: nilamo
  Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop) Placebo 2 3,624 Sep-30-2018, 01:19 PM
Last Post: Placebo
  while loop issue - stuck in the loop! EricMichel 6 8,808 Aug-06-2018, 03:59 PM
Last Post: EricMichel
  While loop issue CWatters 4 3,930 Sep-25-2017, 08:58 PM
Last Post: nilamo
  Code issue with time remaining loop. Python3 deboerdn2000 11 9,064 May-04-2017, 04:53 PM
Last Post: deboerdn2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020