Commit 6314a6b9 by Owo Sugiana

Status db mirror menggunakan query

1 parent 2a445676
0.9 7-7-2024
------------
- Status database mirror kini menggunakan query ketimbang baca log file.
0.8.2 14-3-2024
---------------
- Bug fixed pada log_table saat diterapkan pada tabel ke-2. Ini terkait cache
......
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,
from maintenance import (
is_mirror,
Reader,
)
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')
......@@ -55,6 +23,20 @@ def mkdir(name):
os.mkdir(name)
def is_active(conf_file):
r = Reader(conf_file)
port = r['port']
cmd_psql = f'psql -p {port} -c "SELECT pg_is_in_recovery()"'
cmd1 = ['su', '-', 'postgres', '-c', cmd_psql]
cmd2 = ['sed', '-n', '3p']
p1 = Popen(cmd1, stdout=PIPE)
p2 = Popen(cmd2, stdin=p1.stdout, stdout=PIPE)
out, err = p2.communicate()
if not err:
s = out.decode('utf-8')
return s.strip()
def main(argv=sys.argv[1:]):
option = get_option(argv)
conf_file = option.config
......@@ -71,17 +53,9 @@ def main(argv=sys.argv[1:]):
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}')
new_status = is_active(pg_conf)
label = new_status and 'OK' or 'GAGAL'
print(f'{version} {cluster} {label}')
status_file = f'postgresql-{version}-{cluster}.txt'
status_file = os.path.join(status_dir, status_file)
if os.path.exists(status_file):
......
RE_PREFIX = r'([\d]*)-([\d]*)-([\d]*) ([\d]*):([\d]*):([\d]*).([\d]*) '\
r'([A-Z]*) .([\d]*). '
# 2023-05-21 09:56:50.553 WIB [2429] LOG: streaming replication successfully
# connected to primary
# 2022-01-03 07:04:42.728 WIB [1268975] LOG: started streaming WAL from
# primary at 3/B6000000 on timeline 1
# 2023-05-09 07:29:49.335 WIB [3811560] DETAIL: Last completed transaction was
# at log time 2023-05-09 07:28:19.547035+07.
RE_OK = RE_PREFIX + '(LOG: streaming replication successfully|'\
'LOG: started streaming WAL|DETAIL: Last completed)'
# 2022-01-03 07:04:10.757 WIB [1258171] FATAL: terminating walreceiver due to
# timeout
RE_TIMEOUT = RE_PREFIX + 'FATAL: terminating walreceiver due to timeout'
# 2022-01-06 08:55:58.797 WIB [1983436] FATAL: could not connect to the
# primary server: connection to server at "10.8.18.1", port 5432 failed:
# Koneksi kehabisan waktu
RE_NO_ROUTE = RE_PREFIX + 'FATAL: could not connect to'
# 2022-01-03 07:27:16.064 WIB [22592] FATAL: could not receive data from WAL
# stream: ERROR: requested WAL segment 00000001000000A5000000A9 has already
# been removed
RE_REMOVED = RE_PREFIX + '(.*) removed'
# 2022-01-05 04:22:58.394 WIB [1237] LOG: invalid record length
RE_INVALID = RE_PREFIX + 'LOG: invalid'
RE_ANY = RE_PREFIX + 'FATAL: (.*)'
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!