forked from awslabs/amazon-documentdb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-docdb-dashboard.py
More file actions
129 lines (98 loc) · 4.35 KB
/
Copy pathcreate-docdb-dashboard.py
File metadata and controls
129 lines (98 loc) · 4.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
129
import json
import boto3
import argparse
import widgets as w
# Checking to see if widget metric requires are cluster level or instance level.
# If the metric is instance level, associate all instances for instance level metrics
def add_metric(widJson, widgets, region, instanceList, clusterList):
for widget in widgets:
widget["properties"]['region'] = region
if 'metrics' in widget["properties"]:
if 'DBInstanceIdentifier' in widget["properties"]["metrics"][0]:
for i, DBInstanceIdentifier in enumerate(instanceList):
if DBInstanceIdentifier['IsClusterWriter']:
instanceType = '|PRIMARY'
else:
instanceType = '|REPLICA'
if (i == 0):
widget["properties"]["metrics"][i].append(DBInstanceIdentifier['DBInstanceIdentifier'])
widget["properties"]["metrics"][i].append({"label":DBInstanceIdentifier['DBInstanceIdentifier']+instanceType})
else:
widget["properties"]["metrics"].append([".",".",".",DBInstanceIdentifier['DBInstanceIdentifier'],{"label":DBInstanceIdentifier['DBInstanceIdentifier']+instanceType}])
else:
for i, DBClusterIdentifier in enumerate(clusterList):
if (i == 0):
widget["properties"]["metrics"][i].append(DBClusterIdentifier)
widget["properties"]["metrics"][i].append({"label":DBClusterIdentifier})
else:
widget["properties"]["metrics"].append([".",".",".",DBClusterIdentifier,{"label":DBClusterIdentifier}])
widJson["widgets"].append(widget)
# Main method
def main():
# Command line arguments for user to pass
parser = argparse.ArgumentParser()
parser.add_argument('--name', type=str, required=True, help="Name of CloudWatch dashboard to create")
parser.add_argument('--region', type=str, required=True, help="Region of Amazon DocumentDB cluster(s)")
parser.add_argument('--clusterID', type=str, required=True, help="Single Amazon DocumentDB cluster ID or comma separated list of cluster IDs")
args = parser.parse_args()
# DocumentDB Configurations
docdbclient = boto3.client('docdb', region_name=args.region)
clusterList = args.clusterID.split(',')
instanceList = []
for thisCluster in clusterList:
response = docdbclient.describe_db_clusters(DBClusterIdentifier=thisCluster,Filters=[{'Name': 'engine','Values': ['docdb']}])
for thisInstance in response["DBClusters"][0]["DBClusterMembers"]:
instanceList.append(thisInstance)
#instanceID = response["DBClusters"][0]["DBClusterMembers"]
# CloudWatch client
client = boto3.client('cloudwatch', region_name=args.region)
# All widgets to be displayed on the dashboard
widgets = [
w.ClusterHeading,
w.metricHelp,
w.bestPractices,
w.DBClusterReplicaLagMaximum,
w.DatabaseCursorsTimedOut,
w.VolumeWriteIOPS,
w.VolumeReadIOPS,
w.OpscountersInsert,
w.OpscountersUpdate,
w.OpscountersDelete,
w.OpscountersQuery,
w.InstanceHeading,
w.CPUUtilization,
w.DatabaseConnections,
w.DatabaseCursors,
w.BufferCacheHitRatio,
w.IndexBufferCacheHitRatio,
w.FreeableMemory,
w.NetworkTransmitThroughput,
w.NetworkReceiveThroughput,
w.StorageNetworkTransmitThroughput,
w.StorageNetworkReceiveThroughput,
w.DocsInserted,
w.DocsDeleted,
w.DocsUpdated,
w.DocsReturned,
w.ReadLatency,
w.WriteLatency,
w.DiskQueueDepth,
w.DBInstanceReplicaLag,
w.WriteIops,
w.WriteThroughput,
w.ReadIops,
w.ReadThroughput,
w.BackupStorageHeading,
w.VolumeBytesUsed,
w.BackupRetentionPeriodStorageUsed,
w.TotalBackupStorageBilled
]
# Deploy metrics
add_metric(w.widget_json, widgets, args.region, instanceList, clusterList)
# Converting python to json
dashBody = json.dumps(w.widget_json)
# Create dashboard
client.put_dashboard(DashboardName=args.name, DashboardBody=dashBody)
print("Dashboard {} deployed to CloudWatch".format(args.name))
if __name__ == "__main__":
main()