def GetAttribute(DBInstanceId, Key):
    Attribute = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
    )
    Attribute.set_DBInstanceId(DBInstanceId)
    AttributeInfo = clt.do_action_with_exception(Attribute)
    Info = json.loads(AttributeInfo)
    print Info['Items']['DBInstanceAttribute'][0][Key]
Beispiel #2
0
 def get_DescribeDBInstanceAttribute(self, DBInstanceId):
     '获取RDS基本属性'
     request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
     request.set_accept_format('json')
     request.set_action_name('DescribeDBInstanceAttribute')
     request.set_DBInstanceId(str(DBInstanceId))
     return self.clt.do_action_with_exception(request)
Beispiel #3
0
 def DescribeDBInstance(self, dbInstanceId):
     request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
     )
     request.set_accept_format('json')
     request.set_DBInstanceId(dbInstanceId)
     s = json.loads(self.clt.do_action(request))
     print s
def GetDBInstanceStorage(DBInstanceId, Key):
    DBInstanceStorage = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
    )
    DBInstanceStorage.set_accept_format('json')
    DBInstanceStorage.set_DBInstanceId(DBInstanceId)
    DBInstanceStorage = clt.do_action_with_exception(DBInstanceStorage)
    DBStorage = (
        json.loads(DBInstanceStorage))["Items"]["DBInstanceAttribute"][0][Key]
    #print DBStorage

    Performance = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest(
    )
    Performance.set_accept_format('json')
    Performance.set_DBInstanceId(DBInstanceId)
    Performance.set_Key("MySQL_DetailedSpaceUsage")
    Performance.set_StartTime(StartTime)
    Performance.set_EndTime(EndTime)
    PerformanceInfo = clt.do_action_with_exception(Performance)
    #print PerformanceInfo
    Info = (json.loads(PerformanceInfo))
    #print Info
    Value = Info['PerformanceKeys']['PerformanceKey'][0]['Values'][
        'PerformanceValue'][-1]['Value']
    UsedSpace = int(str(Value).split('&')[0]) / 1024
    #print UsedSpace

    print UsedSpace / float(DBStorage) * 100
def checkInstanceStatus(client, instanceId):
    # Creating request object
    request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
    )
    # Setting request paramentes
    request.set_DBInstanceId(instanceId)
    response = client.do_action_with_exception(request)
    return json.loads(
        response.decode("utf-8")
    )['Items']['DBInstanceAttribute'][0]['DBInstanceStatus'].strip()
Beispiel #6
0
 def query_mysql(self):
     self.get_DBid()
     for zone in self.zone:
         client = AcsClient('', '', zone)
         for ids in sum(self.DBid, []):
             request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
             )
             request.set_accept_format('json')
             request.set_DBInstanceId(ids['DBInstanceId'])
             response = json.loads(client.do_action_with_exception(request))
             self.DBinfo.append(response['Items']['DBInstanceAttribute'])
         return self.DBinfo
Beispiel #7
0
 def get_rds_instace(self, DBInstanceId):
     """
     获取rds实例内同
     :param DBInstanceId:
     :return:
     """
     request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
     )
     request.set_DBInstanceId(DBInstanceId)
     request.set_accept_format("json")
     result = json.loads(self.clt.do_action(request))
     print(result)
     return result
Beispiel #8
0
def update_Rds():
    Rds_Info = {}
    client = AcsClient(Config.configs['AliAK']['AccessKey'],
                       Config.configs['AliAK']['AccessKeySecret'], RegionId)
    request = DescribeDBInstancesRequest.DescribeDBInstancesRequest()
    request.set_PageSize(100)
    response = client.do_action_with_exception(request)
    RdsInfo = json.loads(response)

    for DBInstance in RdsInfo['Items']['DBInstance']:
        request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
        )
        request.set_DBInstanceId(DBInstance['DBInstanceId'])
        response = client.do_action_with_exception(request)
        Rds_Info[DBInstance['DBInstanceId']] = json.loads(
            response.decode('utf-8'))['Items']['DBInstanceAttribute'][0]

    conn.set('Rds_Info', json.dumps(Rds_Info))
Beispiel #9
0
 def get_instances(cls):
     ret = []
     instance_ids_list = cls._get_instance_ids()
     for clt, instance_ids in instance_ids_list:
         access_key = clt.get_access_key()
         # aliyun API only can accept at most 30 instance ids, so split into chunks
         chunk_instance_ids = [
             instance_ids[i:i + 30] for i in range(0, len(instance_ids), 30)
         ]
         for chunk in chunk_instance_ids:
             request = RDSAttrRequest.DescribeDBInstanceAttributeRequest()
             request.set_DBInstanceId(','.join(chunk))
             response = cls.do_single_request(clt, request)
             instances = []
             for i in response['Items']['DBInstanceAttribute']:
                 i['Access_key'] = access_key
                 instances.append(i)
             ret.extend(instances)
     return ret
