import boto3
from datetime import datetime
from datetime import timedelta
cloudwatch = boto3.client('cloudwatch')
def rds_connections():
StartTime = datetime(2023,5,1) - timedelta(hours=9)
# 한국시간에 맞게 -0900
# 한국기준으로 5월 1일로 지정해줬기 때문에, UTC는 4월 30일 15시 데이터 추출한다.
EndTime = StartTime + timedelta(days=1)
response = cloudwatch.get_metric_statistics(
Namespace='AWS/RDS',
MetricName='DatabaseConnections',
Dimensions=[
{
'Name': 'DBInstanceIdentifier',
'Value': 'string'}, # 추출할 RDS 식별자 Name을 넣어준다
],
StartTime=StartTime,
EndTime=EndTime,
Period=600,
Statistics=['Average'],
Unit='Count')
return response['Datapoints']
dct = {}
for i in rds_connections():
dct[i['Timestamp'] + timedelta(hours=9)] = i['Average']
# 출력되는 데이터는 한국시간으로 변환 UTC +0900
for k, v in sorted(dct.items()):
print(str(k)[:19], v)
2023-05-01 00:00:00 90.0
2023-05-01 00:10:00 90.1
2023-05-01 00:20:00 90.0
2023-05-01 00:30:00 90.0
2023-05-01 00:40:00 90.0
2023-05-01 00:50:00 90.1
2023-05-01 01:00:00 90.0
2023-05-01 01:10:00 90.1
2023-05-01 01:20:00 90.1
2023-05-01 01:30:00 90.1
2023-05-01 01:40:00 90.1
...
...
...
2023-05-01 21:50:00 100.1
2023-05-01 22:00:00 100.1
2023-05-01 22:10:00 100.3
2023-05-01 22:20:00 100.0
2023-05-01 22:30:00 100.0
2023-05-01 22:40:00 100.1
2023-05-01 22:50:00 100.0
2023-05-01 23:00:00 100.1
2023-05-01 23:10:00 100.1
2023-05-01 23:20:00 100.0
2023-05-01 23:30:00 100.2
2023-05-01 23:40:00 100.0
2023-05-01 23:50:00 100.1