Exemplo n.º 1
0
 def get_DescribeDBInstancePerformance(self, DBInstanceId, key, StartTime, EndTime):
     """
     获取RDS监控数据
     key包含以下项目:
         MySQL_NetworkTraffic	    MySQL实例平均每秒钟的输入流量,MySQL实例平均每秒钟的输出流量。单位为KB。
         MySQL_QPSTPS	            平均每秒SQL语句执行次数,平均每秒事务数
         MySQL_Sessions	            当前活跃连接数,当前总连接数
         MySQL_InnoDBBufferRatio	    InnoDB缓冲池的读命中率,InnoDB缓冲池的利用率,InnoDB缓冲池脏块的百分率
         MySQL_InnoDBDataReadWriten	InnoDB平均每秒钟读取的数据量,InnoDB平均每秒钟写入的数据量。单位为KB
         MySQL_InnoDBLogRequests	    平均每秒向InnoDB缓冲池的读次数,平均每秒向InnoDB缓冲池的写次数
         MySQL_InnoDBLogWrites	    平均每秒日志写请求数,平均每秒向日志文件的物理写次数,平均每秒向日志文件完成的fsync()写数量
         MySQL_TempDiskTableCreates	MySQL执行语句时在硬盘上自动创建的临时表的数量
         MySQL_MyISAMKeyBufferRatio	MyISAM平均每秒Key Buffer利用率,MyISAM平均每秒Key Buffer读命中率,MyISAM平均每秒Key Buffer写命中率
         MySQL_MyISAMKeyReadWrites	MyISAM平均每秒钟从缓冲池中的读取次数,MyISAM平均每秒钟从缓冲池中的写入次数,MyISAM平均每秒钟从硬盘上读取的次数,MyISAM平均每秒钟从硬盘上写入的次数
         MySQL_COMDML	            平均每秒Delete语句执行次数,平均每秒Insert语句执行次数, 平均每秒Insert_Select语句执行次数,平均每秒Replace语句执行次数,平均每秒Replace_Select语句执行次数,平均每秒Select语句执行次数,平均每秒Update语句执行次数
         MySQL_RowDML	            平均每秒从InnoDB表读取的行数,平均每秒从InnoDB表更新的行数,平均每秒从InnoDB表删除的行数,平均每秒从InnoDB表插入的行数,平均每秒向日志文件的物理写次数
         MySQL_MemCpuUsage	        MySQL实例CPU使用率(占操作系统总数),MySQL实例内存使用率(占操作系统总数)
         MySQL_IOPS	                MySQL实例的IOPS(每秒IO请求次数)
         MySQL_DetailedSpaceUsage	MySQL实例空间占用详情:ins_size实例总空间使用量;data_size数据空间;log_size日志空间;tmp_size临时空间;other_size系统空间
         slavestat	                只读实例延迟
     """
     request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
     request.set_accept_format('json')
     request.set_DBInstanceId(DBInstanceId)
     request.set_Key(key)
     request.set_StartTime(StartTime)
     request.set_EndTime(EndTime)
     result = json.loads(self.client.do_action_with_exception(request))
     return result
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def GetPerformance(DBInstanceID, MasterKey):
    '''
    阿里云返回的数据为 UTC 时间,因此要转换为东八区时间。其他时区同理。
    最小时间间隔为 1 分钟,因此这里选择时间跨度为 1 分钟
    '''
    UTC_End = datetime.datetime.today() - datetime.timedelta(hours=8)
    UTC_Start = UTC_End - datetime.timedelta(minutes=1)
    StartTime = datetime.datetime.strftime(UTC_Start, '%Y-%m-%dT%H:%MZ')
    EndTime = datetime.datetime.strftime(UTC_End, '%Y-%m-%dT%H:%MZ')
    Performance = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest(
    )
    Performance.set_accept_format('json')
    Performance.set_DBInstanceId(DBInstanceID)
    Performance.set_Key(MasterKey)
    Performance.set_StartTime(StartTime)
    Performance.set_EndTime(EndTime)
    PerformanceInfo = client.do_action_with_exception(Performance)
    Info = json.loads(PerformanceInfo)
    Value = Info['PerformanceKeys']['PerformanceKey']
    MetricToValue = dict()
    for i in Value:
        a = dict()
        if "&" in str(i):
            vf = i["ValueFormat"].split('&')
            vl = i["Values"]["PerformanceValue"][0]["Value"].split('&')
            l = len(vf)
            while l != 0:
                l -= 1
                a[vf[l]] = vl[l]
        else:
            a[i['ValueFormat']] = i['Values']['PerformanceValue'][0]['Value']
        MetricToValue.update(a)
    return MetricToValue
Exemplo n.º 4
0
 def get_instance_monitordata(self, instance_id, keys, starttime, endtime):
     """获取 RDS 实例的监控数据"""
     request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest(
     )
     request.add_query_param('DBInstanceId', instance_id)
     request.add_query_param('Key', keys)
     request.add_query_param('StartTime', starttime)
     request.add_query_param('EndTime', endtime)
     res = self.clt.do_action_with_exception(request)
     return json.loads(res.decode())
