forked from kravp00L/netflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_cleanup.py
More file actions
executable file
·87 lines (80 loc) · 2.97 KB
/
log_cleanup.py
File metadata and controls
executable file
·87 lines (80 loc) · 2.97 KB
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
'''
################################################################
#
# Python script to manage netflow log and export files
#
################################################################
'''
import datetime
import os
import sys
BASE_DIR = '/opt/netflow'
CONFIG_FILE = 'listener.conf'
def get_config_file():
app_path = os.path.join(BASE_DIR, 'conf')
local_file = os.path.join(app_path, CONFIG_FILE)
if os.path.exists(local_file) and os.path.isfile(local_file):
return local_file
else:
sys.exit(1)
def read_config():
params = dict()
try:
with open(get_config_file(),'r') as f:
for line in f:
if '[global]' in line.strip():
line = f.next()
while len(line.strip()) > 0 and not 'listener' in line.strip():
c_param = (line.strip()).split('=')
params[c_param[0].strip()] = c_param[1].strip()
line = f.next()
except IOError as err:
print ''.join(['Exception: ', str(err)])
except:
e = sys.exc_info()[0]
print ''.join(['Exception in read_config:', str(e)])
return params
def cleanup_export_files(params):
success = False
export_path = params.get('ascii_log_path')
retention_interval = params.get('retention_days')
try:
files = os.listdir(export_path)
for f in files:
if os.path.isfile(''.join([export_path,'/',f])):
file_mod_ts = os.path.getmtime(''.join([export_path,'/',f]))
file_dt = datetime.date.fromtimestamp(file_mod_ts)
diff = datetime.date.today() - file_dt
if diff.days > int(retention_interval):
os.remove(''.join([export_path,'/',f]))
except OSError as err:
print ''.join(['Exception: ',str(err)])
except:
e = sys.exc_info()[0]
print ''.join(['Exception in cleanup_files:', str(e)])
return success
def cleanup_archived_files(params):
success = False
archive_path = params.get('archive_path')
retention_interval = params.get('retention_days')
try:
files = os.listdir(archive_path)
for f in files:
if os.path.isfile(''.join([archive_path,'/',f])):
file_mod_ts = os.path.getmtime(''.join([archive_path,'/',f]))
file_dt = datetime.date.fromtimestamp(file_mod_ts)
diff = datetime.date.today() - file_dt
if diff.days > int(retention_interval):
os.remove(''.join([archive_path,'/',f]))
except OSError as err:
print ''.join(['Exception: ',str(err)])
except:
e = sys.exc_info()[0]
print ''.join(['Exception in cleanup_files:', str(e)])
return success
def main():
params = read_config()
cleanup_archived_files(params)
cleanup_export_files(params)
if __name__ == "__main__":
main()