-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_scan.py
More file actions
75 lines (61 loc) · 2.38 KB
/
error_scan.py
File metadata and controls
75 lines (61 loc) · 2.38 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
import os
import json
import pymysql
from datetime import datetime
class ErrorScaner:
init_error_num = None
current_error_num = None
def __init__(self, logfile):
self.init_error_num = self._load_init_error_num()
self.current_error_num = self._scan_logfile_error(logfile)
def _load_init_error_num(self):
result = {
"error_num": None
}
if os.path.exists('result.json'):
with open('result.json', 'r') as f:
result = json.load(f)
else:
with open('result.json', 'w') as f:
json.dump(result, f)
return result["error_num"]
def _scan_logfile_error(self, logfile):
try:
with open(logfile, 'rb') as f:
lines = f.readlines()
error_lines = [line.decode(encoding='GBK') for line in lines if line.decode(encoding='GBK').startswith('[error]')]
# print(error_lines, len(error_lines))
return len(error_lines)
except FileNotFoundError:
print(f'logfile: {logfile} is not exists!')
return None
def update_result(self, conn, init):
# update init_json
result = {
"error_num": self.current_error_num
}
with open('result.json', 'w') as f:
json.dump(result, f)
# update to mysql
if not init:
try:
with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `error_record` (`host`, `created_time`) VALUES (%s, %s)"
cursor.execute(sql, ('127.0.0.1', datetime.now()))
# connection is not autocommit by default. So you must commit to save
# your changes.
conn.commit()
finally:
conn.close()
if __name__ == '__main__':
conn = pymysql.connect(host='127.0.0.1', user='root', port=6606,
password='grapeadmin', db='errorscanerdb',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
es = ErrorScaner('log.sender')
print(es.init_error_num, es.current_error_num)
if es.init_error_num is None:
es.update_result(conn, init=True)
elif es.init_error_num < es.current_error_num:
es.update_result(conn, init=False)