-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
127 lines (106 loc) · 3.04 KB
/
lambda_function.py
File metadata and controls
127 lines (106 loc) · 3.04 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
import boto3
from botocore.exceptions import ClientError
import gzip
import json
import xmltodict
import base64
def lambda_handler(event, context):
#print(f'Logging Event: {event}')
#print(f"Awslog: {event['awslogs']}")
cw_data = event['awslogs']['data']
#print(f'data: {cw_data}')
#print(f'type: {type(cw_data)}')
compressed_payload = base64.b64decode(cw_data)
uncompressed_payload = gzip.decompress(compressed_payload)
payload = json.loads(uncompressed_payload)
log_events = payload['logEvents']
message = []
for log_event in log_events:
doc = xmltodict.parse(log_event['message'])
message.append(json.dumps(doc))
#filtro = [ ]
filtro = ["EntryId", "Sql_Text", "DB_User", "OS_User"]
strTable = "<table>"
for event in message:
a = json.loads(event)
strRW = ''
if not filtro:
for chave in a['AuditRecord']:
header = "<th>"+chave+"</th>"
strTable = strTable+header
rows = "<td>"+a['AuditRecord'][chave]
strRW = strRW+rows
else:
for chave in filtro:
for x in a['AuditRecord']:
if x == chave:
header = "<th>"+chave+"</th>"
strTable = strTable+header
rows = "<td>"+a['AuditRecord'][chave]
strRW = strRW+rows
strRW = "<tr>"+strRW+"</tr>"
strTable = strTable+strRW
strTable = strTable+"</table>"
SENDER = "MONITORAMENTO <monitoramento@yourcompany.com>"
RECIPIENT = "monitoramento@yourcompany.com"
AWS_REGION = "us-east-1"
SUBJECT = "Cloud Watch Events"
BODY_HTML = """<html>
<head>
<style>
table {{
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}}
td, th {{
border: 1px solid #ddd;
padding: 8px;
}}
th {{
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}}
</style>
</head>
<body>
<h1>Eventos Reportados por CLOUDWATCH</h1>
{}
</body>
</html>
""".format(strTable)
CHARSET = "UTF-8"
client = boto3.client('ses',region_name=AWS_REGION)
try:
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
'Text': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])