-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_log_groups_by_size.py
58 lines (47 loc) · 1.47 KB
/
aws_log_groups_by_size.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
# pip install nose --user
# pip install tornado --user
# pip install boto3 --user
# pip install prettytable --user
import boto3
import json
from hurry.filesize import alternative
from prettytable import PrettyTable
# get log groups
client = boto3.client('logs')
log_groups = [] #client.describe_log_groups()
# create logs list
logs = []
paginator = client.get_paginator('describe_log_groups')
iterator = paginator.paginate(PaginationConfig={'MaxItems': 10000})
for page in iterator:
for log_group in page['logGroups']:
# add logs to list
if log_group['storedBytes'] > 1000000: # 1mb
logs.append({
'log_group_name': log_group['logGroupName'],
'retention_in_days': log_group.get('retentionInDays', 'Never Expire'),
'stored_bytes': log_group['storedBytes']
})
# sorting function
def sortBySize(e):
return e['stored_bytes']
# human readable size function
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
def humansize(nbytes):
i = 0
while nbytes >= 1024 and i < len(suffixes)-1:
nbytes /= 1024.
i += 1
f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
return '%s %s' % (f, suffixes[i])
# sort logs
logs.sort(reverse=True, key=sortBySize)
# add table headers
t = PrettyTable(['log_group_name', 'retention_in_days', 'stored_bytes'])
t.align['log_group_name'] = 'l'
t.align['stored_bytes'] = 'r'
# populate table
for x in logs:
t.add_row([x['log_group_name'], x['retention_in_days'], humansize(x['stored_bytes'])])
# print pretty table
print(t)