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
#2
What happens when you print dic? Are there three 'active' values?
Reply
#3
(Feb-26-2018, 07:58 AM)Gribouillis Wrote: What happens when you print dic? Are there three 'active' values?

Hi Gribouillis

Thanks for reply

Yes, there are 2 active and one inactive node in that json data...

What I feel that program is not going into below loop
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')

and I did this program using pycharm gui and how the below it says
local variable msg_text value is not used something like warning...
for below line of code
msg_text = (CB_message + failed_node + 'Failed')

am i doing something wrong in looping ?
Reply
#4
(Feb-26-2018, 08:56 AM)onenessboy Wrote: Yes, there are 2 active and one inactive node in that json data...
The question is not in the json data, the question is what is exactly the value of the python variable dic at line 25 in the above code ?

(Feb-26-2018, 08:56 AM)onenessboy Wrote: it says
local variable msg_text value is not used
This is true, in function get_CB_cluster_status(), you give a value to msg_text but then you do nothing with it.
Reply
#5
(Feb-26-2018, 09:53 AM)Gribouillis Wrote:
(Feb-26-2018, 08:56 AM)onenessboy Wrote: Yes, there are 2 active and one inactive node in that json data...
The question is not in the json data, the question is what is exactly the value of the python variable dic at line 25 in the above code ?

(Feb-26-2018, 08:56 AM)onenessboy Wrote: it says
local variable msg_text value is not used
This is true, in function get_CB_cluster_status(), you give a value to msg_text but then you do nothing with it.

Hi

When i enabled print dic this is what I am getting


So up to below loop is working fine...

for x in range(0, 3): -- range 0,3 because there will be three nodes for each server which I pass to this program
dic[(data['nodes'][x]['hostname'])] = (data['nodes'][x]['clusterMembership'])
print (dic) -- the output is below
{} -- intial loop
{u'xxx.xx.x.2': u'active'} -- first loop
{u'xxx.xx.x.2': u'active', u'xxx.xx.x.3': u'inactiveFailed'} -- second loop
{u'xxx.xx.x.2': u'active', u'xxx.xx.x.3': u'inactiveFailed', u'xxx.xx.x.4': u'active'} -- third and final loop

so in this cluster(cluster means server which i am inputting for this get get_CB_cluster_status) one node with ip xxx.xx.x.3 is with inactiveFailed means unhealthy and other two nodes with xxx.xx.x.2 and xxx.xx.x.4 are healthy

Now i want program should output:

If even 1 node is down, its unhealthy
Server1 is unhealthy
Node xxx.xx.xx.2 is Up
Node xxx.xx.xx.3 is Down
Node xxx.xx.xx.4 is Up

If all nodes are active

Server1 is Healthy
Node xxx.xx.xx.2 is up
Node xxx.xx.xx.3 is up
Node xxx.xx.xx.4 is up
Reply
#6
When i tried to print values to test it...

even the below part also looks to work fine.. but I am not sure where i am missing
            for key2, value2 in dic.items():
                print key2
                print value2
                if value2 != 'active':
                    key2 = ('Node' + ': !!!Alert!!! ' + key2 + ' is unhealthy ')
                    print key2
xxx.xx.x.2
active
xxx.xx.x.3
inactiveFailed
Node: !!!Alert!!! xxx.xx.x.3 is unhealthy
xxx.xx.x.4
active
Reply
#7
You only need to write this at line 25 and remove the rest of the function (this code needs to be indented at the same level as count = 0)
healthy = all(v == 'active' for v in dic.values())
print('{} is {}healthy'.format(key1, '' if healthy else 'un'))
for node, status in dic.items():
    print('Node {} is {}'.format(node, 'Up' if status == 'active' else 'Down'))
Also as the function does not return anything, it shouldn't be named get_CB_cluster_status() but rather display_CB_cluster_status() or something like that.
Reply
#8
(Feb-26-2018, 03:55 PM)Gribouillis Wrote: You only need to write this at line 25 and remove the rest of the function (this code needs to be indented at the same level as count = 0)
healthy = all(v == 'active' for v in dic.values())
print('{} is {}healthy'.format(key1, '' if healthy else 'un'))
for node, status in dic.items():
    print('Node {} is {}'.format(node, 'Up' if status == 'active' else 'Down'))
Also as the function does not return anything, it shouldn't be named get_CB_cluster_status() but rather display_CB_cluster_status() or something like that.

thanks for suggestion, i have changed the code accordingly,
its printing correctly on screen.

but my goal is to send these as list to slack channel and now i ran into another issue, instead of printing, i wanted to send it to slack channel via msg_lst[]...but its saying msg_list name not defined

import requests
from requests.auth import HTTPBasicAuth
 

