1
1
from mamonsu .plugins .pgsql .plugin import PgsqlPlugin as Plugin
2
+ from distutils .version import LooseVersion
2
3
from .pool import Pooler
3
4
from mamonsu .lib .zbx_template import ZbxTemplate
4
5
@@ -12,14 +13,14 @@ class ArchiveCommand(Plugin):
12
13
query_agent_count_files = """
13
14
WITH values AS (
14
15
SELECT
15
- 4096/(pg_settings.setting::bigint/1024/1024) AS segment_parts_count,
16
+ 4096/(coalesce(1, pg_settings.setting::bigint/1024/1024) ) AS segment_parts_count,
16
17
setting::bigint AS segment_size,
17
18
('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
18
19
('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
19
20
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,
21
22
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
23
24
FROM pg_settings, pg_stat_archiver
24
25
WHERE pg_settings.name = 'wal_segment_size')
25
26
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
@@ -28,17 +29,17 @@ class ArchiveCommand(Plugin):
28
29
query_agent_size_files = """
29
30
WITH values AS (
30
31
SELECT
31
- 4096/(pg_settings.setting::bigint/1024/1024) AS segment_parts_count,
32
+ 4096/(coalesce(1, pg_settings.setting::bigint/1024/1024) ) AS segment_parts_count,
32
33
setting::bigint AS segment_size,
33
34
('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
34
35
('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
35
36
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,
37
38
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
39
40
FROM pg_settings, pg_stat_archiver
40
41
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
42
43
FROM values;
43
44
"""
44
45
@@ -57,28 +58,33 @@ class ArchiveCommand(Plugin):
57
58
old_failed_count = None
58
59
59
60
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 = """
66
62
WITH values AS (
67
63
SELECT
68
- 4096/(pg_settings.setting::bigint/1024/1024) AS segment_parts_count,
64
+ 4096/(coalesce(1, pg_settings.setting::bigint/1024/1024) ) AS segment_parts_count,
69
65
setting::bigint AS segment_size,
70
66
('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
71
67
('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
72
68
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,
74
70
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
76
72
FROM pg_settings, pg_stat_archiver
77
73
WHERE pg_settings.name = 'wal_segment_size')
78
74
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,
79
75
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
80
76
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' ))
82
88
result2 = Pooler .query ("""SELECT archived_count, failed_count from pg_stat_archiver;""" )
83
89
84
90
current_archived_count = result2 [0 ][0 ]
@@ -164,10 +170,16 @@ def triggers(self, template, dashboard=False):
164
170
165
171
def keys_and_queries (self , template_zabbix ):
166
172
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' )))
171
183
result .append ('{0}[*],$2 $1 -c "{1}"' .format (self .key .format ("." + self .Items [2 ][0 ]),
172
184
self .query_agent_archived_count ))
173
185
result .append ('{0}[*],$2 $1 -c "{1}"' .format (self .key .format ("." + self .Items [3 ][0 ]),
0 commit comments