关机的主机也会同步到consul,并加上tag标识。

pull/27/head
StarsL.cn 2022-07-18 00:00:44 +08:00
parent 70b8e62bf9
commit 7aa10e223d
2 changed files with 55 additions and 52 deletions

View File

@ -24,60 +24,59 @@ def w2consul(vendor,account,region,ecs_dict):
off,on = 0,0 off,on = 0,0
for k,v in ecs_dict.items(): for k,v in ecs_dict.items():
iid = k iid = k
#去除consul中关机的ecs #对consul中关机的ecs做标记。
if v['status'] in ['SHUTOFF','Stopped','STOPPED']: if v['status'] in ['SHUTOFF','Stopped','STOPPED']:
off = off + 1 off = off + 1
if k in consul_ecs_iid_list: tags = ['shutoff', region]
dereg = requests.put(f'{delurl}/{iid}', headers=headers) stat = 'off'
if dereg.status_code == 200:
print({"code": 20000,"data": f"{account}-删除成功!"}, flush=True)
else:
print({"code": 50000,"data": f'{dereg.status_code}:{dereg.text}'}, flush=True)
else: else:
on = on + 1 on = on + 1
custom_ecs = consul_kv.get_value(f'ConsulManager/assets/sync_ecs_custom/{iid}') tags = [v['ostype'],region]
port = custom_ecs.get('port') stat = 'on'
ip = custom_ecs.get('ip') custom_ecs = consul_kv.get_value(f'ConsulManager/assets/sync_ecs_custom/{iid}')
if port == None: port = custom_ecs.get('port')
port = 9100 if v['ostype'] == 'linux' else 9182 ip = custom_ecs.get('ip')
if ip == None: if port == None:
ip = v['ip'] if isinstance(v['ip'],list) is False else v['ip'][0] port = 9100 if v['ostype'] == 'linux' else 9182
instance = f'{ip}:{port}' if ip == None:
data = { ip = v['ip'] if isinstance(v['ip'],list) is False else v['ip'][0]
'id': iid, instance = f'{ip}:{port}'
'name': service_name, data = {
'Address': ip, 'id': iid,
'port': port, 'name': service_name,
'tags': [v['ostype'],region], 'Address': ip,
'Meta': { 'port': port,
'iid': iid, 'tags': tags,
'name': v['name'], 'Meta': {
'region': regions[vendor].get(region,'未找到'), 'iid': iid,
'group': v['group'], 'name': v['name'],
'instance': instance, 'region': regions[vendor].get(region,'未找到'),
'account': account, 'group': v['group'],
'vendor': vendors.get(vendor,'未找到'), 'instance': instance,
'os': v['ostype'], 'account': account,
'cpu': v['cpu'], 'vendor': vendors.get(vendor,'未找到'),
'mem': v['mem'], 'os': v['ostype'],
'exp': v['exp'] 'cpu': v['cpu'],
}, 'mem': v['mem'],
"check": { 'exp': v['exp'],
"tcp": f"{ip}:{port}", 'stat': stat
"interval": "60s" },
} "check": {
"tcp": f"{ip}:{port}",
"interval": "60s"
} }
if vendor == 'alicloud' and v['ecstag'] != []: }
ecstag_dict = {} if vendor == 'alicloud' and v['ecstag'] != []:
for ecstag in v['ecstag']: ecstag_dict = {}
if ecstag['TagKey'].encode().isalnum(): for ecstag in v['ecstag']:
ecstag_dict[ecstag['TagKey']] = ecstag['TagValue'] if ecstag['TagKey'].encode().isalnum():
data['Meta'].update(ecstag_dict) ecstag_dict[ecstag['TagKey']] = ecstag['TagValue']
reg = requests.put(puturl, headers=headers, data=json.dumps(data)) data['Meta'].update(ecstag_dict)
if reg.status_code == 200: reg = requests.put(puturl, headers=headers, data=json.dumps(data))
pass if reg.status_code == 200:
#print({f"{account}:code": 20000,"data": "增加成功!"}, flush=True) pass
else: #print({f"{account}:code": 20000,"data": "增加成功!"}, flush=True)
print({f"{account}:code": 50000,"data": f'{reg.status_code}:{reg.text}'}, flush=True) else:
#return {"code": 50000,"data": f'{reg.status_code}:{reg.text}'} print({f"{account}:code": 50000,"data": f'{reg.status_code}:{reg.text}'}, flush=True)
#return {"code": 50000,"data": f'{reg.status_code}:{reg.text}'}
return off,on return off,on

View File

@ -39,12 +39,16 @@ class Jms(Resource):
count_group = consul_kv.get_value(f'ConsulManager/record/jobs/{vendor}/{account}/group')['count'] count_group = consul_kv.get_value(f'ConsulManager/record/jobs/{vendor}/{account}/group')['count']
services_meta = consul_kv.get_services_meta(f'{vendor}_{account}_ecs').get('ecs_list',[]) services_meta = consul_kv.get_services_meta(f'{vendor}_{account}_ecs').get('ecs_list',[])
count_ecs = len(services_meta) count_ecs = len(services_meta)
count_cpu,count_mem,count_win,count_linux = 0,0,0,0 count_off,count_on,count_cpu,count_mem,count_win,count_linux = 0,0,0,0,0,0
for i in services_meta: for i in services_meta:
if i['os'] == linux: if i['os'] == linux:
count_linux = count_linux + 1 count_linux = count_linux + 1
elif i['os'] == windows: elif i['os'] == windows:
count_win = count_win + 1 count_win = count_win + 1
if i.get('stat') == off:
count_off = count_off + 1
else:
count_on = count_on + 1
cpu = int(i['cpu'].replace('','')) cpu = int(i['cpu'].replace('',''))
count_cpu = count_cpu + cpu count_cpu = count_cpu + cpu
mem = int(i['cpu'].replace('GB','')) mem = int(i['cpu'].replace('GB',''))