Beispiel #10
0
	def query_mysql(self):
		try:
			for zone in current_app.config['ALIYUN_ZONE']:
				client = AcsClient(current_app.config['ALIYUN_ACCESS_KEYID'], current_app.config['ALIYUN_ACCESS_KEY_SECRET'], zone)
				request = DescribeDBInstancesRequest.DescribeDBInstancesRequest()
				request.set_accept_format('json')
				response = json.loads(client.do_action_with_exception(request))
				self.DBid.append(response['Items']['DBInstance'])
		except Exception as e:
			raise e
		for zone in current_app.config['ALIYUN_ZONE']:
			client = AcsClient(current_app.config['ALIYUN_ACCESS_KEYID'], current_app.config['ALIYUN_ACCESS_KEY_SECRET'],zone)
			for ids in sum(self.DBid, []):
				request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
				request.set_accept_format('json')
				request.set_DBInstanceId(ids['DBInstanceId'])
				response = json.loads(client.do_action_with_exception(request))
				self.DBinfo.append(response['Items']['DBInstanceAttribute'])
			return self.DBinfo
Beispiel #11
0
def get_rds_item(keys):
    '''
    通过实例ID获取该实例详情
    :param keys:
    :return:
    '''
    key_id = keys['key_id']
    key_secret = keys['key_secret']
    DBInstanceId = keys['DBInstanceId']
    result = {}
    try:
        client = AcsClient(key_id, key_secret)
        request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest(
        )
        request.set_DBInstanceId(DBInstanceId)
        response = client.do_action_with_exception(request)
        response_json = json.loads(response)
        Items = response_json['Items']['DBInstanceAttribute'][0]
        Cpu = str(Items['DBInstanceCPU'])
        Memory = str(math.floor(Items['DBInstanceMemory'] / 1024))
        result['cpuMemory'] = Cpu + 'CPU' + ' ' + Memory + 'GB'

        if Items.get('masterInstanceId') == None:
            result['masterInstanceId'] = ''
        else:
            result['masterInstanceId'] = Items.get('masterInstanceId')
        if Items.get('guardDBInstanceId') == None:
            result['guardDBInstanceId'] = ''
        else:
            result['guardDBInstanceId'] = Items.get('guardDBInstanceId')
        if not Items.get('ReadOnlyDBInstanceIds').get('ReadOnlyDBInstanceId'):
            result['readOnlyDBInstanceId'] = ''
        else:
            result['readOnlyDBInstanceId'] = [
                i.get('DBInstanceId') for i in Items.get(
                    'ReadOnlyDBInstanceIds').get('ReadOnlyDBInstanceId')
            ]

    except Exception as e:
        print(e)

    return result
Beispiel #12
0
def GetRdsAttribute(DBInstanceId):
    RdsAttribute = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
    RdsAttribute.set_accept_format('json')
    RdsAttribute.set_DBInstanceId(DBInstanceId)
    RdsAttributeInfo = clt.do_action_with_exception(RdsAttribute)
    Info = (json.loads(RdsAttributeInfo))
    Version = Info['Items']['DBInstanceAttribute'][0]['Engine'] +   Info['Items']['DBInstanceAttribute'][0]['EngineVersion']
    Lock =  Info['Items']['DBInstanceAttribute'][0]['LockMode']
    Status = Info['Items']['DBInstanceAttribute'][0]['DBInstanceStatus']
    Description = Info['Items']['DBInstanceAttribute'][0]['DBInstanceDescription']
    DBInstanceStorage = Info['Items']['DBInstanceAttribute'][0]['DBInstanceStorage']
    if Info['Items']['DBInstanceAttribute'][0]['LockMode'] != 'Unlock':
        LockReason = Info['Items']['DBInstanceAttribute'][0]['LockReason']
        print Description, '\t', Version, '\t', Lock, '\t', Status + '\t'+ '\tLockReason' + Info['Items']['DBInstanceAttribute'][0]['LockReason']
    else:
        LockReason = 'NULL'
        print Description, '\t', Version, '\t', Lock, '\t', Status + '\tLockReason' + '\tNULL'
    Sql = "update checklist set description='%s',version='%s',status='%s',lock_about='%s',lock_reason='%s',disktotal='%s' where item='%s'and date=curdate()" %(Description,Version,Status,Lock,LockReason,DBInstanceStorage,DBInstanceId)
    DatabaseConnect(Sql)
    print DBInstanceStorage
