AWS api数据信息获取(boto3)

GitHub - starsliao/TenSunS: 🦄后羿 - TenSunS(原ConsulManager):基于Consul的运维平台:更优雅的Consul管理UI&多云与自建ECS/MySQL/Redis同步Prometheus/JumpServer&ECS/MySQL/Redis云监控指标采集&Blackbox站点监控维护&漏洞通知/资源到期余额告警&各类资源Grafana看板展示🦄后羿 - TenSunS(原ConsulManager):基于Consul的运维平台:更优雅的Consul管理UI&多云与自建ECS/MySQL/Redis同步Prometheus/JumpServer&ECS/MySQL/Redis云监控指标采集&Blackbox站点监控维护&漏洞通知/资源到期余额告警&各类资源Grafana看板展示 - starsliao/TenSunShttps://github.com/starsliao/TenSunS

所有依赖(包含无用的,懒得不分了)

bash 复制代码
Werkzeug==2.0.3
itsdangerous==2.0.1
flask==2.0.2
flask-restful==0.3.9
flask-cors==3.0.10
Flask-HTTPAuth==4.5.0
requests==2.27.1
Flask-APScheduler==1.12.3
xlrd==1.2.0
#pyDes==2.0.1
pycryptodome==3.14.1
beautifulsoup4==4.11.1
ldap3==2.9.1
loguru==0.6.0
huaweicloudsdkcore==3.1.11
huaweicloudsdkecs==3.1.11
huaweicloudsdkeps==3.1.11
huaweicloudsdkbss==3.1.11
huaweicloudsdkrds==3.1.11
huaweicloudsdkces==3.1.11
huaweicloudsdkdcs==3.1.11
alibabacloud_resourcemanager20200331==2.1.1
alibabacloud_ecs20140526==2.1.3
alibabacloud_rds20140815==2.1.2
alibabacloud_r_kvstore20150101==2.20.7
alibabacloud_bssopenapi20171214==2.0.6
aliyun-python-sdk-cms==7.0.32
tencentcloud-sdk-python-common==3.0.779
tencentcloud-sdk-python-cvm==3.0.779
tencentcloud-sdk-python-cdb==3.0.779
tencentcloud-sdk-python-dcdb==3.0.779
tencentcloud-sdk-python-billing==3.0.779
tencentcloud-sdk-python-monitor==3.0.779
tencentcloud-sdk-python-redis==3.0.779
boto3==1.23.10

源码

python 复制代码
from Tea.exceptions import TeaException
import boto3

import sys,datetime,hashlib,math,traceback
from units import consul_kv,consul_svc
from units.cloud import sync_ecs,sync_rds,sync_redis,notify
from units.config_log import *

def exp(account,collect_days,notify_days,notify_amount):
    pass

def group(account):
    try:
        now = datetime.datetime.now().strftime('%m.%d/%H:%M')
        group_dict = {"1": "1"} # AWS没有资产组概念
        consul_kv.put_kv(f'ConsulManager/assets/awscloud/group/{account}',group_dict)
        count = len(group_dict)
        data = {'count':count,'update':now,'status':20000,'msg':f'同步资源组成功!总数:{count}'}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/group', data)
        logger.info(f'【JOB】===>awscloud_group {account} {data}')
    except TeaException as e:
        emsg = e.message.split('. ',1)[0]
        logger.error(f"【code:】{e.code}\n【message:】{emsg}\n{traceback.format_exc()}")
        data = consul_kv.get_value(f'ConsulManager/record/jobs/awscloud/{account}/group')
        if data == {}:
            data = {'count':'无','update':f'失败{e.code}','status':50000,'msg':emsg}
        else:
            data['update'] = f'失败{e.code}'
            data['msg'] = emsg
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/group', data)
    except Exception as e:
        logger.error(f'{e}\n{traceback.format_exc()}')
        data = {'count':'无','update':f'失败','status':50000,'msg':str(e)}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/group', data)

