forked from awslabs/amazon-documentdb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment-scanner.py
More file actions
348 lines (282 loc) · 17.6 KB
/
Copy pathdeployment-scanner.py
File metadata and controls
348 lines (282 loc) · 17.6 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
import boto3
import datetime
import argparse
import requests
import json
import sys
import os
def deleteLog(appConfig):
if os.path.exists(appConfig['logFileName']):
os.remove(appConfig['logFileName'])
def printLog(thisMessage,appConfig):
with open(appConfig['logFileName'], 'a') as fp:
fp.write("{}\n".format(thisMessage))
def get_cw_metric_daily_average(appConfig, cwClient, cwMetric, cwMath, cwCluster):
namespace = "AWS/DocDB"
metric = cwMetric
period = 87600 # Seconds in a day
dimensions = [{"Name":"DBClusterIdentifier","Value":cwCluster}]
startTime = appConfig['startTime']
endTime = appConfig['endTime']
response = cwClient.get_metric_statistics(
Namespace=namespace,
MetricName=metric,
StartTime=startTime,
EndTime=endTime,
Period=period,
Statistics=[cwMath],
Dimensions=dimensions,
)
metricValues = {}
cwMetricTotal = 0
cwMetricValues = 0
cwMetricAverage = 0
for cw_metric in response['Datapoints']:
metricValues[cw_metric['Timestamp']] = cw_metric.get(cwMath)
cwMetricTotal += cw_metric.get(cwMath)
cwMetricValues += 1
#if cwMetric == 'CPUSurplusCreditsCharged':
# print("{}".format(cw_metric))
if cwMetricValues == 0:
cwMetricAverage = int(0)
else:
cwMetricAverage = int(cwMetricTotal // cwMetricValues)
return cwMetricAverage
def get_docdb_instance_based_clusters(appConfig, pricingDict):
gbBytes = 1000 * 1000 * 1000
gibBytes = 1024 * 1024 * 1024
client = boto3.client('docdb',region_name=appConfig['region'])
cwClient = boto3.client('cloudwatch',region_name=appConfig['region'])
printLog("{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}".format('cluster','io-type','version','num-instances','standard-compute','standard-io','standard-storage','standard-backup','standard-total','io-optimized-compute','io-optimized-io','io-optimized-storage','io-optimized-backup','io-optimized-total','recommendation','estimated-potential-savings'),appConfig)
#response = client.describe_db_clusters()
response = client.describe_db_clusters(Filters=[{'Name': 'engine','Values': ['docdb']}])
for thisCluster in response['DBClusters']:
monthlyStandard = 0.00
monthlyIoOptimized = 0.00
ioType = thisCluster.get('StorageType','standard')
#print("{}".format(thisCluster.get('StorageType','missing')))
thisMonthlyStandardIoCompute = 0.00
thisMonthlyOptimizedIoCompute = 0.00
numInstances = 0
engineVersionFull = thisCluster['EngineVersion']
engineVersionMajor = int(engineVersionFull.split('.')[0])
for thisInstance in thisCluster['DBClusterMembers']:
# get instance type
responseInstance = client.describe_db_instances(DBInstanceIdentifier=thisInstance['DBInstanceIdentifier'])
numInstances += 1
thisStandardIoCompute = round(float(pricingDict['compute|'+appConfig['region']+'|'+responseInstance['DBInstances'][0]['DBInstanceClass']+'|standard']['price'])*30*24,0)
thisOptimizedIoCompute = round(float(pricingDict['compute|'+appConfig['region']+'|'+responseInstance['DBInstances'][0]['DBInstanceClass']+'|iopt1']['price'])*30*24,0)
thisMonthlyStandardIoCompute += thisStandardIoCompute
thisMonthlyOptimizedIoCompute += thisOptimizedIoCompute
print("")
print("cluster = {} | IO type = {} | version = {} | instances = {:d}".format(thisCluster['DBClusterIdentifier'],ioType,engineVersionFull,numInstances))
print(" ESTIMATED standard storage costs | ESTIMATED io optimized storage costs")
monthlyStandard += thisMonthlyStandardIoCompute
monthlyIoOptimized += thisMonthlyOptimizedIoCompute
# get historical cloudwatch information
avgReadIopsMonth = get_cw_metric_daily_average(appConfig, cwClient, 'VolumeReadIOPs', 'Sum', thisCluster['DBClusterIdentifier'])
avgWriteIopsMonth = get_cw_metric_daily_average(appConfig, cwClient, 'VolumeWriteIOPs', 'Sum', thisCluster['DBClusterIdentifier'])
totStorageBytes = get_cw_metric_daily_average(appConfig, cwClient, 'VolumeBytesUsed', 'Maximum', thisCluster['DBClusterIdentifier'])
totBackupStorageBilledBytes = get_cw_metric_daily_average(appConfig, cwClient, 'TotalBackupStorageBilled', 'Maximum', thisCluster['DBClusterIdentifier'])
totCPUCredits = get_cw_metric_daily_average(appConfig, cwClient, 'CPUSurplusCreditsCharged', 'Sum', thisCluster['DBClusterIdentifier'])
totIopsMonth = (avgReadIopsMonth * 30) + (avgWriteIopsMonth * 30)
# estimated CPU credits
thisCPUCreditCost = round(totCPUCredits * float(pricingDict['cpu-credits|'+appConfig['region']]['price']) / 60 * 30,0)
thisMonthlyStandardIoCompute += thisCPUCreditCost
thisMonthlyOptimizedIoCompute += thisCPUCreditCost
monthlyStandard += thisCPUCreditCost
monthlyIoOptimized += thisCPUCreditCost
# estimated io cost
thisStandardIopsCost = round(totIopsMonth * float(pricingDict['io|'+appConfig['region']+'|standard']['price']),0)
thisOptimizedIopsCost = round(totIopsMonth * float(pricingDict['io|'+appConfig['region']+'|iopt1']['price']),0)
monthlyStandard += thisStandardIopsCost
monthlyIoOptimized += thisOptimizedIopsCost
# estimated storage cost
thisStandardStorageCost = round(totStorageBytes * float(pricingDict['storage|'+appConfig['region']+'|standard']['price']) / gbBytes,0)
thisOptimizedStorageCost = round(totStorageBytes * float(pricingDict['storage|'+appConfig['region']+'|iopt1']['price']) / gbBytes,0)
monthlyStandard += thisStandardStorageCost
monthlyIoOptimized += thisOptimizedStorageCost
# estimated backup cost
thisBackupCost = round(totStorageBytes * float(pricingDict['storage-snapshot|'+appConfig['region']]['price']) / gbBytes,0)
monthlyStandard += thisBackupCost
monthlyIoOptimized += thisBackupCost
print(" compute = ${:10,.0f} | compute = ${:10,.0f}".format(thisMonthlyStandardIoCompute,thisMonthlyOptimizedIoCompute))
print(" io = ${:10,.0f} | io = ${:10,.0f}".format(thisStandardIopsCost,thisOptimizedIopsCost))
print(" storage = ${:10,.0f} | storage = ${:10,.0f}".format(thisStandardStorageCost,thisOptimizedStorageCost))
print(" backup storage = ${:10,.0f} | backup storage = ${:10,.0f}".format(thisBackupCost,thisBackupCost))
print(" ESTIMATED monthly total = ${:10,.0f} | Estimated monthly total = ${:10,.0f}".format(monthlyStandard,monthlyIoOptimized))
recommendationString = ""
estimatedMonthlySavings = 0.00
if (ioType == "standard") and (monthlyIoOptimized < monthlyStandard) and (engineVersionMajor < 5):
estimatedMonthlySavings = monthlyStandard-monthlyIoOptimized
recommendationString = " **** recommendation - consider switching to IO optimized to potentially save ${:.0f} per month but requires upgrading to DocumentDB v5+".format(estimatedMonthlySavings)
print("")
print(recommendationString)
elif (ioType == "standard") and (monthlyIoOptimized < monthlyStandard):
estimatedMonthlySavings = monthlyStandard-monthlyIoOptimized
recommendationString = " **** recommendation - consider switching to IO optimized to potentially save ${:.0f} per month".format(estimatedMonthlySavings)
print("")
print(recommendationString)
elif (ioType != "standard") and (monthlyStandard < monthlyIoOptimized):
estimatedMonthlySavings = monthlyIoOptimized-monthlyStandard
recommendationString = " **** recommendation - consider switching to standard IO to potentially save ${:.0f} per month".format(estimatedMonthlySavings)
print("")
print(recommendationString)
else:
estimatedMonthlySavings = 0.00
if (ioType == "standard"):
ioTypeText = "standard"
else:
ioTypeText = "io optimized"
recommendationString = " **** current {} storage configuration achieves the lowest possible price point".format(ioTypeText)
print("")
print(recommendationString)
printLog("{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}".format(thisCluster['DBClusterIdentifier'],ioType,engineVersionFull,numInstances,
thisMonthlyStandardIoCompute,thisStandardIopsCost,thisStandardStorageCost,thisBackupCost,monthlyStandard,
thisMonthlyOptimizedIoCompute,thisOptimizedIopsCost,thisOptimizedStorageCost,thisBackupCost,monthlyIoOptimized,
recommendationString,estimatedMonthlySavings),appConfig)
client.close()
cwClient.close()
def get_pricing(appConfig):
pd = {}
print("retrieving pricing...")
pricingUrl = 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonDocDB/current/index.json'
response = requests.get(pricingUrl)
pricingDict = json.loads(response.text)
# get the terms
terms = {}
for thisTermKey in pricingDict['terms']['OnDemand']:
for thisTerm in pricingDict['terms']['OnDemand'][thisTermKey].values():
# find the price
thisTermSku = thisTerm['sku']
thisTermPrice = list(thisTerm['priceDimensions'].values())[0]['pricePerUnit']['USD']
terms[thisTermSku] = thisTermPrice
# get the pricing
for thisProductKey in pricingDict['products']:
thisProduct = pricingDict['products'][thisProductKey]
if 'productFamily' not in thisProduct and thisProduct['attributes']['group'] == 'Global Cluster I/O Operation':
# Global Cluster IO cost
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
thisIoType = 'standard'
thisPrice = terms[thisSku]
thisPricingDictKey = "{}|{}|{}".format('io',thisRegion,thisIoType)
pd[thisPricingDictKey] = {'type':'global-cluster-io','region':thisRegion,'ioType':thisIoType,'price':thisPrice}
# no charge for IO iopt1
thisIoType = 'iopt1'
thisPrice = 0.00
thisPricingDictKey = "{}|{}|{}".format('io',thisRegion,thisIoType)
pd[thisPricingDictKey] = {'type':'global-cluster-io','region':thisRegion,'ioType':thisIoType,'price':thisPrice}
elif 'productFamily' not in thisProduct:
print("*** missing productFamily *** | {}".format(thisProduct))
sys.exit(1)
elif (thisProduct["productFamily"] == "System Operation"):
# IO cost
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
thisIoType = 'standard'
thisPrice = terms[thisSku]
thisPricingDictKey = "{}|{}|{}".format('io',thisRegion,thisIoType)
pd[thisPricingDictKey] = {'type':'io','region':thisRegion,'ioType':thisIoType,'price':thisPrice}
# no charge for IO iopt1
thisIoType = 'iopt1'
thisPrice = 0.00
thisPricingDictKey = "{}|{}|{}".format('io',thisRegion,thisIoType)
pd[thisPricingDictKey] = {'type':'io','region':thisRegion,'ioType':thisIoType,'price':thisPrice}
elif (thisProduct["productFamily"] == "Database Instance"):
# Database Instance
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
thisInstanceType = thisProduct["attributes"]["instanceType"]
thisPrice = terms[thisSku]
if thisProduct["attributes"]["volumeType"] in ["IO-Optimized-DocDB","NVMe SSD IO-Optimized"]:
volumeType = 'iopt1'
elif thisProduct["attributes"]["volumeType"] in ["General Purpose","NVMe SSD"]:
volumeType = 'standard'
else:
print("*** Unknown volumeType {}, exiting".format(thisProduct["attributes"]["volumeType"]))
sys.exit(1)
thisPricingDictKey = "{}|{}|{}|{}".format('compute',thisRegion,thisInstanceType,volumeType)
pd[thisPricingDictKey] = {'type':'compute','region':thisRegion,'instanceType':thisInstanceType,'price':thisPrice,'volumeType':volumeType}
elif (thisProduct["productFamily"] == "Database Storage"):
# Database Storage
# volumeType in ['General Purpose','IO-Optimized-DocDB']
# skip elastic clusters storage
thisStorageUsage = thisProduct["attributes"].get('usagetype','UNKNOWN')
if (thisProduct["attributes"].get('volumeType','UNKNOWN') in ['General Purpose','IO-Optimized-DocDB','NVMe SSD','NVMe SSD IO-Optimized']) and ('StorageUsage' in thisStorageUsage) and ('Elastic' not in thisStorageUsage):
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
if thisProduct["attributes"]["volumeType"] in ["IO-Optimized-DocDB","NVMe SSD IO-Optimized"]:
thisIoType = 'iopt1'
elif thisProduct["attributes"]["volumeType"] in ["General Purpose","NVMe SSD"]:
thisIoType = 'standard'
thisPrice = terms[thisSku]
thisPricingDictKey = "{}|{}|{}".format('storage',thisRegion,thisIoType)
pd[thisPricingDictKey] = {'type':'storage','region':thisRegion,'ioType':thisIoType,'price':thisPrice}
elif thisProduct["attributes"].get('volumeType','UNKNOWN') not in ['General Purpose','IO-Optimized-DocDB','NVMe SSD','NVMe SSD IO-Optimized']:
print("*** Unknown volumeType {}, exiting".format(thisProduct["attributes"].get('volumeType','UNKNOWN')))
sys.exit(1)
elif (thisProduct["productFamily"] == "Storage Snapshot"):
# Storage Snapshot
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
thisPrice = terms[thisSku]
thisPricingDictKey = "{}|{}".format('storage-snapshot',thisRegion)
pd[thisPricingDictKey] = {'type':'storage-snapshot','region':thisRegion,'price':thisPrice}
elif (thisProduct["productFamily"] == "Database Utilization"):
# Database Utilization - EC vCPU pricing
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
thisPrice = terms[thisSku]
thisPricingDictKey = "{}|{}".format('ec-vcpu',thisRegion)
pd[thisPricingDictKey] = {'type':'ec-vcpu','region':thisRegion,'price':thisPrice}
elif (thisProduct["productFamily"] == "CPU Credits"):
# CPU Credits
# using db.t4g.medium for all burstable [conserving cloudwatch calls - cluster only, minor price difference]
if (thisProduct["attributes"]["instanceType"] == 'db.t4g.medium'):
thisSku = thisProduct['sku']
thisRegion = thisProduct["attributes"]["regionCode"]
thisPrice = terms[thisSku]
thisInstanceType = thisProduct["attributes"]["instanceType"]
thisPricingDictKey = "{}|{}".format('cpu-credits',thisRegion)
pd[thisPricingDictKey] = {'type':'cpu-credits','region':thisRegion,'price':thisPrice,'instanceType':thisInstanceType}
else:
print("UNKNOWN - {}".format(thisProduct))
sys.exit(1)
return pd
def main():
parser = argparse.ArgumentParser(description='DocumentDB Deployment Scanner')
parser.add_argument('--region',required=True,type=str,help='AWS Region')
parser.add_argument('--start-date',required=False,type=str,help='Start date for historical usage calculations, format=YYYYMMDD')
parser.add_argument('--end-date',required=False,type=str,help='End date for historical usage calculations, format=YYYYMMDD')
parser.add_argument('--log-file-name',required=True,type=str,help='Log file for CSV output')
args = parser.parse_args()
if (args.start_date is not None and args.end_date is None):
print("Must provide --end-date when providing --start-date, exiting.")
sys.exit(1)
elif (args.start_date is None and args.end_date is not None):
print("Must provide --start-date when providing --end-date, exiting.")
sys.exit(1)
if (args.start_date is None) and (args.end_date is None):
# use last 30 days
startTime = (datetime.datetime.utcnow() - datetime.timedelta(days=30)).strftime("%Y-%m-%dT00:00:00")
endTime = (datetime.datetime.utcnow() - datetime.timedelta(days=0)).strftime("%Y-%m-%dT00:00:00")
else:
# use provided start/end dates
startTime = "{}-{}-{}T00:00:00".format(args.start_date[0:4],args.start_date[4:6],args.start_date[6:8])
endTime = "{}-{}-{}T00:00:00".format(args.end_date[0:4],args.end_date[4:6],args.end_date[6:8])
print("collecting CloudWatch data for {} to {}".format(startTime,endTime))
appConfig = {}
appConfig['region'] = args.region
appConfig['logFileName'] = args.log_file_name+'.csv'
appConfig['startTime'] = startTime
appConfig['endTime'] = endTime
deleteLog(appConfig)
pricingDict = get_pricing(appConfig)
clusterList = get_docdb_instance_based_clusters(appConfig,pricingDict)
print("")
print("Created {} with CSV data".format(appConfig['logFileName']))
print("")
if __name__ == "__main__":
main()