使用阿里云API-SDK获取阿里云Redis的监控指标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import jsonimport datetimefrom aliyunsdkcore.client import AcsClientfrom aliyunsdkcore.acs_exception.exceptions import ClientExceptionfrom aliyunsdkcore.acs_exception.exceptions import ServerExceptionfrom aliyunsdkr_kvstore.request.v20150101.DescribeHistoryMonitorValuesRequest import DescribeHistoryMonitorValuesRequestdef get_metric (metric ): client = AcsClient('<accessKeyId>' , '<accessSecret>' , 'cn-beijing' ) request = DescribeHistoryMonitorValuesRequest() request.set_accept_format('json' ) utc_now = datetime.datetime.utcnow() utc_minutes_later = utc_now + datetime.timedelta(minutes=1 ) aliyun_utc_format = '%Y-%m-%dT%H:%M:00Z' start_time = utc_now.strftime(aliyun_utc_format) end_time = utc_minutes_later.strftime(aliyun_utc_format) request.set_StartTime(start_time) request.set_EndTime(end_time) request.set_IntervalForHistory('01m' ) request.set_InstanceId('r-2zef7cf6dadbd110' ) request.set_MonitorKeys(metric) response = client.do_action_with_exception(request) resp_json = json.loads(response.decode()) monitor_history = resp_json.get('MonitorHistory' ) print (json.loads(monitor_history).get(start_time).get(metric)) if __name__ == "__main__" : get_metric('Keys' ) get_metric('CpuUsage' ) get_metric('InFlow' ) get_metric('OutFlow' ) get_metric('UsedMemory' ) get_metric('memoryUsage' )
引用参考