__init__.py
2.97 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
import sys
import os
import re
from argparse import ArgumentParser
from configparser import ConfigParser
from subprocess import (
PIPE,
Popen,
)
from maintenance import is_mirror
from .structure import (
RE_OK,
RE_TIMEOUT,
RE_NO_ROUTE,
RE_REMOVED,
RE_INVALID,
RE_ANY,
)
REC_OK = re.compile(RE_OK)
REC_TIMEOUT = re.compile(RE_TIMEOUT)
REC_NO_ROUTE = re.compile(RE_NO_ROUTE)
REC_REMOVED = re.compile(RE_REMOVED)
REC_INVALID = re.compile(RE_INVALID)
REC_ANY = re.compile(RE_ANY)
PARSER = [
(REC_OK, 'OK'),
(REC_TIMEOUT, 'network bermasalah'),
(REC_NO_ROUTE, 'network bermasalah'),
(REC_REMOVED, 'FATAL, harus dibuat ulang'),
(REC_INVALID, 'WARNING, invalid record length'),
(REC_ANY, 10)]
def parse_log(s):
for regex, message in PARSER:
match = regex.search(s)
if not match:
continue
if isinstance(message, int):
return match.group(message)
return message
def get_option(argv):
parser = ArgumentParser()
parser.add_argument('config')
return parser.parse_args(argv)
def mkdir(name):
if not os.path.exists(name):
os.mkdir(name)
def main(argv=sys.argv[1:]):
option = get_option(argv)
conf_file = option.config
conf = ConfigParser()
conf.read(conf_file)
status_dir = conf.get('mirror_status', 'status_dir')
status_change_cmd = conf.get('mirror_status', 'after')
mkdir(status_dir)
pg_conf_dir = conf.get('main', 'pg_conf_dir')
for version in os.listdir(pg_conf_dir):
version_conf_dir = os.path.join(pg_conf_dir, version)
for cluster in os.listdir(version_conf_dir):
cluster_conf_dir = os.path.join(version_conf_dir, cluster)
pg_conf = os.path.join(cluster_conf_dir, 'postgresql.conf')
if not is_mirror(pg_conf):
continue
log_file = '/var/log/postgresql/'\
f'postgresql-{version}-{cluster}.log'
c1 = ['grep', '^20', log_file]
c2 = ['tail', '-n', '1']
p1 = Popen(c1, stdout=PIPE)
p2 = Popen(c2, stdin=p1.stdout, stdout=PIPE)
new_status, stderr = p2.communicate()
new_status = parse_log(new_status.decode('utf8'))
if not new_status:
continue
print(f'{cluster} {new_status}')
status_file = f'postgresql-{version}-{cluster}.txt'
status_file = os.path.join(status_dir, status_file)
if os.path.exists(status_file):
with open(status_file, 'rb') as f:
old_status = f.read()
old_status = old_status.decode('utf8')
else:
old_status = None
with open(status_file, 'wb') as f:
f.write(new_status.encode('utf8'))
if new_status != old_status:
d = dict(status_file=status_file)
cmd = status_change_cmd.format(**d)
print(cmd)
os.system(cmd)