__init__.py
4.66 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
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
import sys
import re
from argparse import ArgumentParser
from configparser import ConfigParser
import select
from hashlib import md5
from time import sleep
from subprocess import (
Popen,
PIPE,
)
from mmap import (
mmap,
ACCESS_READ,
)
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import IntegrityError
from opensipkd.waktu import create_datetime
from .models import (
Base,
Log,
)
FIRST_LINE_PATTERN = \
r'^([\d]*)-([\d]*)-([\d]*) '\
r'([\d]*):([\d]*):([\d]*),([\d]*) '
FIRST_LINE_REGEX = re.compile(FIRST_LINE_PATTERN)
abaikan_error = []
global_vars = dict()
class BacaFile:
def __init__(self, filename, cari=''):
if cari:
self.is_found = False
else:
self.is_found = True
is_first = True
self.file = open(filename, 'rb')
s = mmap(self.file.fileno(), 0, access=ACCESS_READ)
while cari:
pos = s.find(cari.encode('utf-8'))
if pos == -1: # Tidak ketemu
return
if pos == 0: # Ketemu di baris ke-1
self.is_found = True
break
if not is_first:
self.file.seek(pos)
self.file.readline()
self.is_found = True
break
cari = '\n' + cari # Cari di baris berikutnya
self.file.seek(0)
is_first = False
def close(self):
self.file.close()
def readline(self):
if self.is_found:
return self.file.readline()
def save(lines, line_id, waktu):
s = '\n'.join(lines)
print([s])
row = Log(line=s, line_id=line_id, tgl=waktu)
db_session = global_vars['db_session']
db_session.add(row)
try:
db_session.flush()
db_session.commit()
except IntegrityError as e:
s_err = str(e)
for error in abaikan_error:
if s_err.find(error) > -1:
print(' sudah ada')
db_session.rollback()
return
raise e
def read_once(log_file, start_from):
lines = []
line_id = None
f = BacaFile(log_file, start_from)
while True:
line = f.readline()
if not line and line_id:
save(lines, line_id, waktu)
break
s = line.rstrip().decode('utf-8')
match = FIRST_LINE_REGEX.search(s)
if match:
if line_id:
save(lines, line_id, waktu)
del lines[:]
line_id = md5(line).hexdigest()
year, month, day, hour, minute, sec, msec = match.groups()
waktu = create_datetime(
int(year), int(month), int(day), int(hour),
int(minute), int(sec), int(msec)*1000)
lines.append(s)
f.close()
def read_forever(log_file):
lines = []
line_id = None
f = Popen(['tail', '-f', log_file], stdout=PIPE, stderr=PIPE)
p = select.poll()
p.register(f.stdout)
while True:
if p.poll(1):
line = f.stdout.readline()
line = line.rstrip()
line = line.decode('utf-8')
match = FIRST_LINE_REGEX.search(line)
if match:
if line_id:
save(lines, line_id, waktu)
line_id = md5(line.encode('utf-8')).hexdigest()
lines = [line]
year, month, day, hour, minute, sec, msec = match.groups()
waktu = create_datetime(
int(year), int(month), int(day), int(hour),
int(minute), int(sec), int(msec)*1000)
else:
lines.append(line)
sleep(0.1)
def get_option(argv):
pars = ArgumentParser()
pars.add_argument('conf')
pars.add_argument('--log-file', help='log file')
pars.add_argument('--run-and-stop', action='store_true')
pars.add_argument('--start-from')
return pars.parse_args(argv)
def main(argv=sys.argv[1:]):
option = get_option(argv)
conf = ConfigParser()
conf.read(option.conf)
for line in conf.get('main', 'abaikan_error').splitlines():
s = line.strip()
if s:
abaikan_error.append(s)
engine = engine_from_config(conf['main'], 'db_')
factory = sessionmaker(bind=engine)
global_vars['db_session'] = factory()
if option.run_and_stop:
read_once(option.log_file, option.start_from)
else:
read_forever(option.log_file)
def init(argv=sys.argv[1:]):
option = get_option(argv)
conf = ConfigParser()
conf.read(option.conf)
engine = engine_from_config(conf['main'], 'db_')
engine.echo = True
Base.metadata.create_all(engine)