1
1
# Standard libs
2
- import collections
3
2
import os
3
+ import sys
4
4
import json
5
5
import logging
6
6
import datetime
16
16
17
17
class AuditLogCollector (ApiConnection .ApiConnection ):
18
18
19
- def __init__ (self , content_types , * args , resume = True , fallback_time = None ,
19
+ def __init__ (self , * args , content_types = None , resume = True , fallback_time = None ,
20
20
file_output = False , output_path = None ,
21
21
graylog_output = False , graylog_address = None , graylog_port = None ,
22
22
azure_oms_output = False , azure_oms_workspace_id = None , azure_oms_shared_key = None ,
@@ -41,38 +41,46 @@ def __init__(self, content_types, *args, resume=True, fallback_time=None,
41
41
self .graylog_output = graylog_output
42
42
self .azure_oms_output = azure_oms_output
43
43
self .output_path = output_path
44
- self .content_types = content_types
44
+ self .content_types = content_types or collections . deque ()
45
45
self ._last_run_times = {}
46
+ self .resume = resume
46
47
if resume :
47
48
self .get_last_run_times ()
48
49
self ._fallback_time = fallback_time or datetime .datetime .now (datetime .timezone .utc ) - datetime .timedelta (days = 1 )
49
50
self ._known_content = {}
50
- if self .azure_oms_output :
51
- self ._azure_oms_interface = AzureOMSInterface .AzureOMSInterface (workspace_id = azure_oms_workspace_id ,
52
- shared_key = azure_oms_shared_key )
53
- if self .graylog_output :
54
- self ._graylog_interface = GraylogInterface .GraylogInterface (graylog_address = graylog_address ,
55
- graylog_port = graylog_port )
51
+ self ._azure_oms_interface = AzureOMSInterface .AzureOMSInterface (workspace_id = azure_oms_workspace_id ,
52
+ shared_key = azure_oms_shared_key )
53
+ self ._graylog_interface = GraylogInterface .GraylogInterface (graylog_address = graylog_address ,
54
+ graylog_port = graylog_port )
56
55
self .blobs_to_collect = collections .defaultdict (collections .deque )
57
56
self .monitor_thread = threading .Thread ()
58
57
self .retrieve_available_content_threads = collections .deque ()
59
58
self .retrieve_content_threads = collections .deque ()
59
+ self .run_started = None
60
60
self .logs_retrieved = 0
61
61
62
62
def run_once (self , start_time = None ):
63
63
"""
64
64
Check available content and retrieve it, then exit.
65
65
"""
66
- run_started = datetime .datetime .now ()
66
+ self ._known_content .clear ()
67
+ self .logs_retrieved = 0
68
+ self ._graylog_interface .successfully_sent = 0
69
+ self ._graylog_interface .unsuccessfully_sent = 0
70
+ self ._azure_oms_interface .successfully_sent = 0
71
+ self ._azure_oms_interface .unsuccessfully_sent = 0
72
+ self .run_started = datetime .datetime .now ()
67
73
self ._clean_known_content ()
74
+ if self .resume :
75
+ self .get_last_run_times ()
68
76
self .start_monitoring ()
69
77
self .get_all_available_content (start_time = start_time )
70
78
self .monitor_thread .join ()
71
- if self ._last_run_times :
79
+ if self .resume and self . _last_run_times :
72
80
with open ('last_run_times' , 'w' ) as ofile :
73
81
json .dump (fp = ofile , obj = self ._last_run_times )
74
82
logging .info ("Finished. Total logs retrieved: {}. Run time: {}." .format (
75
- self .logs_retrieved , datetime .datetime .now () - run_started ))
83
+ self .logs_retrieved , datetime .datetime .now () - self . run_started ))
76
84
if self .azure_oms_output :
77
85
logging .info ("Azure OMS output report: {} successfully sent, {} errors" .format (
78
86
self ._azure_oms_interface .successfully_sent , self ._azure_oms_interface .unsuccessfully_sent ))
@@ -127,7 +135,7 @@ def get_all_available_content(self, start_time=None):
127
135
"""
128
136
for content_type in self .content_types .copy ():
129
137
if not start_time :
130
- if content_type in self ._last_run_times .keys ():
138
+ if self . resume and content_type in self ._last_run_times .keys ():
131
139
start_time = self ._last_run_times [content_type ]
132
140
else :
133
141
start_time = self ._fallback_time
@@ -336,8 +344,12 @@ def known_content(self):
336
344
elif argsdict ['time_hours' ]:
337
345
fallback_time = datetime .datetime .now (datetime .timezone .utc ) - datetime .timedelta (days = argsdict ['time_hours' ])
338
346
339
- logging .basicConfig (filemode = 'w' , filename = argsdict ['log_path' ],
340
- level = logging .INFO if not argsdict ['debug_logging' ] else logging .DEBUG )
347
+ logger = logging .getLogger ()
348
+ fileHandler = logging .FileHandler (argsdict ['log_path' ], mode = 'w' )
349
+ streamHandler = logging .StreamHandler (sys .stdout )
350
+ logger .addHandler (streamHandler )
351
+ logger .addHandler (fileHandler )
352
+ logger .setLevel (logging .INFO if not argsdict ['debug_logging' ] else logging .DEBUG )
341
353
logging .log (level = logging .INFO , msg = 'Starting run @ {0}' .format (datetime .datetime .now ()))
342
354
343
355
collector = AuditLogCollector (
0 commit comments