Created
October 27, 2022 02:56
-
-
Save strongant/73c4c9987c596869626ef47115d5aa93 to your computer and use it in GitHub Desktop.
get-consul-service-instance.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import json | |
import urllib | |
from urllib.request import urlopen | |
from urllib import request | |
# from urllib2 import urlopen | |
import sys | |
def main(): | |
baseURL = str(sys.argv[1]) | |
serviceTag = str(sys.argv[2]) | |
authUser = str(sys.argv[3]) | |
url = baseURL + 'v1/catalog/services' | |
hdr = {'Authorization': 'Basic YWRtaW46SjZvME1nSVEyaw=='} | |
req = urllib.request.Request(url, headers=hdr) | |
response = urllib.request.urlopen(req) | |
data = json.loads(response.read()) | |
print("当前consul集群地址:") | |
print(url) | |
# print("总服务数:") | |
# print(len(data)) | |
# print(data) | |
membersURL = baseURL + '/v1/agent/members' | |
hdr = {'Authorization': 'Basic YWRtaW46SjZvME1nSVEyaw=='} | |
req = urllib.request.Request(membersURL, headers=hdr) | |
membersResponse = urlopen(req) | |
memberData = json.loads(membersResponse.read()) | |
consulClients = {''} | |
# print("当前consul nodes:") | |
# for node in memberData: | |
# if(node['Tags']['role']=='node'): | |
# consulClients.add(node['Addr']+':8500') | |
# for n in consulClients: | |
# if(n!=''): | |
# print(n) | |
arr = {""} | |
instanceSet = {""} | |
instanceAddressSet = {""} | |
allInstanceList = [] | |
allInstanceDict = {} | |
allInstances = [] | |
# print the keys and values | |
for key in data: | |
url = baseURL + '/v1/health/service/' + key + '?passing=true' | |
hdr = {'Authorization': 'Basic YWRtaW46SjZvME1nSVEyaw=='} | |
req = urllib.request.Request(url, headers=hdr) | |
response = urlopen(req) | |
data_json = json.loads(response.read()) | |
counts = dict() | |
instanceDict = dict() | |
uqInstance = dict() | |
allInstance = dict() | |
for item in data_json: | |
uqInstance[item['Service']['ID']] = uqInstance.get(item['Service']['ID'], 0) + 1 | |
sid = item['Service']['ID'] | |
if (sid not in allInstance.keys()): | |
allInstance[sid] = [] | |
if(item['Service']["Meta"] is not None and "sourceClusterId" in item['Service']["Meta"]): | |
allInstance.get(sid).append(item['Node']['Address']) | |
l = len(item['Checks']) | |
if (l >= 2): | |
counts[item['Service']['ID']] = counts.get(item['Service']['ID'], 0) + 1 | |
instanceDict[item['Service']['ID']] = item['Node']['Address'] | |
allInstances.append(item['Service']['Address']) | |
if (item['Service']['ID'] not in allInstanceDict.keys()): | |
allInstanceDict[item['Service']['ID']] = 0 | |
allInstanceDict[item['Service']['ID']] = allInstanceDict[item['Service']['ID']] + 1 | |
instanceSet.add(item['Service']['ID']) | |
# print(item['Service']['Tags']) | |
arr.add(key) | |
instanceAddressSet.add(item['Service']['Address']) | |
allInstanceList.append(item['Service']['ID']) | |
last = 3 | |
# for (key, value) in uqInstance.items(): | |
# if (len(uqInstance) > 1 and value > 1 and last != value): | |
# if (value > 3): | |
# tmp = allInstance.get(key) | |
# del tmp[:3] | |
# for t in (tmp): | |
# print("curl -XPUT http://" + t + ":8500/v1/agent/service/deregister/" + key) | |
print("当前服务总数:") | |
print(len(arr)) | |
# print(arr) | |
# for s in arr: | |
# if(s!="consul"): | |
# print(s) | |
print("========================") | |
print("========================") | |
print("========================") | |
print("当前服务实例总数:") | |
print(len(allInstances)) | |
print("当前唯一服务实例总数:") | |
print(len(instanceAddressSet)) | |
# for s in instanceSet: | |
# if(s!="consul"): | |
# print(s) | |
print("========================") | |
print("========================") | |
print("========================") | |
# for s in instanceSet: | |
# if(s!="consul"): | |
# for c in consulClients: | |
# if(c!='' and s!=''): | |
# print('curl -XPUT http://'+c+'/v1/agent/service/deregister/'+s) | |
# print("========================") | |
# print("=========所有服务实例ID===============") | |
# print("=========所有服务实例总数===============") | |
# print(len(allInstanceDict.keys())) | |
# for s in allInstanceDict.keys(): | |
# print(s+':'+str(allInstanceDict[s])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment