forked from jros2300/redshiftMonitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
308 lines (284 loc) · 9.78 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
from __future__ import print_function
import json
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'
##################
print('Loading function')
kms = boto3.client('kms')
password = kms.decrypt(CiphertextBlob=base64.b64decode(enc_password))['Plaintext']
cw = boto3.client('cloudwatch')
pg8000.paramstyle = "qmark"
def lambda_handler(event, context):
conn = pg8000.connect(database=database, user=user, password=password, host=host, port=port, ssl=ssl)
cursor = conn.cursor()
cursor.execute("select \"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
cursor.execute("SELECT 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
cursor.execute("SELECT 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
cursor.execute("SELECT 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
cursor.execute("SELECT 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 '%s'" % interval)
total_disk_based_queries = cursor.fetchone()[0]
if total_disk_based_queries == None:
total_disk_based_queries = 0
cursor.execute("select 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
cursor.execute("select 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
cursor.execute("select 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
cursor.execute("select sum(packets) from stl_dist where starttime >= GETDATE() - INTERVAL '%s'" % interval)
total_packets = cursor.fetchone()[0]
if total_packets == None:
total_packets = 0
cursor.execute("select 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
cw.put_metric_data(
Namespace = 'Redshift',
MetricData=[
{
'MetricName': 'TablesNotCompressed',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': tables_not_compressed,
'Unit': 'Count'
},
{
'MetricName': 'ColumnsNotCompressed',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': columns_not_compressed,
'Unit': 'Count'
},
{
'MetricName': 'MaxSkewRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': max_skew_ratio,
'Unit': 'None'
},
{
'MetricName': 'AvgSkewRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': avg_skew_ratio,
'Unit': 'None'
},
{
'MetricName': 'Tables',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': number_tables,
'Unit': 'Count'
},
{
'MetricName': 'QueriesScanNoSort',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': queries_scan_no_sort,
'Unit': 'Count'
},
{
'MetricName': 'MaxSkewSortRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': max_skew_sort_ratio,
'Unit': 'None'
},
{
'MetricName': 'AvgSkewSortRatio',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': avg_skew_sort_ratio,
'Unit': 'None'
},
{
'MetricName': 'TablesStatsOff',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': number_tables_statsoff,
'Unit': 'Count'
},
{
'MetricName': 'MaxVarcharSize',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': max_varchar_size,
'Unit': 'None'
},
{
'MetricName': 'TotalWLMQueueTime',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': total_wlm_queue_time,
'Unit': 'Seconds'
},
{
'MetricName': 'DiskBasedQueries',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': total_disk_based_queries,
'Unit': 'Count'
},
{
'MetricName': 'AvgCommitQueueTime',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': avg_commit_queue,
'Unit': 'Milliseconds'
},
{
'MetricName': 'TotalAlerts',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': total_alerts,
'Unit': 'Count'
},
{
'MetricName': 'MaxUnsorted',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': max_unsorted_pct,
'Unit': 'Percent'
},
{
'MetricName': 'Rows',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': total_rows,
'Unit': 'Count'
},
{
'MetricName': 'AverageQueryTime',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': avg_query_time,
'Unit': 'Milliseconds'
},
{
'MetricName': 'Packets',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': total_packets,
'Unit': 'Count'
},
{
'MetricName': 'QueriesWithHighTraffic',
'Dimensions': [
{ 'Name': 'ClusterIdentifier', 'Value': cluster}
],
'Timestamp': datetime.datetime.now(),
'Value': queries_traffic,
'Unit': 'Count'
}
]
)
cursor.close()
conn.close()
return 'Finished'