forked from reka193/froud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch.py
124 lines (93 loc) · 3.88 KB
/
cloudwatch.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
import datetime
import re
import os
import sys
from prettytable import PrettyTable
from botocore.exceptions import EndpointConnectionError
from botocore.exceptions import ClientError
import common
def list_and_save(logs_client, start_time, stop_time):
groups = []
try:
groups = logs_client.describe_log_groups()['logGroups']
except EndpointConnectionError as error:
print('Error: {}'.format(error))
sys.exit()
except ClientError as error:
common.exception(error, 'Describe log groups failed.')
values = []
filenames = []
for group in groups:
group_name = group['logGroupName']
streams = logs_client.describe_log_streams(logGroupName=group_name)['logStreams']
for stream in streams:
stream_name = stream['logStreamName']
values.append(str(group_name))
log_events = logs_client.get_log_events(logGroupName=group_name, logStreamName=stream_name,
startTime=start_time, endTime=stop_time)
events = log_events['events']
groupname = re.sub('[^\w\s-]', '', group_name)
streamname = re.sub('[^\w\s-]', '', stream_name)
gr_st = groupname + '--' + streamname
filenames += write_events(events, gr_st)
print('Files downloaded to $currentpath/cw_logs folder.')
values = set(values)
return filenames, values
def write_events(events, gr_st):
filenames = []
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'cw_logs')
if not os.path.exists(final_directory):
os.makedirs(final_directory)
file_name = ""
try:
message = ''
for event in events:
if event['message']:
message = message + event['message'] + '\n'
if message:
file_name = final_directory + '/' + gr_st + '.txt'
filenames.append(file_name)
with open(file_name, 'w+') as f:
f.write(message)
except Exception as e:
print('File is skipped: {}, due to: {}'.format(file_name, e))
return filenames
def print_table(values):
nums = range(len(values))
nums = [x + 1 for x in nums]
values_to_print = [list(a) for a in zip(nums, values)]
values_to_print.sort()
x = PrettyTable()
x.field_names = ["No.", "Groups"]
x.align["Groups"] = "l"
for value in values_to_print:
x.add_row(value)
print('\nAvailable Cloudwatch logs: \n')
print(x)
def main():
description = '\n[*] Cloudwatch log scanner.\n'
'[*] The results will be saved to $currentpath/cw_logs folder.\n'
'[*] The logs are read for a specified number of hours until the current time. Default value: 24 hours.\n'
'[*] If a bucket is provided, the results are uploaded to the bucket. \n\n'
optional_params = [['-b', '--bucketName', 'Specify the name of the bucket.'],
['-t', '--time', 'Specify the number of hours to read the logs '
'until the current time. Default value: 24 hours.']]
args, logs_client, s3_client = common.init(description, 'logs', optional_params)
if args['time']:
hours_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=int(args['time']))
else:
hours_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=24)
start_time = int(hours_ago.strftime("%s")) * 1000
stop_time = int(datetime.datetime.utcnow().strftime("%s")) * 1000
print('Collecting CloudWatch logs...')
filenames, values = list_and_save(logs_client, start_time, stop_time)
print_table(values)
if args['bucketName']:
bucket_name = args['bucketName']
if filenames:
common.upload_files(s3_client, filenames, bucket_name)
else:
print('There are no files to upload.')
if __name__ == '__main__':
main()