-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi_query_data.py
27 lines (23 loc) · 1.24 KB
/
api_query_data.py
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
from subprocess import getoutput
from datetime import datetime
from datetime import timedelta
from json import loads
from csv import writer
LatestTime = "2022-09-21T01:00:00"
dbInstanceIdentifier = "dbidentifier"
daysToRetrieve = 31
csvOutput = [["Identifier","Timestamp","CPU Usage"]]
statistics = "Average"
for day in range(0, daysToRetrieve):
endTime = datetime.isoformat(datetime.strptime(LatestTime, "%Y-%m-%dT%H:%M:%S") - timedelta(days=day))
startTime = datetime.isoformat(datetime.strptime(endTime, "%Y-%m-%dT%H:%M:%S") - timedelta(days=1))
cwOutput = getoutput(f'aws cloudwatch get-metric-statistics --namespace "AWS/RDS" --metric-name "CPUUtilization" --start-time {startTime}Z --end-time {endTime}Z --period 300 --statistics {statistics} --dimensions Name=DBInstanceIdentifier,Value={dbInstanceIdentifier}')
daysOutput = {}
daysOutput = loads(cwOutput)
for dataPoint in range(0, len(daysOutput['Datapoints'])):
Timestamp = daysOutput['Datapoints'][dataPoint]['Timestamp']
cpuUsage = daysOutput['Datapoints'][dataPoint][statistics]
csvOutput.append([dbInstanceIdentifier,Timestamp,cpuUsage])
with open('output.csv', 'w', newline='') as file: # Open the file, write the data.
writer = writer(file)
writer.writerows(csvOutput)