def ecs(account,region,isextip=False):
    ak,sk = consul_kv.get_aksk('awscloud',account)
    now = datetime.datetime.now().strftime('%m.%d/%H:%M')
    group_dict = consul_kv.get_value(f'ConsulManager/assets/awscloud/group/{account}')  # {"1":"1"}

    ecs_dict = {}
    try:
        ec2 = boto3.client(
            "ec2",
            aws_access_key_id=ak,
            aws_secret_access_key=sk,
            region_name=region,
        )
        response = ec2.describe_instances()

        for reservations in response['Reservations']:
            for instances in reservations['Instances']:
                InstanceId = instances["InstanceId"]
                ecs_dict_temp = {InstanceId: {}}
                for tag in instances["Tags"]:
                    if tag["Key"] == "Name":
                        ecs_dict_temp[InstanceId]["name"] = tag["Value"]
                ecs_dict_temp[InstanceId]["group"] = "无"
                ecs_dict_temp[InstanceId]["ostype"] = "windows" if "win" in instances["PlatformDetails"].lower() else "linux"
                ecs_dict_temp[InstanceId]["status"] = instances["State"]["Name"]
                ecs_dict_temp[InstanceId]["region"] = region
                ecs_dict_temp[InstanceId]["ip"] = instances["PrivateIpAddress"]
                ecs_dict_temp[InstanceId]["cpu"] = f'{instances["CpuOptions"]["CoreCount"]*instances["CpuOptions"]["ThreadsPerCore"]}核'
                ecs_dict_temp[InstanceId]["mem"] = "0"
                ecs_dict_temp[InstanceId]["exp"] = "按量" # AWS仅按量 
                if isextip:
                    ecs_dict_temp[InstanceId]["ip"] = instances["PublicIpAddress"]
                ecs_dict.update(ecs_dict_temp)

        count = len(ecs_dict)
        off,on = sync_ecs.w2consul('awscloud',account,region,ecs_dict)
        data = {'count':count,'update':now,'status':20000,'on':on,'off':off,'msg':f'ECS同步成功!总数:{count},开机:{on},关机:{off}'}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/ecs/{region}', data)
        logger.info(f'【JOB】===>awscloud_ecs {account} {region} {data}')
    except TeaException as e:
        emsg = e.message.split('. ',1)[0]
        logger.error(f"【code:】{e.code}\n【message:】{emsg}\n{traceback.format_exc()}")
        data = consul_kv.get_value(f'ConsulManager/record/jobs/awscloud/{account}/ecs/{region}')
        if data == {}:
            data = {'count':'无','update':f'失败{e.code}','status':50000,'msg':emsg}
        else:
            data['update'] = f'失败{e.code}'
            data['msg'] = emsg
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/ecs/{region}', data)
    except Exception as e:
        logger.error(f'{e}\n{traceback.format_exc()}')
        data = {'count':'无','update':f'失败','status':50000,'msg':str(e)}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/ecs/{region}', data)

def redis(account,region):
    ak,sk = consul_kv.get_aksk('awscloud',account)
    now = datetime.datetime.now().strftime('%m.%d/%H:%M')
    group_dict = consul_kv.get_value(f'ConsulManager/assets/awscloud/group/{account}')

    redis_dict = {}
    try:
        elasticache = boto3.client(
            'elasticache',
            aws_access_key_id=ak,
            aws_secret_access_key=sk,
            region_name=region,
        )
        response = elasticache.describe_cache_clusters()

        for rdb in response["CacheClusters"]:
            CacheClusterId = rdb["CacheClusterId"]
            redis_dict_temp = {CacheClusterId: {}}
            redis_dict_temp[CacheClusterId]["name"] = rdb["CacheClusterId"]
            redis_dict_temp[CacheClusterId]["domain"] = rdb.get("PrivateIp","null")
            redis_dict_temp[CacheClusterId]["ip"] = rdb.get("PrivateIp","null")
            redis_dict_temp[CacheClusterId]["port"] = 6379
            redis_dict_temp[CacheClusterId]["region"] = region
            redis_dict_temp[CacheClusterId]["group"] = rdb["ReplicationGroupId"]
            redis_dict_temp[CacheClusterId]["status"] = rdb["CacheClusterStatus"]
            redis_dict_temp[CacheClusterId]["itype"] = rdb["Engine"]
            redis_dict_temp[CacheClusterId]["ver"] = rdb["EngineVersion"]
            redis_dict_temp[CacheClusterId]["mem"] = "0"
            redis_dict_temp[CacheClusterId]["exp"] = "按量"
            redis_dict.update(redis_dict_temp)

        count = len(redis_dict)
        off,on = sync_redis.w2consul('awscloud',account,region,redis_dict)
        data = {'count':count,'update':now,'status':20000,'on':on,'off':off,'msg':f'redis同步成功!总数:{count},开机:{on},关机:{off}'}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/redis/{region}', data)
        logger.info(f'【JOB】===>awscloud_redis {account} {region} {data}')
    except TeaException as e:
        emsg = e.message.split('. ',1)[0]
        logger.error(f"【code:】{e.code}\n【message:】{e.message}\n{traceback.format_exc()}")
        data = consul_kv.get_value(f'ConsulManager/record/jobs/awscloud/{account}/redis/{region}')
        if data == {}:
            data = {'count':'无','update':f'失败{e.code}','status':50000,'msg':emsg}
        else:
            data['update'] = f'失败{e.code}'
            data['msg'] = emsg
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/redis/{region}', data)
    except Exception as e:
        logger.error(f'{e}\n{traceback.format_exc()}')
        data = {'count':'无','update':f'失败','status':50000,'msg':str(e)}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/redis/{region}', data)

