The following Python script will help you to export list of any all the CloudWatch logs with their retention time and size of the log group an AWS account. The script also helps you to save the output within CSV file.
#!/usr/bin/env python3.8
#This script will save the list of CloudWatch log group names, their retention time and
# size (in MBs) in to CSV file
import boto3
import time
import csv
# Setting up AWS profile and region
profile = "default"
region = "us-east-1"
session = boto3.Session(profile_name=profile, region_name=region)
#getting current date and time
date_time = time.strftime("%d%m%Y_%H%M")
# setting up file name
fname = "CloudWatch-Logs_"
ftime = date_time
fext = ".csv"
filename = fname + ftime + fext
# open file to write output
with open(filename,'w') as f:
print('Profile, Region,Log Group Name, Retention Time (in days), Size (in MB)', file=f)
cw_logs = session.client('logs')
paginator = cw_logs.get_paginator('describe_log_groups')
iterator = paginator.paginate()
for page in iterator:
for log_group in page['logGroups']:
log_group_name = log_group['logGroupName']
log_size = float((log_group['storedBytes'])/1000000) #converts bytes into MBs
try:
retention_Time = log_group['retentionInDays']
if type(retention_Time) == int:
print(profile[1]["name"],',', region,',', log_group_name,',', retention_Time,',', log_size, file=f)
except Exception as err:
print(profile[1]["name"],',', region, log_group_name, 'Never Expire,', log_size, file=f)
continue
Hope you find it useful.
Disclaimer: www.TechieTalks.co.uk does not conceal the possibility of error and shortcomings due to human or technical factors. www.TechieTalks.co.uk does not bear responsibility upon any loss or damage arising from conduct or activities related to the use of data and information contained in this blog.
Comments
Post a Comment