CL20 remove token auth as api for stats uses basic auth only
This commit is contained in:
parent
c68fbee582
commit
ffc65103c8
101
hx_metrics.py
101
hx_metrics.py
|
@ -1,9 +1,10 @@
|
||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# ver 1.1 CL2019
|
# ver 1.2 CL2020
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask import Response
|
from flask import Response
|
||||||
import requests
|
import requests
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from gevent.wsgi import WSGIServer
|
from gevent.wsgi import WSGIServer
|
||||||
|
@ -21,7 +22,7 @@ hosts=[{'host':'10.1.1.1', 'username':'local/root', 'password':'*******'},{'host
|
||||||
|
|
||||||
'''
|
'''
|
||||||
#
|
#
|
||||||
server_IP ='10.100.253.13'
|
server_IP ='10.100.252.13'
|
||||||
server_port = '8082'
|
server_port = '8082'
|
||||||
|
|
||||||
# Logging config
|
# Logging config
|
||||||
|
@ -43,17 +44,11 @@ def get_stats():
|
||||||
results =''
|
results =''
|
||||||
for host in hx_creds.hosts:
|
for host in hx_creds.hosts:
|
||||||
logger.info("----------- Processing Host: %s -----------"%host['host'])
|
logger.info("----------- Processing Host: %s -----------"%host['host'])
|
||||||
# get auth token (either cached or new one)
|
|
||||||
authdata = get_auth(host['host'], host['username'], host['password'])
|
|
||||||
if not authdata:
|
|
||||||
logger.info("Missing token, skipping host: "+host['host'])
|
|
||||||
continue
|
|
||||||
url = "https://"+host['host']
|
url = "https://"+host['host']
|
||||||
|
|
||||||
# uri for throughput data with "last 5 min" filter
|
# uri for throughput data with "last 5 min" filter
|
||||||
uri_MBps = '/render?target=stats.counters.scvmclient.allhosts.nfsBytesRead.cluster.rate&target=stats.counters.scvmclient.allhosts.nfsBytesWritten.cluster.rate&format=json&from=-5min'
|
uri_MBps = '/stats?target=stats.counters.scvmclient.allhosts.nfsBytesRead.cluster.rate&target=stats.counters.scvmclient.allhosts.nfsBytesWritten.cluster.rate&format=json&from=-5min'
|
||||||
# Get throughput data
|
# Get throughput data
|
||||||
MBps_data = get_stats(authdata,url+uri_MBps)
|
MBps_data = get_stats(host['username'],host['password'],url+uri_MBps)
|
||||||
if MBps_data:
|
if MBps_data:
|
||||||
try:
|
try:
|
||||||
MBps_Read=round(MBps_data[0]['datapoints'][-2][0],3)
|
MBps_Read=round(MBps_data[0]['datapoints'][-2][0],3)
|
||||||
|
@ -70,9 +65,9 @@ def get_stats():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# url to get the IOPS data
|
# url to get the IOPS data
|
||||||
uri_IOPS = '/render?target=stats.counters.scvmclient.allhosts.nfsReads.cluster.rate&target=stats.counters.scvmclient.allhosts.nfsWrites.cluster.rate&format=json&from=-5min'
|
uri_IOPS = '/stats?target=stats.counters.scvmclient.allhosts.nfsReads.cluster.rate&target=stats.counters.scvmclient.allhosts.nfsWrites.cluster.rate&format=json&from=-5min'
|
||||||
# get IOPS data
|
# get IOPS data
|
||||||
IOPS_data = get_stats(authdata,url+uri_IOPS)
|
IOPS_data = get_stats(host['username'],host['password'],url+uri_IOPS)
|
||||||
if IOPS_data:
|
if IOPS_data:
|
||||||
try:
|
try:
|
||||||
IOPS_Read=round(IOPS_data[0]['datapoints'][-2][0],3)
|
IOPS_Read=round(IOPS_data[0]['datapoints'][-2][0],3)
|
||||||
|
@ -89,10 +84,10 @@ def get_stats():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# url to get Latency data
|
# url to get Latency data
|
||||||
uri_Lat ='/render?target=divideSeries(stats.timers.scvmclient.allhosts.nfsReadLatency.cluster.total%2Cstats.counters.scvmclient.allhosts.nfsReads.cluster.count)&target=divideSeries(stats.timers.scvmclient.allhosts.nfsWriteLatency.cluster.total%2Cstats.counters.scvmclient.allhosts.nfsWrites.cluster.count)&format=json&from=-5min'
|
uri_Lat ='/stats?target=divideSeries(stats.timers.scvmclient.allhosts.nfsReadLatency.cluster.total%2Cstats.counters.scvmclient.allhosts.nfsReads.cluster.count)&target=divideSeries(stats.timers.scvmclient.allhosts.nfsWriteLatency.cluster.total%2Cstats.counters.scvmclient.allhosts.nfsWrites.cluster.count)&format=json&from=-5min'
|
||||||
|
|
||||||
# get latency data
|
# get latency data
|
||||||
Lat_data = get_stats(authdata,url+uri_Lat)
|
Lat_data = get_stats(host['username'],host['password'],url+uri_Lat)
|
||||||
if Lat_data:
|
if Lat_data:
|
||||||
try:
|
try:
|
||||||
Lat_Read=round(Lat_data[0]['datapoints'][-2][0],3)
|
Lat_Read=round(Lat_data[0]['datapoints'][-2][0],3)
|
||||||
|
@ -114,81 +109,29 @@ def get_stats():
|
||||||
logger.info("----------- Finished -----------")
|
logger.info("----------- Finished -----------")
|
||||||
return Response(results, mimetype='text/plain')
|
return Response(results, mimetype='text/plain')
|
||||||
|
|
||||||
#
|
|
||||||
# Returns Authentication token
|
|
||||||
#
|
|
||||||
def get_auth(host, username, password):
|
|
||||||
|
|
||||||
logger.info("Trying to auth "+host)
|
|
||||||
|
|
||||||
global tokens
|
|
||||||
|
|
||||||
headers={'content-type':'application/json'}
|
|
||||||
payload = {
|
|
||||||
"username": username,
|
|
||||||
"password": password,
|
|
||||||
"client_id": "HxGuiClient",
|
|
||||||
"client_secret": "Sunnyvale",
|
|
||||||
"redirect_uri": "http://"+host
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if tokens.get(host):
|
|
||||||
# looks like we have token cached already
|
|
||||||
# let's check if it's valid
|
|
||||||
|
|
||||||
payload = {
|
|
||||||
"access_token": tokens.get(host)['access_token'],
|
|
||||||
"scope": "READ",
|
|
||||||
"token_type": tokens.get(host)['token_type']
|
|
||||||
}
|
|
||||||
try:
|
|
||||||
#validating token
|
|
||||||
url = 'https://%s/aaa/v1/validate'%host
|
|
||||||
response = requests.post(url,headers=headers,data=json.dumps(payload),verify=False,timeout=4)
|
|
||||||
if response.status_code == 200:
|
|
||||||
logger.info("Re-using existing auth token")
|
|
||||||
return tokens.get(host)
|
|
||||||
logger.error("Failed to validate existing token "+response.content)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("Post for token validate failed \n"+str(e))
|
|
||||||
|
|
||||||
# this happens if no cached token found, or existing token was invalid
|
|
||||||
url = 'https://%s/aaa/v1/auth?grant_type=password'%host
|
|
||||||
payload = {
|
|
||||||
"username": username,
|
|
||||||
"password": password,
|
|
||||||
"client_id": "HxGuiClient",
|
|
||||||
"client_secret": "Sunnyvale",
|
|
||||||
"redirect_uri": "http://"+host
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = requests.post(url,headers=headers,data=json.dumps(payload),verify=False,timeout=4)
|
|
||||||
if response.status_code == 201:
|
|
||||||
if response.json().get('access_token'):
|
|
||||||
tokens[host]=response.json()
|
|
||||||
logger.info("Got token ok")
|
|
||||||
return response.json()
|
|
||||||
logger.error("Failed get a token "+response.content)
|
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("Post for token get failed \n"+str(e))
|
|
||||||
return None
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# calls HX API
|
# calls HX API
|
||||||
#
|
#
|
||||||
def get_stats(authdata, url):
|
def get_stats(username, password, url):
|
||||||
logger.info("call for get_stats")
|
logger.info("call for get_stats")
|
||||||
try:
|
try:
|
||||||
headers = {'Authorization': authdata['token_type'] + ' ' + authdata['access_token'],'Connection':'close'}
|
headers = {'Connection':'close'}
|
||||||
response = requests.get(url,headers=headers,verify=False,timeout=4)
|
|
||||||
|
#logger.info("username: %s"%username)
|
||||||
|
#logger.info("pass: %s"%password)
|
||||||
|
#logger.info("url: %s"%url)
|
||||||
|
#logger.info("headers: %s"%headers)
|
||||||
|
|
||||||
|
response = requests.get(url, auth=HTTPBasicAuth('admin',password), headers=headers, verify=False,timeout=4)
|
||||||
|
|
||||||
|
#logger.info("status code:%s"%str(response.status_code))
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
logger.info("Got data ok")
|
logger.info("Got data ok")
|
||||||
return response.json()
|
return response.json()
|
||||||
logger.error("Failed to get data "+response.content)
|
logger.error("Failed to get data "+response.content)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Post for data failed \n"+str(e))
|
logger.error("Post for data failed \n"+str(e))
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue