forked from awslabs/amazon-redshift-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
executable file
·356 lines (324 loc) · 14.5 KB
/
lambda_function.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
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
349
350
351
352
353
354
355
356
#!/usr/bin/env python
from __future__ import print_function
# Copyright 2016-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
# http://aws.amazon.com/apache2.0/
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
import sys
import boto3
import base64
import pg8000
import datetime
#### Configuration
user = 'dbuser'
enc_password = 'CiC5vxxxxxNg=='
host = 'endpoint'
port = 8192
database = 'dbname'
ssl = True
cluster = 'clustername'
interval = '1 hour'
debug = True
##################
try:
kms = boto3.client('kms')
password = kms.decrypt(CiphertextBlob=base64.b64decode(enc_password))['Plaintext']
except:
print('KMS access failed: exception %s' % sys.exc_info()[1])
cw = boto3.client('cloudwatch')
pg8000.paramstyle = "qmark"
def run_command(cursor, statement):
if debug:
print("Running Statement: %s" % statement)
return cursor.execute(statement)
def lambda_handler(event, context):
try:
if debug:
print('Connect to Redshift: %s' % host)
conn = pg8000.connect(database=database, user=user, password=password, host=host, port=port, ssl=ssl)
except:
print('Redshift Connection Failed: exception %s' % sys.exc_info()[1])
return 'Failed'
if debug:
print('Succesfully Connected Redshift Cluster')
cursor = conn.cursor()
run_command(cursor,"select /* Lambda CloudWatch Exporter */ \"schema\" || '.' || \"table\" as table, encoded, max_varchar, unsorted, stats_off, tbl_rows, skew_sortkey1, skew_rows from svv_table_info")
tables_not_compressed = 0
max_skew_ratio = 0
total_skew_ratio = 0
number_tables_skew = 0
number_tables = 0
max_skew_sort_ratio = 0
total_skew_sort_ratio = 0
number_tables_skew_sort = 0
number_tables_statsoff = 0
max_varchar_size = 0
max_unsorted_pct = 0
total_rows = 0
result = cursor.fetchall()
for table in result:
table_name, encoded, max_varchar, unsorted, stats_off, tbl_rows, skew_sortkey1, skew_rows = table
number_tables += 1
if encoded == 'N':
tables_not_compressed += 1
if skew_rows != None:
if skew_rows > max_skew_ratio:
max_skew_ratio = skew_rows
total_skew_ratio += skew_rows
number_tables_skew += 1
if skew_sortkey1 != None:
if skew_sortkey1 > max_skew_sort_ratio:
max_skew_sort_ratio = skew_sortkey1
total_skew_sort_ratio += skew_sortkey1
number_tables_skew_sort += 1
if stats_off != None and stats_off > 5:
number_tables_statsoff += 1
if max_varchar != None and max_varchar > max_varchar_size:
max_varchar_size = max_varchar
if unsorted != None and unsorted > max_unsorted_pct:
max_unsorted_pct = unsorted
if tbl_rows != None:
total_rows += tbl_rows
if number_tables_skew > 0:
avg_skew_ratio = total_skew_ratio / number_tables_skew
else:
avg_skew_ratio = 0
if number_tables_skew_sort > 0:
avg_skew_sort_ratio = total_skew_sort_ratio / number_tables_skew_sort
else:
avg_skew_sort_ratio = 0
run_command(cursor,"SELECT /* Lambda CloudWatch Exporter */ count(a.attname) FROM pg_namespace n, pg_class c, pg_attribute a WHERE n.oid = c.relnamespace AND c.oid = a.attrelid AND a.attnum > 0 AND NOT a.attisdropped and n.nspname NOT IN ('information_schema','pg_catalog','pg_toast') AND format_encoding(a.attencodingtype::integer) = 'none' AND c.relkind='r' AND a.attsortkeyord != 1")
columns_not_compressed = cursor.fetchone()[0]
if columns_not_compressed == None:
columns_not_compressed = 0
run_command(cursor,"SELECT /* Lambda CloudWatch Exporter */ sum(nvl(s.num_qs,0)) FROM svv_table_info t LEFT JOIN (SELECT tbl, COUNT(distinct query) num_qs FROM stl_scan s WHERE s.userid > 1 AND starttime >= GETDATE() - INTERVAL '%s' GROUP BY tbl) s ON s.tbl = t.table_id WHERE t.sortkey1 IS NULL" % interval)
queries_scan_no_sort = cursor.fetchone()[0]
if queries_scan_no_sort == None:
queries_scan_no_sort = 0
run_command(cursor,"SELECT /* Lambda CloudWatch Exporter */ SUM(w.total_queue_time) / 1000000.0 FROM stl_wlm_query w WHERE w.queue_start_time >= GETDATE() - INTERVAL '%s' AND w.total_queue_time > 0" % interval)
total_wlm_queue_time = cursor.fetchone()[0]
if total_wlm_queue_time == None:
total_wlm_queue_time = 0
run_command(cursor,"SELECT /* Lambda CloudWatch Exporter */ count(distinct query) FROM svl_query_report WHERE is_diskbased='t' AND (LABEL LIKE 'hash%%' OR LABEL LIKE 'sort%%' OR LABEL LIKE 'aggr%%') AND userid > 1 AND start_time >= GETDATE() - INTERVAL '{0}'".format(interval))
total_disk_based_queries = cursor.fetchone()[0]
if total_disk_based_queries == None:
total_disk_based_queries = 0
run_command(cursor,"select /* Lambda CloudWatch Exporter */ avg(datediff(ms,startqueue,startwork)) from stl_commit_stats where startqueue >= GETDATE() - INTERVAL '%s'" % interval)
avg_commit_queue = cursor.fetchone()[0]
if avg_commit_queue == None:
avg_commit_queue = 0
run_command(cursor,"select /* Lambda CloudWatch Exporter */ count(distinct l.query) from stl_alert_event_log as l where l.userid >1 and l.event_time >= GETDATE() - INTERVAL '%s'" % interval)
total_alerts = cursor.fetchone()[0]
if total_alerts == None:
total_alerts = 0
run_command(cursor,"select /* Lambda CloudWatch Exporter */ avg(datediff(ms, starttime, endtime)) from stl_query where starttime >= GETDATE() - INTERVAL '%s'" % interval)
avg_query_time = cursor.fetchone()[0]
if avg_query_time == None:
avg_query_time = 0
run_command(cursor,"select /* Lambda CloudWatch Exporter */ sum(packets) from stl_dist where starttime >= GETDATE() - INTERVAL '%s'" % interval)
total_packets = cursor.fetchone()[0]
if total_packets == None:
total_packets = 0
run_command(cursor,"select /* Lambda CloudWatch Exporter */ sum(total) from (select count(query) total from stl_dist where starttime >= GETDATE() - INTERVAL '%s' group by query having sum(packets) > 1000000)" % interval)
queries_traffic = cursor.fetchone()[0]
if queries_traffic == None:
queries_traffic = 0
run_command(cursor,"select /* Lambda CloudWatch Exporter */ count(event) from stl_connection_log where event = 'initiating session' and username != 'rdsdb' and pid not in (select pid from stl_connection_log where event = 'disconnecting session')")
db_connections = cursor.fetchone()[0]
if db_connections == None:
db_connections = 0
if debug:
print("Publishing CloudWatch Metrics")
try:
cw.put_metric_data(
Namespace='Redshift',
MetricData=[
{
'MetricName': 'TablesNotCompressed',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': tables_not_compressed,
'Unit': 'Count'
},
{
'MetricName': 'ColumnsNotCompressed',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': columns_not_compressed,
'Unit': 'Count'
},
{
'MetricName': 'MaxSkewRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': max_skew_ratio,
'Unit': 'None'
},
{
'MetricName': 'AvgSkewRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': avg_skew_ratio,
'Unit': 'None'
},
{
'MetricName': 'Tables',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': number_tables,
'Unit': 'Count'
},
{
'MetricName': 'QueriesScanNoSort',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': queries_scan_no_sort,
'Unit': 'Count'
},
{
'MetricName': 'MaxSkewSortRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': max_skew_sort_ratio,
'Unit': 'None'
},
{
'MetricName': 'AvgSkewSortRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': avg_skew_sort_ratio,
'Unit': 'None'
},
{
'MetricName': 'TablesStatsOff',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': number_tables_statsoff,
'Unit': 'Count'
},
{
'MetricName': 'MaxVarcharSize',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': max_varchar_size,
'Unit': 'None'
},
{
'MetricName': 'TotalWLMQueueTime',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': total_wlm_queue_time,
'Unit': 'Seconds'
},
{
'MetricName': 'DiskBasedQueries',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': total_disk_based_queries,
'Unit': 'Count'
},
{
'MetricName': 'AvgCommitQueueTime',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': avg_commit_queue,
'Unit': 'Milliseconds'
},
{
'MetricName': 'TotalAlerts',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': total_alerts,
'Unit': 'Count'
},
{
'MetricName': 'MaxUnsorted',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': max_unsorted_pct,
'Unit': 'Percent'
},
{
'MetricName': 'Rows',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': total_rows,
'Unit': 'Count'
},
{
'MetricName': 'AverageQueryTime',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': avg_query_time,
'Unit': 'Milliseconds'
},
{
'MetricName': 'Packets',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': total_packets,
'Unit': 'Count'
},
{
'MetricName': 'QueriesWithHighTraffic',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': queries_traffic,
'Unit': 'Count'
},
{
'MetricName': 'DbConnections',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.utcnow(),
'Value': db_connections,
'Unit': 'Count'
}
]
)
except:
print('Pushing metrics to CloudWatch failed: exception %s' % sys.exc_info()[1])
cursor.close()
conn.close()
return 'Finished'
if __name__ == "__main__":
lambda_handler(sys.argv[0], None)