Repository navigation
Expand file tree
/
Copy pathdocdb-inventory.py
More file actions
128 lines (101 loc) · 6.35 KB
/
Copy pathdocdb-inventory.py
File metadata and controls
128 lines (101 loc) · 6.35 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
#!/usr/bin/env python3
import boto3
from botocore.config import Config
import datetime
import argparse
import requests
import json
import sys
import os
def report_clusters(appConfig):
print("Gathering cluster details.")
botoConfig = Config(retries={'max_attempts': 10,'mode': 'standard'})
maxClusterNameLength = 0
maxClusterStatusLength = 0
if appConfig['endpointUrl'] == 'NONE':
client = boto3.client('docdb',region_name=appConfig['region'],config=botoConfig)
else:
client = boto3.client('docdb',region_name=appConfig['region'],endpoint_url=appConfig['endpointUrl'],config=botoConfig)
response = client.describe_db_clusters(Filters=[{'Name': 'engine','Values': ['docdb']}])
clusterArr = {}
for thisCluster in response['DBClusters']:
thisDBClusterIdentifier = thisCluster['DBClusterIdentifier']
includeThisCluster = False
if appConfig['filterString'] == []:
includeThisCluster = True
else:
for thisSearchString in appConfig['filterString']:
if thisSearchString.casefold() in thisDBClusterIdentifier:
includeThisCluster = True
if includeThisCluster:
thisClusterDict = {}
thisClusterDict['ioType'] = thisCluster.get('StorageType','standard')
thisClusterDict['parameterGroup'] = thisCluster.get('DBClusterParameterGroup','***MISSING***')
thisClusterDict['engineVersionFull'] = thisCluster['EngineVersion']
thisClusterDict['engineVersionMajor'] = int(thisClusterDict['engineVersionFull'].split('.')[0])
thisClusterDict['status'] = thisCluster['Status']
thisClusterDict['fullPayload'] = thisCluster
maxClusterStatusLength = max(len(thisCluster['Status']),maxClusterStatusLength)
maxClusterNameLength = max(len(thisCluster['DBClusterIdentifier']),maxClusterNameLength)
thisClusterInstancesDict = {}
numInstances = 0
for thisInstance in thisCluster['DBClusterMembers']:
# get instance type
responseInstance = client.describe_db_instances(DBInstanceIdentifier=thisInstance['DBInstanceIdentifier'])
thisClusterInstanceDict = {}
thisClusterInstanceDict['DBInstanceClass'] = responseInstance['DBInstances'][0]['DBInstanceClass']
thisClusterInstanceDict['IsClusterWriter'] = thisInstance['IsClusterWriter']
thisClusterInstanceDict['fullPayload'] = responseInstance
thisClusterInstancesDict[responseInstance['DBInstances'][0]['DBInstanceIdentifier']] = thisClusterInstanceDict.copy()
numInstances += 1
thisClusterDict['numInstances'] = numInstances
clusterArr[thisCluster['DBClusterIdentifier']] = {'clusterDetails':thisClusterDict.copy(), 'instanceDetails':thisClusterInstancesDict.copy()}
client.close()
clustersFound = False
for thisDBClusterIdentifier in sorted(clusterArr.keys()):
thisCluster = clusterArr[thisDBClusterIdentifier]
clustersFound = True
if appConfig['compact']:
print("{0:<{w1}} | IO = {1} | ver = {2} | inst = {3:d} | stat = {4:<{w2}} | pg = {6} | endpt = {5}".format(thisDBClusterIdentifier,thisCluster['clusterDetails']['ioType'],
thisCluster['clusterDetails']['engineVersionFull'],thisCluster['clusterDetails']['numInstances'],thisCluster['clusterDetails']['status'],
thisCluster['clusterDetails']['fullPayload'].get('Endpoint','<<missing>>'),thisCluster['clusterDetails']['parameterGroup'],w1=maxClusterNameLength,w2=maxClusterStatusLength))
else:
print("")
print("cluster = {} | IO = {} | ver = {} | inst = {:d} | stat = {} | pg = {} | endpt = {} | arn = {}".format(thisDBClusterIdentifier,thisCluster['clusterDetails']['ioType'],
thisCluster['clusterDetails']['engineVersionFull'],thisCluster['clusterDetails']['numInstances'],thisCluster['clusterDetails']['status'],thisCluster['clusterDetails']['parameterGroup'],
thisCluster['clusterDetails']['fullPayload'].get('Endpoint','<<missing>>'),thisCluster['clusterDetails']['fullPayload']['DBClusterArn']))
for DBInstanceIdentifier in sorted(thisCluster['instanceDetails'].keys()):
thisInstance = thisCluster['instanceDetails'][DBInstanceIdentifier]
if thisInstance['IsClusterWriter']:
instanceRole = 'writer'
else:
instanceRole = 'reader'
print(" inst = {} | type = {} | role = {} | az = {} | status = {} | arn = {}".format(DBInstanceIdentifier,thisInstance['DBInstanceClass'],instanceRole,thisInstance['fullPayload']['DBInstances'][0].get('AvailabilityZone','UNKNOWN'),
thisInstance['fullPayload']['DBInstances'][0]['DBInstanceStatus'],thisInstance['fullPayload']['DBInstances'][0]['DBInstanceArn']))
if appConfig['verbose']:
print("{}".format(json.dumps(thisCluster,sort_keys=True,indent=4,default=str)))
if not clustersFound:
if (appConfig['filterString'] == 'NONENONENONE'):
print(" no clusters found")
else:
print(" no clusters found for filter {}".format(appConfig['filterString']))
def main():
parser = argparse.ArgumentParser(description='DocumentDB Deployment Scanner')
parser.add_argument('--region',required=True,type=str,help='AWS Region')
parser.add_argument('--endpoint-url',required=False,type=str,default='NONE',help='Endpoint URL')
parser.add_argument('--filter-string',required=False,type=str,default='NONENONENONE',help='Only display clusters containing given string(s), comma separated')
parser.add_argument('--verbose',required=False,action="store_true",help='Enable verbose output')
parser.add_argument('--compact',required=False,action="store_true",help='Enable compact output')
args = parser.parse_args()
appConfig = {}
appConfig['region'] = args.region
appConfig['endpointUrl'] = args.endpoint_url
if args.filter_string == "NONENONENONE":
appConfig['filterString'] = []
else:
appConfig['filterString'] = args.filter_string.split(",")
appConfig['verbose'] = args.verbose
appConfig['compact'] = args.compact
report_clusters(appConfig)
if __name__ == "__main__":
main()