-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogwatcher.py
executable file
·189 lines (169 loc) · 5.52 KB
/
logwatcher.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import time
try:
import sqlite3
except (ImportError):
import sqlite as sqlite3
LOG_START_PAT = '(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})[-+]\d{2}:\d{2}: (\w+):'
_db_name = 'logwatch'
_log_display_format = '[ID: "{0}", Type: "{1}", Date: "{2}"]\n\nContent: {3}'
def clear_table():
conn = sqlite3.Connection(_db_name)
c = conn.cursor()
c.execute('DELETE FROM `logs`')
result = c.fetchall()
conn.commit()
return result
def get_last():
conn = sqlite3.Connection(_db_name)
c = conn.cursor()
sql_query = '''SELECT `id`, `type`, `date`, `content`
FROM `logs`
ORDER BY `id` DESC
LIMIT 1'''
c.execute(sql_query)
return c.fetchone()
def init_database():
conn = sqlite3.Connection(_db_name)
c = conn.cursor()
sql_command = '''CREATE TABLE `logs` (
`id` INTEGER PRIMARY KEY,
`type` TEXT,
`date` TEXT,
`content` TEXT
)'''
try:
result = c.execute(sql_command)
except (sqlite3.OperationalError):
result = False
conn.commit()
return result
def log_search(query_params):
conn = sqlite3.Connection(_db_name)
c = conn.cursor()
sql_command = '''SELECT `id`, `type`, `date`, `content`
FROM `logs`
'''
begins_with_keyword = any([
query_params.lower().startswith('where '),
query_params.lower().startswith('order '),
query_params.lower().startswith('group '),
query_params.lower().startswith('limit ')
])
if not begins_with_keyword:
sql_command += ' WHERE '
sql_command += query_params
c.execute(sql_command)
return c.fetchall()
def save_log(log, cursor=None):
'''Saves log to the database. Log is passed as dictionary.
'''
log['content'] = log['content'].strip(' \n')
if cursor:
params = (log['type'], log['date'], log['content'])
sql_command = '''INSERT INTO `logs` (`type`,`date`,`content`)
VALUES (?, ?, ?)'''
result = cursor.execute(sql_command, params)
return result
else:
print '"{type}" log dated "{date}" was:\n\n{content}\n'.format(**log)
return False # not saved, only outputted
def process_file(log_filename):
conn = sqlite3.Connection(_db_name)
cursor = conn.cursor()
with open(log_filename) as log_file:
i = 1 # lines count
lc = 0 # logs count
cur_log = {}
proc_status_fmt = '\r{} lines processed, {} logs found'
compiled_pattern = re.compile(LOG_START_PAT)
while True:
line = log_file.readline()
if not line:
break
match = compiled_pattern.match(line)
if match:
if cur_log:
save_log(cur_log, cursor)
lc += 1
cur_log = {}
cur_log['date'] = match.group(1)
cur_log['type'] = match.group(2)
cur_log['content'] = ' '.join(line.split(' ')[2:]).lstrip(' \n')
elif cur_log:
cur_log['content'] += line
else:
print 'STRANGE'
raise KeyboardInterrupt
i += 1
# print 'Line no. {} processed'.format(i)
sys.stdout.flush()
sys.stdout.write(proc_status_fmt.format(i, lc))
conn.commit()
conn.close()
print
print 'Finished processing log file.'
def show_help(subc=None):
docs = {
'init': 'Initialize the database by creating table and file',
'clear': 'Delete logs from database',
'process': 'Process the filename given as parameter and save logs',
'last': 'Show last log from the database',
'search': 'Search logs by giving SQLite condition as parameter'
}
if subc:
try:
print '"{} {}": {}'.format(sys.argv[0], subc, docs[subc])
except (KeyError):
print 'Command or help for this command does not exist'
else:
print 'Possible commands: {}'.format(', '.join(docs))
def main():
try:
command = sys.argv[1]
except (IndexError):
command = None
if command == 'init':
if init_database():
print 'Database initialized'
else:
print 'Database has not been initialized'
elif command == 'clear':
result = clear_table()
print 'Cleared the table'
elif command == 'process':
try:
process_file(sys.argv[2])
except (IndexError):
frmt = 'Provide name of the file to be processed: "{} {} {}"'
repls = (sys.argv[0], command, 'filename.log')
print frmt.format(*repls)
elif command == 'last':
last_log = get_last()
if last_log:
print _log_display_format.format(*last_log)
else:
print 'No logs have been found'
elif command == 'search':
result = log_search(' '.join(sys.argv[2:]))
if result:
for row in result:
print _log_display_format.format(*row) + '\n' + '=' * 60
else:
print 'No results found'
elif command == 'help':
show_help(sys.argv[2] if sys.argv[2:3] else None)
else:
if command:
print 'Unsupported command: "{}"'.format(command)
else:
print 'Provide some command to be executed'
show_help()
if __name__ == '__main__':
start_time = time.time()
main()
print '(Execution time: {} seconds)'.format(time.time() - start_time)