From 7aa10e223d086d1c8f1ed725efdf5c553beea437 Mon Sep 17 00:00:00 2001 From: "StarsL.cn" Date: Mon, 18 Jul 2022 00:00:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E6=9C=BA=E7=9A=84=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E4=B9=9F=E4=BC=9A=E5=90=8C=E6=AD=A5=E5=88=B0consul=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E5=8A=A0=E4=B8=8Atag=E6=A0=87=E8=AF=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flask-consul/units/cloud/sync_ecs.py | 101 +++++++++++++-------------- flask-consul/views/jms.py | 6 +- 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/flask-consul/units/cloud/sync_ecs.py b/flask-consul/units/cloud/sync_ecs.py index e891ae0..fecc1fc 100644 --- a/flask-consul/units/cloud/sync_ecs.py +++ b/flask-consul/units/cloud/sync_ecs.py @@ -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 diff --git a/flask-consul/views/jms.py b/flask-consul/views/jms.py index 5fdf323..8841760 100644 --- a/flask-consul/views/jms.py +++ b/flask-consul/views/jms.py @@ -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',''))