Beispiel #13
0
 def get_rds_instances(self, pageSize=100):
     request = DescribeDBInstancesRequest.DescribeDBInstancesRequest()
     request.set_accept_format('json')
     attributeRequest = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
     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), encoding='utf-8')
             region_result = clt_result['Items']['DBInstance']
             pageNumber += 1
             if len(region_result) == 0:
                 print(pageNumber, clt_result['PageRecordCount'])
                 break
             for Instance in region_result:
                 attributeRequest.add_query_param("action_name", "DescribeDBInstanceAttribute")
                 attributeRequest.add_query_param("DBInstanceId", Instance['DBInstanceId'])
                 r = json.loads(clt.do_action_with_exception(attributeRequest))
                 attr = r['Items']['DBInstanceAttribute'][0]
                 expired_time = attr.get('ExpireTime')
                 if not expired_time:
                     expired_time = None
                 yield {
                     'instance_id': attr.get('DBInstanceId'),
                     'region': attr.get('RegionId'),
                     'instance_name': attr.get('DBInstanceDescription'),
                     'network_type': attr.get('InstanceNetworkType'),
                     'engine_version': attr.get('EngineVersion'),
                     'status': attr.get('DBInstanceStatus'),
                     'expired_time': expired_time,
                     'connection_string': attr.get('ConnectionString'),
                     'connections': attr.get('MaxConnections'),
                     'cpu': attr.get('DBInstanceCPU'),
                     'memory': attr.get('DBInstanceMemory'),
                     'iops': attr.get('MaxIOPS'),
                     'storage': attr.get('DBInstanceStorage')
                 }
Beispiel #14
0
 def get_rds_list(self, pageNumber = 1, pageSize = 100, price = False):
     tmp    = []
     result = []
     request = DescribeDBInstancesRequest.DescribeDBInstancesRequest()
     request.set_accept_format('json')
     request.set_query_params(dict(PageNumber = pageNumber, PageSize = pageSize))
     request1 = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
     request1.set_accept_format('json')
     for clt in self.clt_conn_list:
         clt_result = json.loads(clt.do_action(request))
         result += clt_result['Items']['DBInstance']
         totalCount = clt_result['TotalRecordCount']
         while totalCount > pageNumber * pageSize:
             pageNumber += 1 
             request.set_query_params(dict(PageNumber = pageNumber, PageSize = pageSize))
             clt_result = json.loads(clt.do_action(request))
             result += clt_result['Items']['DBInstance']
     for r in result:
         request1.set_query_params({'DBInstanceId' : r['DBInstanceId']})
         tmp.append(json.loads(clt.do_action(request1))['Items']['DBInstanceAttribute'][0])
     if price:
         print len(tmp)
         url = 'https://buy.aliyun.com/ajax/CalculatorAjax/Product.jsonp?callback=jQuery&commodity_code=rds&spec_code=rds' 
         headers = {'Referer' : 'https://www.aliyun.com/price/product'}
         rds_region_Product = json.loads(self.requests.get(url, headers = headers).text.replace('jQuery(', '').replace(');', ''))
         rds_region_list    = set([x['value'] for x in rds_region_Product['data']['components']['rds_region']['rds_region']])
         for num, ri in enumerate(tmp): 
             if ri['PayType'] == 'Prepaid' and ri['DBInstanceType'] == 'Primary':
                 rds_class  = ri['DBInstanceClass']
                 rds_region = [x for x in rds_region_list if ri['RegionId'] in x][0]
                 rds_dbtype = ri['Engine'].lower()
                 rds_storage = ri['DBInstanceStorage']
                 rds_price = self.get_rds_price(rds_class = rds_class, rds_region = rds_region, rds_dbtype = rds_dbtype, rds_storage = rds_storage, discount = 1)
                 if rds_price['code'] == 200:
                     ri['price'] = rds_price
                     tmp[num] = ri
     return tmp
Beispiel #15
0
    def get_rds_detail_by_ids(self, instance_list):

        id_str = ','.join(instance_list)
        request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
        request.set_DBInstanceId(id_str)

        result = self._request(request)

        desc_key = 'DBInstanceDescription'
        pfb_db_list = [
            '-master',
            '-redash-slave',
            #'brcb-prod-master'
        ]

        id_desc_list = {}

        for item in result['Items']['DBInstanceAttribute']:
            if desc_key not in item or item[desc_key] not in pfb_db_list:
                continue
            else:
                id_desc_list[item[desc_key]] = item['DBInstanceId']

        return id_desc_list
