-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2_logging_config
34 lines (29 loc) · 1.33 KB
/
ec2_logging_config
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
import boto3
import time
import calendar
from datetime import datetime
import json
def lambda_handler(event, context):
#Get all Log Group names in account
client = boto3.client('logs')
response = client.describe_log_groups()
for logGroup in response["logGroups"]:
logGroupName = logGroup["logGroupName"]
# Get LogStreams from LogGroups that start with "PROD"
if logGroupName.startswith( 'PROD' ):
newResponse = client.describe_log_streams(logGroupName=str(logGroupName))
for logStreams in newResponse["logStreams"]:
logStreamName = logStreams["logStreamName"]
# Get timestamp of most recent log
try:
lastTimeStamp = logStreams["lastIngestionTime"]
lastTimeStamp = str(lastTimeStamp)
lastTimeStamp = lastTimeStamp[:-3]
except KeyError:
continue
# Compare current time and time of most recent log stream
current = calendar.timegm(time.gmtime())
timeDiff = int(current) - int(lastTimeStamp)
if timeDiff >= 300 and timeDiff <= 36000:
dt_object = datetime.fromtimestamp(int(lastTimeStamp))
print("The EC2 instance with ID: "+str(logStreamName)+" from logGroup: "+str(logGroupName)+" has not sent logs since: "+str(dt_object))