def rds(account,region):
    ak,sk = consul_kv.get_aksk('awscloud',account)
    now = datetime.datetime.now().strftime('%m.%d/%H:%M')
    group_dict = consul_kv.get_value(f'ConsulManager/assets/awscloud/group/{account}')

    rds_dict = {}
    try:
        rds = boto3.client(
            "rds",
            aws_access_key_id=ak,
            aws_secret_access_key=sk,
            region_name=region,
        )
        response = rds.describe_db_instances()

        for db in response["DBInstances"]:
            DBInstanceId = db["DBInstanceIdentifier"]
            rds_dict_temp = {DBInstanceId: {}}
            rds_dict_temp[DBInstanceId]["name"] = db["DBInstanceIdentifier"]
            rds_dict_temp[DBInstanceId]["domain"] = db["Endpoint"]["Address"]
            rds_dict_temp[DBInstanceId]["ip"] = db["Endpoint"]["Address"]
            rds_dict_temp[DBInstanceId]["port"] = db["Endpoint"]["Port"]
            rds_dict_temp[DBInstanceId]["region"] = region
            rds_dict_temp[DBInstanceId]["group"] = db["DBClusterIdentifier"]
            rds_dict_temp[DBInstanceId]["status"] = db["DBInstanceStatus"]
            rds_dict_temp[DBInstanceId]["itype"] = db["Engine"]
            rds_dict_temp[DBInstanceId]["ver"] = db["EngineVersion"]
            rds_dict_temp[DBInstanceId]["exp"] = "按量"
            rds_dict_temp[DBInstanceId]["cpu"] = "0"
            rds_dict_temp[DBInstanceId]["mem"] = "0"
            rds_dict_temp[DBInstanceId]["disk"] = "0"
            rds_dict.update(rds_dict_temp)
 
        count = len(rds_dict)
        off,on = sync_rds.w2consul('awscloud',account,region,rds_dict)
        data = {'count':count,'update':now,'status':20000,'on':on,'off':off,'msg':f'rds同步成功!总数:{count},开机:{on},关机:{off}'}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/rds/{region}', data)
        logger.info(f'【JOB】===>awscloud_rds {account} {region} {data}')
    except TeaException as e:
        emsg = e.message.split('. ',1)[0]
        logger.error(f"【code:】{e.code}\n【message:】{e.message}\n{traceback.format_exc()}")
        data = consul_kv.get_value(f'ConsulManager/record/jobs/awscloud/{account}/rds/{region}')
        if data == {}:
            data = {'count':'无','update':f'失败{e.code}','status':50000,'msg':emsg}
        else:
            data['update'] = f'失败{e.code}'
            data['msg'] = emsg
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/rds/{region}', data)
    except Exception as e:
        logger.error(f'{e}\n{traceback.format_exc()}')
        data = {'count':'无','update':f'失败','status':50000,'msg':str(e)}
        consul_kv.put_kv(f'ConsulManager/record/jobs/awscloud/{account}/rds/{region}', data)
相关推荐
黄公子学安全38 分钟前
Java的基础概念(一)
java·开发语言·python
小峰编程1 小时前
独一无二,万字详谈——Linux之文件管理
linux·运维·服务器·云原生·云计算·ai原生
程序员一诺1 小时前
【Python使用】嘿马python高级进阶全体系教程第10篇:静态Web服务器-返回固定页面数据,1. 开发自己的静态Web服务器【附代码文档】
后端·python
小木_.2 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器
Jiude2 小时前
算法题题解记录——双变量问题的 “枚举右,维护左”
python·算法·面试
唐小旭2 小时前
python3.6搭建pytorch环境
人工智能·pytorch·python
是十一月末3 小时前
Opencv之对图片的处理和运算
人工智能·python·opencv·计算机视觉
爱学测试的李木子3 小时前
Python自动化测试的2种思路
开发语言·软件测试·python
終不似少年遊*3 小时前
华为云计算HCIE笔记04
网络·华为云·云计算·学习笔记·hcie·认证·数据中心
kitsch0x973 小时前
工具学习_Conan 安装第三方库
开发语言·python·学习