Exemplo n.º 5
0
def GetPerformance(DBInstanceId,MasterKey,IndexNum,StartTime,EndTime):
    Performance = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    Performance.set_accept_format('json')
    Performance.set_DBInstanceId(DBInstanceId)
    Performance.set_Key(MasterKey)
    Performance.set_StartTime(StartTime)
    Performance.set_EndTime(EndTime)
    PerformanceInfo = clt.do_action_with_exception(Performance)
    #print PerformanceInfo
    Info = (json.loads(PerformanceInfo))
    Value = Info['PerformanceKeys']['PerformanceKey'][0]['Values']['PerformanceValue'][-1]['Value']
    print str(Value).split('&')[IndexNum]
Exemplo n.º 6
0
 def query_rds_performance_metrics(self, id):
     req = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
     req.set_DBInstanceId(id)
     req.set_Key(','.join([metric['name'] for metric in self.parent.metrics[rds_performance]]))
     now = datetime.utcnow();
     now_str = now.replace(second=0, microsecond=0).strftime("%Y-%m-%dT%H:%MZ")
     one_minute_ago_str = (now - timedelta(minutes=1)).replace(second=0, microsecond=0).strftime("%Y-%m-%dT%H:%MZ")
     req.set_StartTime(one_minute_ago_str)
     req.set_EndTime(now_str)
     try:
         resp = self.parent.client.do_action_with_exception(req)
     except Exception as e:
         logging.error('Error request rds performance api', exc_info=e)
         return []
     data = json.loads(resp)
     return data['PerformanceKeys']['PerformanceKey']
Exemplo n.º 7
0
def getPerformanceUsage(client, dbInstanceId, startTime, endTime):
    request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest(
    )
    request.set_DBInstanceId(dbInstanceId)
    request.set_Key(
        "PGSQL_MemCpuUsage,PGSQL_SpaceUsage,PGSQL_IOPS,PSSQL_Sessions")
    request.set_StartTime(startTime)
    request.set_EndTime(endTime)
    try:
        response = client.do_action_with_exception(request)
        return response
    except ServerException as exception:
        print("Error while getting performance period.")
        print("HTTP statue = ", exception.get_http_status())
        print("HTTP Error code = ", exception.get_error_code())
        print("Error message = ", exception.get_error_msg())
    return None
Exemplo n.º 8
0
def GetPerformance(DBInstanceId,MasterKey,StartTime,EndTime):
    Performance = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest()
    Performance.set_accept_format('json')
    Performance.set_DBInstanceId(DBInstanceId)
    Performance.set_Key(MasterKey)
    Performance.set_StartTime(StartTime)
    Performance.set_EndTime(EndTime)
    PerformanceInfo = clt.do_action_with_exception(Performance)
    Info = (json.loads(PerformanceInfo))
    FirstList = []
    SecondList = []
    for i in Info['PerformanceKeys']['PerformanceKey'][0]['Values']['PerformanceValue']:
        FirstList.append(i['Value'].split('&')[0])
    for i in range(0, len(FirstList)):
        for j in range(i + 1, len(FirstList)):
            first = float(FirstList[i])
            second = float(FirstList[j])
            if first < second:
                FirstList[i] = FirstList[j]
                FirstList[j] = first
    MaxFirst = FirstList[0]
    for i in Info['PerformanceKeys']['PerformanceKey'][0]['Values']['PerformanceValue']:
        SecondList.append(i['Value'].split('&')[1])
    for i in range(0, len(SecondList)):
        for j in range(i + 1, len(SecondList)):
            first = float(SecondList[i])
            second = float(SecondList[j])
            if first < second:
                SecondList[i] = SecondList[j]
                SecondList[j] = first
    MaxSecond = SecondList[0]
    if (MasterKey == 'MySQL_QPSTPS'):
        QPS = MaxFirst
        TPS = MaxSecond
        Sql = "update checklist set max_QPS='%s',max_TPS='%s' where item='%s' and date=curdate()" % (QPS,TPS, DBInstanceId)
        print QPS, TPS
    elif (MasterKey == 'MySQL_MemCpuUsage'):
        CpuUsage = MaxFirst
        MemUsage = MaxSecond
        Sql = "update checklist set max_cpu='%s',max_mem='%s' where item='%s' and date=curdate()" % (CpuUsage, MemUsage, DBInstanceId)
        print CpuUsage,MemUsage
    DatabaseConnect(Sql)
Exemplo n.º 9
0
 def get_performance(self):
     """
     获取各项性能指标
     """
     request = DescribeDBInstancePerformanceRequest.DescribeDBInstancePerformanceRequest(
     )
     request.set_accept_format('json')
     request.set_DBInstanceId(self.db_instance_id)
     request.set_Key(self.master_key)
     request.set_StartTime(StartTime)
     request.set_EndTime(EndTime)
     response = self.ali_client.do_action_with_exception(request)
     info = (json.loads(response))
     value_list = info['PerformanceKeys']['PerformanceKey'][0]['Values'][
         'PerformanceValue'][-1]['Value'].split('&')
     key_list = info['PerformanceKeys']['PerformanceKey'][0][
         'ValueFormat'].split('&')
     key_value = dict(zip(key_list, value_list))
     value_time = info['PerformanceKeys']['PerformanceKey'][0]['Values'][
         'PerformanceValue'][-1]["Date"]  # UTC时间
     return key_value, value_time
Exemplo n.º 10
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)