CB_message = '!!!ALERT!!! cb node: '

 
 
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
        healthy = all(v == 'active' for v in dic.values())
        msg1=('{} is {}healthy'.format(key1, '' if healthy else 'un'))
        for node, status in dic.items():
            msg2 = ('Node {} is {}'.format(node, 'Up' if status == 'active' else 'Down'))
            msg_list.append(msg1+msg2)
    return
 
MESSAGE = {'text': '\n'.join(msg_list)}
SLACK_CHANNEL = "#xxxxxxxxx"
HOOK_URL = "https://hooks.slack.com/services/xxxx/xxxxx/xxxxxxxx"


response = requests.post(
    HOOK_URL, data=json.dumps(MESSAGE),
    headers={'Content-Type': 'application/json'})
# Error handling section
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )
 
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)
That means the below should be as it is should go to slack channel for all servers

Server1 is unhealthy
Node xxx.xx.x.2 is Up
Node xxx.xx.x.3 is Down
Node xxx.xx.x.4 is Up

Server2 is healthy
Node xxx.xx.x.2 is Up
Node xxx.xx.x.3 is Up
Node xxx.xx.x.4 is Up
Reply
#9
Do you understand what the code does? msg_list is a local variable in the function. The code that follows needs to be indented to become part of the function. Try this:
import requests
from requests.auth import HTTPBasicAuth
  
 
CB_message = '!!!ALERT!!! cb node: '
 
  
  
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
        healthy = all(v == 'active' for v in dic.values())
        msg1=('{} is {}healthy'.format(key1, '' if healthy else 'un'))
        msg_list.extend('', msg1)
        for node, status in dic.items():
            msg2 = ('Node {} is {}'.format(node, 'Up' if status == 'active' else 'Down'))
            msg_list.append(msg2)
  
    MESSAGE = {'text': '\n'.join(msg_list).lstrip('\n')}
    SLACK_CHANNEL = "#xxxxxxxxx"
    HOOK_URL = "https://hooks.slack.com/services/xxxx/xxxxx/xxxxxxxx"
    
    
    response = requests.post(
        HOOK_URL, data=json.dumps(MESSAGE),
        headers={'Content-Type': 'application/json'})
    # Error handling section
    if response.status_code != 200:
        raise ValueError(
            'Request to slack returned an error %s, the response is:\n%s'
            % (response.status_code, response.text)
        )
  
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)
Reply
#10
(Feb-26-2018, 07:02 PM)Gribouillis Wrote: Do you understand what the code does? msg_list is a local variable in the function. The code that follows needs to be indented to become part of the function. Try this:
import requests
from requests.auth import HTTPBasicAuth
  
 
CB_message = '!!!ALERT!!! cb node: '
 
  
  
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
        healthy = all(v == 'active' for v in dic.values())
        msg1=('{} is {}healthy'.format(key1, '' if healthy else 'un'))
        msg_list.extend('', msg1)
        for node, status in dic.items():
            msg2 = ('Node {} is {}'.format(node, 'Up' if status == 'active' else 'Down'))
            msg_list.append(msg2)
  
    MESSAGE = {'text': '\n'.join(msg_list).lstrip('\n')}
    SLACK_CHANNEL = "#xxxxxxxxx"
    HOOK_URL = "https://hooks.slack.com/services/xxxx/xxxxx/xxxxxxxx"
    
    
    response = requests.post(
        HOOK_URL, data=json.dumps(MESSAGE),
        headers={'Content-Type': 'application/json'})
    # Error handling section
    if response.status_code != 200:
        raise ValueError(
            'Request to slack returned an error %s, the response is:\n%s'
            % (response.status_code, response.text)
        )
  
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)

Hi Gribouillis,

Thanks for your patience with me. Sorry I was not good in python, seems I need to dig deep drive learning...

and I tried above suggestion, i am getting error at line 26 its saying

Traceback (most recent call last):
File "/home/user/abcbslack.py", line 48, in <module>
get_CB_cluster_status(IP, PWD)
File "/home/user/abcbslack.py", line 26, in get_CB_cluster_status
msg_list.extend('', msg1)
TypeError: extend() takes exactly one argument (2 given)

Regards
ASP
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For List Loop Issue Galdain 2 2,046 Dec-31-2019, 04:53 AM
Last Post: Galdain
  issue with updating list every iteration of a loop ftrillaudp 2 3,057 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  while loop issue, help please? itmustbebunnies 6 3,015 Oct-25-2018, 07:22 AM
Last Post: itmustbebunnies
  for-loop issue digysol 2 2,088 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,506 Sep-30-2018, 01:19 PM
Last Post: Placebo
  while loop issue - stuck in the loop! EricMichel 6 8,598 Aug-06-2018, 03:59 PM
Last Post: EricMichel
  While loop issue CWatters 4 3,785 Sep-25-2017, 08:58 PM
Last Post: nilamo
  Code issue with time remaining loop. Python3 deboerdn2000 11 8,809 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