AWS Infra

[ AWS ] Python boto3를 이용하여 ElastiCache Database 메모리 사용량 확인

Pydole 2023. 5. 31. 15:04

 

 

from datetime import datetime
from datetime import timedelta
import boto3

cloudwatch = boto3.client('cloudwatch')  

def elastiCache_Database_Memory():
    # 샘플 2023년 5월 1일 데이터
    
    st = datetime(2023,5,1,0,0,0) - timedelta(hours=9)
    et = st + timedelta(days=1)

    response = cloudwatch.get_metric_statistics(
            Namespace='AWS/ElastiCache',
            MetricName='DatabaseMemoryUsagePercentage',
            Dimensions=[
                { 
                 'Name': 'CacheClusterId',
                 'Value': 'string'},         # Cluster Node Name
                ],
            StartTime=st,
            EndTime=et,
            Period=3600,
            Statistics=['Average'],
            Unit='Percent')

    return response['Datapoints']

dct = {}

for i in elastiCache_Database_Memory():
    dct[i['Timestamp'] + timedelta(hours=9)] = round(i['Average'],2)
    # 출력되는 데이터는 한국시간으로 변환 UTC +0900

for k, v in sorted(dct.items()):
    print(str(k)[:19], v)

 

2023-05-01 00:00:00 0.32
2023-05-01 01:00:00 0.32
2023-05-01 02:00:00 0.32
2023-05-01 03:00:00 0.32
2023-05-01 04:00:00 0.32
2023-05-01 05:00:00 0.32
2023-05-01 06:00:00 0.32
2023-05-01 07:00:00 0.32
2023-05-01 08:00:00 0.32
2023-05-01 09:00:00 0.32
2023-05-01 10:00:00 0.32
2023-05-01 11:00:00 0.32
2023-05-01 12:00:00 0.32
2023-05-01 13:00:00 0.32
2023-05-01 14:00:00 0.32
2023-05-01 15:00:00 0.32
2023-05-01 16:00:00 0.32
2023-05-01 17:00:00 0.32
2023-05-01 18:00:00 0.32
2023-05-01 19:00:00 0.32
2023-05-01 20:00:00 0.32
2023-05-01 21:00:00 0.32
2023-05-01 22:00:00 0.32
2023-05-01 23:00:00 0.32