Skip to content

Commit d260e0f

Browse files
committed
fix: fixed Archive Command plugin queries for compatibility with PG 9.6 and lower
1 parent 75a9be4 commit d260e0f

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

mamonsu/plugins/pgsql/archive_command.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin
2+
from distutils.version import LooseVersion
23
from .pool import Pooler
34
from mamonsu.lib.zbx_template import ZbxTemplate
45

@@ -17,9 +18,9 @@ class ArchiveCommand(Plugin):
1718
('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
1819
('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
1920
CASE WHEN pg_is_in_recovery() THEN NULL ELSE
20-
('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 9 for 8))::bit(32)::int END AS current_wal_div,
21+
('x' || substring(pg_{1}_name(pg_current_{0}()) from 9 for 8))::bit(32)::int END AS current_wal_div,
2122
CASE WHEN pg_is_in_recovery() THEN NULL ELSE
22-
('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 17 for 8))::bit(32)::int END AS current_wal_mod
23+
('x' || substring(pg_{1}_name(pg_current_{0}()) from 17 for 8))::bit(32)::int END AS current_wal_mod
2324
FROM pg_settings, pg_stat_archiver
2425
WHERE pg_settings.name = 'wal_segment_size')
2526
SELECT greatest(coalesce((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1, 0), 0) AS count_files
@@ -33,12 +34,12 @@ class ArchiveCommand(Plugin):
3334
('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
3435
('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
3536
CASE WHEN pg_is_in_recovery() THEN NULL ELSE
36-
('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 9 for 8))::bit(32)::int END AS current_wal_div,
37+
('x' || substring(pg_{1}_name(pg_current_{0}()) from 9 for 8))::bit(32)::int END AS current_wal_div,
3738
CASE WHEN pg_is_in_recovery() THEN NULL ELSE
38-
('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 17 for 8))::bit(32)::int END AS current_wal_mod
39+
('x' || substring(pg_{1}_name(pg_current_{0}()) from 17 for 8))::bit(32)::int END AS current_wal_mod
3940
FROM pg_settings, pg_stat_archiver
4041
WHERE pg_settings.name = 'wal_segment_size')
41-
SELECT greatest(coalesce(((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1) * segment_size, 0), 0) AS size_files
42+
greatest(coalesce(((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1) * segment_size, 0), 0) AS size_files
4243
FROM values;
4344
"""
4445

@@ -57,28 +58,33 @@ class ArchiveCommand(Plugin):
5758
old_failed_count = None
5859

5960
def run(self, zbx):
60-
self.disable_and_exit_if_archive_mode_is_not_on()
61-
if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater('2.3.4'):
62-
result2 = Pooler.query("""SELECT * from mamonsu.archive_stat()""")
63-
result1 = Pooler.query("""select * from mamonsu.archive_command_files()""")
64-
else:
65-
result1 = Pooler.query("""
61+
query_queue = """
6662
WITH values AS (
6763
SELECT
6864
4096/(pg_settings.setting::bigint/1024/1024) AS segment_parts_count,
6965
setting::bigint AS segment_size,
7066
('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
7167
('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
7268
CASE WHEN pg_is_in_recovery() THEN NULL ELSE
73-
('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 9 for 8))::bit(32)::int END AS current_wal_div,
69+
('x' || substring(pg_{1}_name(pg_current_{0}()) from 9 for 8))::bit(32)::int END AS current_wal_div,
7470
CASE WHEN pg_is_in_recovery() THEN NULL ELSE
75-
('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 17 for 8))::bit(32)::int END AS current_wal_mod
71+
('x' || substring(pg_{1}_name(pg_current_{0}()) from 17 for 8))::bit(32)::int END AS current_wal_mod
7672
FROM pg_settings, pg_stat_archiver
7773
WHERE pg_settings.name = 'wal_segment_size')
7874
SELECT greatest(coalesce((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1, 0), 0) AS count_files,
7975
greatest(coalesce(((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1) * segment_size, 0), 0) AS size_files
8076
FROM values;
81-
""")
77+
"""
78+
79+
self.disable_and_exit_if_archive_mode_is_not_on()
80+
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+
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'))
8288
result2 = Pooler.query("""SELECT archived_count, failed_count from pg_stat_archiver;""")
8389

8490
current_archived_count = result2[0][0]
@@ -164,10 +170,16 @@ def triggers(self, template, dashboard=False):
164170

165171
def keys_and_queries(self, template_zabbix):
166172
result = []
167-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[0][0]),
168-
self.query_agent_count_files))
169-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[1][0]),
170-
self.query_agent_size_files))
173+
if LooseVersion(self.VersionPG) >= LooseVersion('10'):
174+
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[0][0]),
175+
self.query_agent_count_files.format('wal_lsn', 'walfile')))
176+
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[1][0]),
177+
self.query_agent_size_files.format('wal_lsn', 'walfile')))
178+
else:
179+
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[0][0]),
180+
self.query_agent_count_files.format('xlog_location', 'xlogfile')))
181+
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[1][0]),
182+
self.query_agent_size_files.format('xlog_location', 'xlogfile')))
171183
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[2][0]),
172184
self.query_agent_archived_count))
173185
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[3][0]),

0 commit comments

Comments
 (0)