Beispiel #16
0
def RDSMonitor(ak, aks, region, DBInstanceId):
    gpus = ""
    if len(sys.argv) > 1:
        gpus = sys.argv[1]
        if gpus != "MaxConnections" and gpus != "MaxIOPS" and gpus != "DBInstanceStorage" and gpus != "MemoryUsage" and gpus != "DBInstanceMemory":
            if gpus.find("_") == -1:
                print "参数有误,必须包含\"_\"。如:MySQL_NetworkTraffic"
                return

    RDS = {}

    if gpus == "MySQL_MemoryUsage":
        # 查询该实例云监控内存使用率的平均值
        try:
            clt = client.AcsClient(ak, aks, region)
            request3 = QueryMetricListRequest.QueryMetricListRequest()
            request3.set_accept_format('json')
            request3.set_Project('acs_rds_dashboard')
            start_time = (datetime.datetime.now() - datetime.timedelta(minutes=8)).strftime('%Y-%m-%d %H:%M:%S')
            timestamp_start = int(time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))) * 1000
            request3.set_StartTime(timestamp_start)
            request3.set_Metric('MemoryUsage')
            request3.set_Dimensions("{'instanceId':'" + DBInstanceId + "'}")
            request3.set_Period('300')
            result = json.loads(clt.do_action_with_exception(request3))
            print result['Datapoints'][-1]['Average']
        except Exception as e:
            print str(e)
        finally:
            return

    try:
        clt = client.AcsClient(ak, aks, region)
        request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
        request.set_DBInstanceId(DBInstanceId)
        request.set_Key(PERFORMANCELIST)
        request.set_StartTime(getRegionTime(region)[0])
        request.set_EndTime(getRegionTime(region)[1])

        response = clt.do_action_with_exception(request)
        response_dir = json.loads(response)
    except:
        print "ak,aks,地域或者实例ID配置有误"
    if response_dir['PerformanceKeys']['PerformanceKey'][0]['Values']['PerformanceValue'] == []:
        print  "还没有最新数据"
        return

    Engine = str(response_dir['Engine'])

    if Engine == 'MySQL':
        for i in range(len(response_dir['PerformanceKeys']['PerformanceKey'])):
            MySQL_key_head = str(response_dir['PerformanceKeys']['PerformanceKey'][i]['Key'])
            if MySQL_key_head == 'slavestat':
                continue
            MySQL_key_bodyList = str(response_dir['PerformanceKeys']['PerformanceKey'][i]['ValueFormat']).split('&')
            MySQL_valueList = str(
                response_dir['PerformanceKeys']['PerformanceKey'][i]['Values']['PerformanceValue'][-1]['Value']).split(
                '&')
            for i in range(len(MySQL_key_bodyList)):
                RDS[MySQL_key_head + "_" + MySQL_key_bodyList[i]] = MySQL_valueList[i]

    elif Engine == 'SQLServer':

        for i in range(len(response_dir['PerformanceKeys']['PerformanceKey'])):
            SQLServer_key = str(response_dir['PerformanceKeys']['PerformanceKey'][i]['Key'])
            if SQLServer_key == 'SQLServer_NetworkTraffic' or 'SQLServer_DetailedSpaceUsage':
                SQLServer_key_bodyList = str(response_dir['PerformanceKeys']['PerformanceKey'][i]['ValueFormat']).split(
                    '&')
                SQLServer_valueList = str(
                    response_dir['PerformanceKeys']['PerformanceKey'][i]['Values']['PerformanceValue'][-1][
                        'Value']).split('&')
                for i in range(len(SQLServer_key_bodyList)):
                    RDS[SQLServer_key + "_" + SQLServer_key_bodyList[i]] = SQLServer_valueList[i]

            else:
                SQLServer_value = str(
                    response_dir['PerformanceKeys']['PerformanceKey'][i]['Values']['PerformanceValue'][-1]['Value'])
                RDS[SQLServer_key] = SQLServer_value

    # 查询该实例的最大连接数,最大iops数
    request2 = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest()
    request2.set_DBInstanceId(DBInstanceId)
    request2.set_accept_format('json')
    response2 = clt.do_action_with_exception(request2)
    response_dir2 = json.loads(response2)
    try:
        RDS['MaxConnections'] = str(response_dir2['Items']['DBInstanceAttribute'][0]['MaxConnections'])
        RDS['MaxIOPS'] = str(response_dir2['Items']['DBInstanceAttribute'][0]['MaxIOPS'])
        RDS['DBInstanceStorage'] = str(response_dir2['Items']['DBInstanceAttribute'][0]['DBInstanceStorage'])
        RDS['DBInstanceMemory'] = str(response_dir2['Items']['DBInstanceAttribute'][0]['DBInstanceMemory'])
    except:
        pass

    if gpus != "":
        try:
            print RDS[gpus]
        except:
            print "参数有误,或此数据库应为%s类型,应以\"%s_\"开头" % (Engine, Engine)
    else:
        print json.dumps(RDS, indent=4)