-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_log.py
51 lines (38 loc) · 1.33 KB
/
parse_log.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
from pandas import read_csv
import os
import sys
from libv1.logparser import CleanLogData
"""
ABOUT:
This script parses a single YaV1 log located in the `rawlogs` folder. Just
pass the date of the log file you want to clean and it will output a new
CSV file in the `cleanlogs` folder.
USE:
python parse_log.py 2014-02-01
"""
basedir = os.path.realpath('.')
# Take the date of the log file from the command line
if len(sys.argv) == 2:
logdate = sys.argv[1]
logfilepath = '%s/rawlogs/%s_alert.log' % (basedir, logdate)
if not os.path.exists(logfilepath):
sys.exit('ERROR: %s does not exist.' % logfilepath)
# Read a raw log file into a DataFrame.
in_df = read_csv(logfilepath)
# Clean the log and export it to a CSV file.
out_df = CleanLogData(in_df)
outfilepath = '%s/cleanlogs/%s_alert.csv' % (basedir, logdate)
out_df.to_csv(outfilepath, index=False)
print 'Success! Cleaned log file saved to: %s' % outfilepath
else:
sys.exit('''
**** ERROR ****
---------------
Please run the script again by passing the date of a log file in your
rawlogs folder. For example:
python parse_log.py 2013-02-01
which will clean the log file located at
./rawlogs/2014-02-01_alert.log
and create a new file at
./cleanlogs/2014-02-01_alert.csv
''')