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

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

View File

@ -39,12 +39,16 @@ class Jms(Resource):
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',[])
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:
if i['os'] == linux:
count_linux = count_linux + 1
elif i['os'] == windows:
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('',''))
count_cpu = count_cpu + cpu
mem = int(i['cpu'].replace('GB',''))