Posts: 33
Threads: 10
Joined: Feb 2018
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
Posts: 4,490
Threads: 69
Joined: Jan 2018
What happens when you print dic ? Are there three 'active' values?
Posts: 33
Threads: 10
Joined: Feb 2018
(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 ?
Posts: 4,490
Threads: 69
Joined: Jan 2018
Feb-26-2018, 09:53 AM
(This post was last modified: Feb-26-2018, 09:53 AM by Gribouillis.)
(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.
Posts: 33
Threads: 10
Joined: Feb 2018
(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
Posts: 33
Threads: 10
Joined: Feb 2018
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
Posts: 4,490
Threads: 69
Joined: Jan 2018
Feb-26-2018, 03:55 PM
(This post was last modified: Feb-26-2018, 03:55 PM by Gribouillis.)
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.
Posts: 33
Threads: 10
Joined: Feb 2018
(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
Posts: 4,490
Threads: 69
Joined: Jan 2018
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)
Posts: 33
Threads: 10
Joined: Feb 2018
(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
|