This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
check_dynamodb
executable file
·261 lines (230 loc) · 11.1 KB
/
check_dynamodb
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 Carles Amigó <[email protected]>
#
# Distributed under terms of the MIT license.
"""
Nagios plugin to check DynamoDB consumed capacity
"""
from __future__ import print_function
import sys
import argparse
import boto.ec2.cloudwatch
import datetime
import pandas as pd
NAGIOS_STATUSES = {
'OK': 0,
'WARNING': 1,
'CRITICAL': 2,
'UNKNOWN': 3
}
def main():
CAPACITY_METRIC = {'read':
{'consumed': 'ConsumedReadCapacityUnits',
'provisioned': 'ProvisionedReadCapacityUnits',
},
'write':
{'consumed': 'ConsumedWriteCapacityUnits',
'provisioned': 'ProvisionedWriteCapacityUnits',
},
'read_index':
{'consumed': 'ConsumedReadCapacityUnits',
'provisioned': 'ProvisionedReadCapacityUnits',
},
'write_index':
{'consumed': 'ConsumedWriteCapacityUnits',
'provisioned': 'ProvisionedWriteCapacityUnits',
},
}
argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument('table',
help='Table to get the metric from.')
argp.add_argument('-R', '--region', default='eu-west-1',
help='The AWS region to read metrics from. Default: \
%(default)s')
argp.add_argument('-w', '--warning', default='70%:25%',
help='Capacity warning threshold. It has two values \
separated by a colon \':\'. The first value \
specifies the warning threshold. It can be \
specified as an integer or as a percentage. The \
second value specifies how many values need to \
be over the threshold to trigger a warning. It \
can also be specified as an integer or as a \
percentage. A warning will be triggered if the \
used capacity is over the specified threshold \
for more than the specified period. \
Default: %(default)s')
argp.add_argument('-c', '--critical', default='85%:25%',
help='Capacity critical threshold. See the warning \
definition for details on how it works. \
Default: %(default)s')
argp.add_argument('-p', '--period', default=60, type=int,
help='The granularity, in seconds, of the returned \
datapoints. period must be at least 60 seconds \
and must be a multiple of 60. Default: \
%(default)s.')
argp.add_argument('-t', '--timedelta', default=60, type=int,
help='Period in minutes to extract the data. Default: \
%(default)s')
argp.add_argument('-C', '--capacity', default='read',
choices=CAPACITY_METRIC.keys(),
help='The capacity metric to evaluate. Default: \
%(default)s')
argp.add_argument('-i', '--index',
help='If index capacity is evaluated, the index name \
needs to be specified')
argp.add_argument('-d', '--debug', action='store_true',
help='Enable debug mode (print extra data)')
args = argp.parse_args()
# Check parameters
if args.capacity not in CAPACITY_METRIC.keys():
argp.error('Capacity not valid')
if args.period < 60 or args.period % 60 != 0:
argp.error('Period must be at least 60 seconds and multiple of 60.')
if args.capacity in ['read_index', 'write_index']:
if not args.index:
argp.error('If capacity is read_index or write_index, an index'
' name needs to be specified.')
if ':' in args.warning and ':' in args.critical:
try:
values = args.warning.replace('%', '').split(':') + \
args.critical.replace('%', '').split(':')
[int(x) for x in values]
except ValueError:
argp.error('Warning and Critical parameters need to be numbers'
' representing a percentage or a fixed value.')
else:
argp.error('Warning and Critical parameters should be two values '
'separated by the colon character: \':\'')
# Get data from Cloudwatch
end_time = datetime.datetime.utcnow()
start_time = end_time - datetime.timedelta(minutes=args.timedelta)
cw = boto.ec2.cloudwatch.connect_to_region(args.region)
cw_dimensions = {'TableName': [args.table]}
if args.capacity in ['read_index', 'write_index']:
cw_dimensions['GlobalSecondaryIndexName'] = [args.index]
result_provisioned = cw.get_metric_statistics(period=args.period,
start_time=start_time,
end_time=end_time,
metric_name=CAPACITY_METRIC[
args.capacity][
'provisioned'],
namespace='AWS/DynamoDB',
statistics=['Sum'],
dimensions=cw_dimensions,
unit='Count',
)
result_consumed = cw.get_metric_statistics(period=args.period,
start_time=start_time,
end_time=end_time,
metric_name=CAPACITY_METRIC[
args.capacity]['consumed'],
namespace='AWS/DynamoDB',
statistics=['Sum'],
dimensions=cw_dimensions,
unit='Count',
)
if len(result_provisioned) == 0:
status = 'UNKNOWN'
print(status + ': Could not get table capacities. Is the table name '
'correct?')
sys.exit(NAGIOS_STATUSES[status])
values_provisioned = []
for n in result_provisioned:
values_provisioned.append({'provisioned': n['Sum'],
'date': n['Timestamp']})
df_provisioned = pd.DataFrame(values_provisioned).set_index('date')
values_consumed = []
for n in result_consumed:
values_consumed.append({'consumed': n['Sum']/args.period,
'date': n['Timestamp']})
if len(result_consumed) == 0:
values_consumed.append({'consumed': 0,
'date': df_provisioned.head(1).index[0]})
values_consumed.append({'consumed': 0,
'date': df_provisioned.tail(1).index[0]})
df_consumed = pd.DataFrame(values_consumed).set_index('date')
df = pd.concat([df_consumed, df_provisioned], axis=1, join='outer')
first_date = df.sort_index(0).index[0]
df = df.reindex(pd.date_range(first_date,
periods=args.timedelta/(args.period/60),
freq=pd.DateOffset(
minutes=args.period/60))).interpolate(
limit=args.timedelta/(
args.period/60))
if args.debug:
print("Data collected:")
print(df)
# set default output
msg = ''
status = 'OK'
datapoints_exceeded = 0
total_datapoints = len(df)
# Warning
warning_0 = int(args.warning.replace('%', '').split(':')[0])
warning_1 = int(args.warning.replace('%', '').split(':')[1])
if '%' in args.warning.split(':')[0]:
# first argument value is percentage
len_w = ((100 - (df.provisioned - df.consumed) / df.provisioned * 100)
> warning_0).value_counts().get(True, 0)
else:
# first argument value is fixed value
len_w = (df.provisioned - df.consumed
> warning_0).value_counts().get(True, 0)
if '%' in args.warning.split(':')[1]:
# second argument value is percentage
if (float(len_w) / float(total_datapoints) * 100) > warning_1:
status = 'WARNING'
datapoints_exceeded = len_w
threshold = args.warning.split(':')[0]
else:
# second argument value is fixed value
if len_w > warning_1:
status = 'WARNING'
datapoints_exceeded = len_w
threshold = args.warning.split(':')[0]
if status == 'OK':
msg = 'Table ' + args.table + ' ' + args.capacity + ' capacity is ' + \
'under the specified thresholds'
else:
msg = 'Table ' + args.table + ' ' + args.capacity + ' capacity ' + \
'has exceeded the threshold of ' + \
threshold + ' for a sum of ' + str(datapoints_exceeded) + \
' datapoints from a total of ' + str(total_datapoints)
# Critical
critical_0 = int(args.critical.replace('%', '').split(':')[0])
critical_1 = int(args.critical.replace('%', '').split(':')[1])
if '%' in args.critical.split(':')[0]:
# first argument value is percentage
len_c = ((100 - (df.provisioned - df.consumed) / df.provisioned * 100)
> critical_0).value_counts().get(True, 0)
else:
# first argument value is fixed value
len_c = (df.provisioned - df.consumed
> critical_0).value_counts().get(True, 0)
if '%' in args.critical.split(':')[1]:
# second argument value is percentage
if (float(len_c) / float(total_datapoints) * 100) > critical_1:
status = 'CRITICAL'
datapoints_exceeded = len_c
threshold = args.critical.split(':')[0]
else:
# second argument value is fixed value
if len_c > critical_1:
status = 'CRITICAL'
datapoints_exceeded = len_c
threshold = args.critical.split(':')[0]
if status == 'OK':
msg = 'Table ' + args.table + ' ' + args.capacity + ' capacity is ' + \
'under the specified thresholds'
else:
msg = 'Table ' + args.table + ' ' + args.capacity + ' capacity ' + \
'has exceeded the threshold of ' + \
threshold + ' for a sum of ' + str(datapoints_exceeded) + \
' datapoints from a total of ' + str(total_datapoints)
print(status + ': ' + msg)
sys.exit(NAGIOS_STATUSES[status])
if __name__ == "__main__":
main()