|
2 | 2 | from distutils.version import LooseVersion
|
3 | 3 | from .pool import Pooler
|
4 | 4 | from mamonsu.lib.zbx_template import ZbxTemplate
|
| 5 | +import re |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class ArchiveCommand(Plugin):
|
@@ -77,32 +78,35 @@ def run(self, zbx):
|
77 | 78 | """
|
78 | 79 |
|
79 | 80 | self.disable_and_exit_if_archive_mode_is_not_on()
|
| 81 | + |
80 | 82 | if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater('2.3.4'):
|
81 |
| - result2 = Pooler.query("""SELECT * from mamonsu.archive_stat()""") |
82 |
| - result1 = Pooler.query("""select * from mamonsu.archive_command_files()""") |
| 83 | + result_stats = Pooler.query("""SELECT * from mamonsu.archive_stat()""") |
83 | 84 | else:
|
84 |
| - if Pooler.server_version_greater('10.0'): |
85 |
| - result1 = Pooler.query(query_queue.format('wal_lsn', 'walfile')) |
86 |
| - else: |
87 |
| - result1 = Pooler.query(query_queue.format('xlog_location', 'xlogfile')) |
88 |
| - result2 = Pooler.query("""SELECT archived_count, failed_count from pg_stat_archiver;""") |
89 |
| - |
90 |
| - current_archived_count = result2[0][0] |
91 |
| - current_failed_count = result2[0][1] |
92 |
| - |
| 85 | + result_stats = Pooler.query("""SELECT archived_count, failed_count from pg_stat_archiver;""") |
| 86 | + current_archived_count = result_stats[0][0] |
| 87 | + current_failed_count = result_stats[0][1] |
93 | 88 | if self.old_archived_count is not None:
|
94 | 89 | archived_count = current_archived_count - self.old_archived_count
|
95 | 90 | zbx.send('pgsql.archive_command[{0}]'.format(self.Items[2][0]), archived_count)
|
96 |
| - |
97 | 91 | if self.old_failed_count is not None:
|
98 | 92 | failed_count = current_failed_count - self.old_failed_count
|
99 | 93 | zbx.send('pgsql.archive_command[{0}]'.format(self.Items[3][0]), failed_count)
|
100 |
| - |
101 | 94 | self.old_archived_count = current_archived_count
|
102 | 95 | self.old_failed_count = current_failed_count
|
103 | 96 |
|
104 |
| - zbx.send('pgsql.archive_command[{0}]'.format(self.Items[0][0]), result1[0][0]) |
105 |
| - zbx.send('pgsql.archive_command[{0}]'.format(self.Items[1][0]), result1[0][1]) |
| 97 | + # check the last WAL file name to avoid XXX.history, XXX.partial, etc. |
| 98 | + wal_exists = bool(re.search('^[0-9A-Z]{24}$', str( |
| 99 | + Pooler.query("""SELECT pg_stat_archiver.last_archived_wal FROM pg_stat_archiver;""")[0][0]))) |
| 100 | + if wal_exists: |
| 101 | + if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater('2.3.4'): |
| 102 | + result_queue = Pooler.query("""select * from mamonsu.archive_command_files()""") |
| 103 | + else: |
| 104 | + if Pooler.server_version_greater('10.0'): |
| 105 | + result_queue = Pooler.query(query_queue.format('wal_lsn', 'walfile')) |
| 106 | + else: |
| 107 | + result_queue = Pooler.query(query_queue.format('xlog_location', 'xlogfile')) |
| 108 | + zbx.send('pgsql.archive_command[{0}]'.format(self.Items[0][0]), result_queue[0][0]) |
| 109 | + zbx.send('pgsql.archive_command[{0}]'.format(self.Items[1][0]), result_queue[0][1]) |
106 | 110 |
|
107 | 111 | def items(self, template, dashboard=False):
|
108 | 112 | result = ''
|
|
0 commit comments