Exemple #1
0
 def get_kvstore_instances(self, pageSize=50):
     request = kvInstancesRequest.DescribeInstancesRequest()
     request.set_accept_format('json')
     attributeRequest = DescribeInstanceAttributeRequest.DescribeInstanceAttributeRequest()
     attributeRequest.set_accept_format('json')
     for clt in self.clt_conn_list:
         pageNumber = 1
         while True:
             request.set_query_params(dict(PageNumber=pageNumber, PageSize=pageSize))
             clt_result = json.loads(clt.do_action_with_exception(request))
             region_result = clt_result['Instances']['KVStoreInstance']
             pageNumber += 1
             if len(region_result) == 0:
                 print(pageNumber, clt_result['TotalCount'])
                 break
             for Instance in region_result:
                 yield {
                     'instance_id': Instance['InstanceId'],
                     'network_type': Instance['NetworkType'],
                     'instance_name': Instance['InstanceName'],
                     'instance_class': Instance['InstanceClass'],
                     'engine_version': Instance['EngineVersion'],
                     'region': Instance['RegionId'],
                     'status': Instance['InstanceStatus'],
                     'capacity': Instance['Capacity'],
                     'qps': Instance['QPS'],
                     'bandwidth': Instance['Bandwidth'],
                     'connections': Instance['Connections'],
                     'expired_time': Instance['EndTime'],
                     'connection_domain': Instance['ConnectionDomain']
                 }
Exemple #2
0
def redis(request_mongodb):
    request_mongodb.set_accept_format('json')
    request_mongodb.set_PageSize(50)  # 每页条数
    request_mongodb.set_PageNumber(1)  # 第几页
    # PageNumber, PageSize
    response = json.loads(clt.do_action_with_exception(request_mongodb),
                          encoding='utf-8')
    infoList = response['Instances']['KVStoreInstance']
    if infoList:
        save_data(infoList)
        total = response.get('TotalCount')
        num = int(math.ceil(total / 50.0))
        index = 1
        while index < num:
            index = index + 1
            request_mongodb = DescribeInstancesRequest.DescribeInstancesRequest(
            )
            request_mongodb.set_accept_format('json')
            request_mongodb.set_PageSize(50)  # 每页条数
            request_mongodb.set_PageNumber(index)  # 第几页
            # PageNumber, PageSize
            response = json.loads(
                clt.do_action_with_exception(request_mongodb),
                encoding='utf-8')
            infoList = response.get('Instances').get('Instance')
            save_data(infoList)
Exemple #3
0
 def request_get_instance(self, instance_id):
     request = DescribeInstancesRequest.DescribeInstancesRequest()
     self.request_to_json(request)
     request.add_query_param('RegionId', 'cn-hangzhou')
     request.add_query_param('InstanceIds', instance_id)
     try:
         response = self.clt.do_action_with_exception(request)
     except:
         return {}
     return self.get_json_results(response).get('Instances').get('KVStoreInstance')[0]
Exemple #4
0
 def request_get_page_number(self):
     request = DescribeInstancesRequest.DescribeInstancesRequest()
     request.add_query_param('PageNumber', 1)
     request.add_query_param('PageSize', 1)
     try:
         response = self.clt.do_action_with_exception(request)
     except:
         return {}
     result = self.get_json_results(response)
     return int(result.get('TotalCount')/settings.ALIYUN_PAGESIZE)+1
Exemple #5
0
 def request_get_instances(self, page):
     request = DescribeInstancesRequest.DescribeInstancesRequest()
     self.request_to_json(request)
     request.add_query_param('RegionId', 'cn-hangzhou')
     request.add_query_param('PageNumber', page)
     request.add_query_param('PageSize', settings.ALIYUN_PAGESIZE)
     try:
         response = self.clt.do_action_with_exception(request)
     except:
         return {}
     return self.get_json_results(response).get('Instances').get('KVStoreInstance')
    def get_aliredis_instanceid(self, regionids):
        for regionid in regionids:
            self.client.set_region_id(regionid)
            # 设置初始页码
            pageNumber = 1

            request = DescribeInstancesRequest.DescribeInstancesRequest()
            request.set_accept_format('json')
            request.set_PageSize(10)
            request.set_PageNumber(pageNumber)

            # 发起API请求并显示返回值
            response = self.client.do_action_with_exception(request)
            response_dict = json.loads(response)

            # 生成生成器
            while response_dict['Instances']['KVStoreInstance']:
                yield response_dict['Instances']['KVStoreInstance']
                pageNumber += 1
                request.set_PageNumber(pageNumber)
                response = self.client.do_action_with_exception(request)
                response_dict = json.loads(response)
