-
Notifications
You must be signed in to change notification settings - Fork 3
/
search_engine.py
executable file
·69 lines (56 loc) · 2.56 KB
/
search_engine.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
import argparse
import configparser
import subprocess
import shlex
BASE_DIR = '/var/tmp/data'
CONFIG = '/path/to/config.ini'
def exec(args):
time = args.time
match = args.match
exact = args.e
count = args.c
sum = args.s
verbose = args.v
if count:
if sum:
if exact:
cmd = 'parallel sqlite3 ::: %s/%s.db ::: "select count(*) from syslog where logs match %s;" | awk \'{m+=$1} END{print m;}\'' % (BASE_DIR, time, shlex.quote('\\"%s\\"' % match))
else:
cmd = 'parallel sqlite3 ::: %s/%s.db ::: "select count(*) from syslog where logs match \'%s\';" | awk \'{m+=$1} END{print m;}\'' % (BASE_DIR, time, match)
else:
if exact:
cmd = 'parallel sqlite3 ::: %s/%s.db ::: "select count(*) from syslog where logs match %s;"' % (BASE_DIR, time, shlex.quote('\\"%s\\"' % match))
else:
cmd = 'parallel sqlite3 ::: %s/%s.db ::: "select count(*) from syslog where logs match \'%s\';"' % (BASE_DIR, time, match)
# debug code
# cmd = 'parallel sqlite3 ::: /mnt/ssd1/benchmark-db/100k/100k-1.db ::: "select count(*) from syslog where logs match \'noc\';"'
else:
if exact:
cmd = 'parallel sqlite3 ::: %s/%s.db ::: "select * from syslog where logs match %s;"' % (BASE_DIR, time, shlex.quote('\\"%s\\"' % match))
else:
cmd = 'parallel sqlite3 ::: %s/%s.db ::: "select * from syslog where logs match \'%s\';"' % (BASE_DIR, time, match)
# debug code
# cmd = 'parallel sqlite3 ::: /mnt/ssd1/benchmark-db/100k/100k-1.db ::: "select * from syslog where logs match \'noc\';"'
if verbose:
print(cmd)
subprocess.call(cmd, shell=True)
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read(CONFIG)
BASE_DIR = config['path']['base-dir']
parser = argparse.ArgumentParser()
parser.add_argument("--time",
help="time explain regexp(YYYY/MM/DD/HH/MIN). eg: 2017/04/27/10/*")
parser.add_argument("--match",
help="matching keyword. eg: noc or 'noc Login'")
parser.add_argument("-e",
help="exact match", action="store_true")
parser.add_argument("-c",
help="count", action="store_true")
parser.add_argument("-s",
help="sum", action="store_true")
parser.add_argument("-v",
help="verbose", action="store_true")
parser.parse_args()
args = parser.parse_args()
exec(args)