Exemple #7
0
    def list_redis(self, name_only=False, **kwargs):
        """
        获取实例列表信息
        https://help.aliyun.com/document_detail/60933.html?spm=a2c4g.11186623.6.694.67002cacaswc5I
        :param name_only:
        :param kwargs:
        :return:
        """
        result_list = []
        request = DescribeInstancesRequest.DescribeInstancesRequest()
        regions = self.get_regions(id_only=True)
        for region in regions:
            request.set_PageSize(100)
            request.set_PageNumber(1)
            for k, v in kwargs.items():
                request.add_query_param(k, v)
            request.add_query_param('RegionId', region)
            result = self.client.do_action(request)
            result_list.extend(
                result.get('Instances', {}).get('KVStoreInstance', []))

            total_count = float(result.get('TotalCount', 0))
            if not total_count:
                continue
            page_count = float(result.get('PageSize', 1))
            _p = total_count / page_count
            if _p > 1:
                for _p_num in range(2, int(_p) + 2):
                    request.set_PageNumber(_p_num)
                    result = self.client.do_action(request)
                    result_list.extend(
                        result.get('Instances', {}).get('KVStoreInstance', []))
            # 防止被限流
            time.sleep(1)
        if name_only:
            result_list = [i['InstanceName'] for i in result_list]
        return result_list
Exemple #8
0
    def getAliyunRedisInfo(self, region_ids):
        for region_id in region_ids:

            self.client.set_region_id(region_id)
            # 设置初始页码
            pageNumber = 1

            # 创建request,并设置参数
            request = redisDescribeInstancesRequest.DescribeInstancesRequest()
            request.set_accept_format('json')
            request.set_PageSize(10)
            request.set_PageNumber(pageNumber)
            response = self.client.do_action_with_exception(request)

            # 发起API请求并显示返回值
            response_dict = json.loads(response.decode('utf-8'))

            # 生成生成器
            while response_dict['Instances']['KVStoreInstance']:
                yield response_dict['Instances']['KVStoreInstance']
                pageNumber += 1
                request.set_PageNumber(pageNumber)
                response = self.client.do_action_with_exception(request)
                response_dict = json.loads(response.decode('utf-8'))
 def redis_info(self) -> GaugeMetricFamily:
     req = DescribeRedis.DescribeInstancesRequest()
     return self.info_template(
         req,
         'aliyun_meta_redis_info',
         to_list=lambda data: data['Instances']['KVStoreInstance'])
Exemple #10
0
            dts_list = response['SynchronizationInstances']
            save_dts(dts_list)


if __name__ == '__main__':
    conn = pymysql.connect(user=dbUser,
                           passwd=dbPass,
                           host=dbHost,
                           db=dbName,
                           use_unicode=True,
                           charset="utf8")
    cur = conn.cursor()

    regions = [
        "cn-qingdao", "cn-hangzhou", "cn-beijing", "cn-shanghai",
        "cn-shenzhen", "cn-zhangjiakou"
    ]
    for region in regions:
        # 获得cn-qingdao MongoDB列表
        clt = client.AcsClient(accessKey, accessSecret, region)
        clt_dts = client.AcsClient(accessKey, accessSecret)
        request_mongodb = DescribeInstancesRequest.DescribeInstancesRequest()
        request_redis = DescribeDBInstancesRequest.DescribeDBInstancesRequest()
        request_dts = DescribeSynchronizationJobsRequest.DescribeSynchronizationJobsRequest(
        )
        mongoDB(request_redis)
        redis(request_mongodb)
        dts(request_dts)

    cur.close()
    conn.close()