Skip to content

Commit db108e1

Browse files
committed
refactoring:
- unified SQL queries; - unified PG plugins structure;
1 parent 489fa46 commit db108e1

22 files changed

+1705
-1447
lines changed
Lines changed: 110 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin
24
from distutils.version import LooseVersion
35
from .pool import Pooler
@@ -6,8 +8,10 @@
68

79

810
class ArchiveCommand(Plugin):
9-
AgentPluginType = 'pg'
10-
DEFAULT_CONFIG = {'max_count_files': str(2)}
11+
AgentPluginType = "pg"
12+
DEFAULT_CONFIG = {
13+
"max_count_files": str(2)
14+
}
1115
Interval = 60
1216

1317
# if streaming replication is on, archive queue length and size will always be 0 for replicas
@@ -44,16 +48,20 @@ class ArchiveCommand(Plugin):
4448
FROM values;
4549
"""
4650

47-
query_agent_archived_count = "SELECT archived_count from pg_stat_archiver;"
48-
query_agent_failed_count = "SELECT failed_count from pg_stat_archiver;"
49-
key = 'pgsql.archive_command{0}'
50-
name = 'PostgreSQL archive command {0}'
51+
query_agent_archived_count = """
52+
SELECT archived_count FROM pg_stat_archiver;
53+
"""
54+
query_agent_failed_count = """
55+
SELECT failed_count FROM pg_stat_archiver;
56+
"""
57+
key = "pgsql.archive_command{0}"
58+
name = "PostgreSQL archive command {0}"
5159
Items = [
5260
# key, desc, color, side, graph
53-
('count_files_to_archive', 'count files in archive_status need to archive', 'FFD700', 0, 1),
54-
('size_files_to_archive', 'size of files need to archive', '00FF00', 0, 0),
55-
('archived_files', 'count archived files', '00F000', 0, 1),
56-
('failed_trying_to_archive', 'count attempts to archive files', 'FF0000', 0, 1),
61+
("count_files_to_archive", "count files in archive_status need to archiv", "FFD700", 0, 1),
62+
("size_files_to_archive", "size of files need to archive", "00FF00", 0, 0),
63+
("archived_files", "count archived files", "00F000", 0, 1),
64+
("failed_trying_to_archive", "count attempts to archive files", "FF0000", 0, 1),
5765
]
5866
old_archived_count = None
5967
old_failed_count = None
@@ -79,113 +87,138 @@ def run(self, zbx):
7987

8088
self.disable_and_exit_if_archive_mode_is_not_on()
8189

82-
if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater('2.3.4'):
83-
result_stats = Pooler.query("""SELECT * from mamonsu.archive_stat()""")
90+
if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater("2.3.4"):
91+
result_stats = Pooler.query("""
92+
SELECT *
93+
FROM mamonsu.archive_stat();
94+
""")
8495
else:
85-
result_stats = Pooler.query("""SELECT archived_count, failed_count from pg_stat_archiver;""")
96+
result_stats = Pooler.query("""
97+
SELECT archived_count,
98+
failed_count
99+
FROM pg_stat_archiver;
100+
""")
86101
current_archived_count = result_stats[0][0]
87102
current_failed_count = result_stats[0][1]
88103
if self.old_archived_count is not None:
89104
archived_count = current_archived_count - self.old_archived_count
90-
zbx.send('pgsql.archive_command[{0}]'.format(self.Items[2][0]), archived_count)
105+
zbx.send("pgsql.archive_command[{0}]".format(self.Items[2][0]), archived_count)
91106
if self.old_failed_count is not None:
92107
failed_count = current_failed_count - self.old_failed_count
93-
zbx.send('pgsql.archive_command[{0}]'.format(self.Items[3][0]), failed_count)
108+
zbx.send("pgsql.archive_command[{0}]".format(self.Items[3][0]), failed_count)
94109
self.old_archived_count = current_archived_count
95110
self.old_failed_count = current_failed_count
96111

97112
# 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])))
113+
wal_exists = bool(re.search(r'^[0-9A-Z]{24}$', str(
114+
Pooler.query("""
115+
SELECT pg_stat_archiver.last_archived_wal
116+
FROM pg_stat_archiver;
117+
""")[0][0])))
100118
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()""")
119+
if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater("2.3.4"):
120+
result_queue = Pooler.query("""
121+
SELECT *
122+
FROM mamonsu.archive_command_files();
123+
""")
103124
else:
104-
if Pooler.server_version_greater('10.0'):
105-
result_queue = Pooler.query(query_queue.format('wal_lsn', 'walfile'))
125+
if Pooler.server_version_greater("10.0"):
126+
result_queue = Pooler.query(query_queue.format("wal_lsn", "walfile"))
106127
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])
128+
result_queue = Pooler.query(query_queue.format("xlog_location", "xlogfile"))
129+
zbx.send("pgsql.archive_command[{0}]".format(self.Items[0][0]), result_queue[0][0])
130+
zbx.send("pgsql.archive_command[{0}]".format(self.Items[1][0]), result_queue[0][1])
110131

111132
def items(self, template, dashboard=False):
112-
result = ''
133+
result = ""
113134
result += template.item({
114-
'key': self.right_type(self.key, self.Items[0][0]),
115-
'name': self.name.format(self.Items[0][1]),
116-
'value_type': self.VALUE_TYPE.numeric_unsigned,
117-
'delay': self.plugin_config('interval'),
118-
'delta': Plugin.DELTA.as_is
135+
"key": self.right_type(self.key, self.Items[0][0]),
136+
"name": self.name.format(self.Items[0][1]),
137+
"value_type": self.VALUE_TYPE.numeric_unsigned,
138+
"delay": self.plugin_config("interval"),
139+
"delta": Plugin.DELTA.as_is
119140
}) + template.item({
120-
'key': self.right_type(self.key, self.Items[1][0]),
121-
'name': self.name.format(self.Items[1][1]),
122-
'value_type': self.VALUE_TYPE.numeric_unsigned,
123-
'units': self.UNITS.bytes,
124-
'delay': self.plugin_config('interval'),
125-
'delta': Plugin.DELTA.as_is
141+
"key": self.right_type(self.key, self.Items[1][0]),
142+
"name": self.name.format(self.Items[1][1]),
143+
"value_type": self.VALUE_TYPE.numeric_unsigned,
144+
"units": self.UNITS.bytes,
145+
"delay": self.plugin_config("interval"),
146+
"delta": Plugin.DELTA.as_is
126147
}) + template.item({
127-
'key': self.right_type(self.key, self.Items[2][0]),
128-
'name': self.name.format(self.Items[2][1]),
129-
'value_type': self.VALUE_TYPE.numeric_unsigned,
130-
'delay': self.plugin_config('interval'),
131-
'delta': Plugin.DELTA.simple_change
148+
"key": self.right_type(self.key, self.Items[2][0]),
149+
"name": self.name.format(self.Items[2][1]),
150+
"value_type": self.VALUE_TYPE.numeric_unsigned,
151+
"delay": self.plugin_config("interval"),
152+
"delta": Plugin.DELTA.simple_change
132153
}) + template.item({
133-
'key': self.right_type(self.key, self.Items[3][0]),
134-
'name': self.name.format(self.Items[3][1]),
135-
'value_type': self.VALUE_TYPE.numeric_unsigned,
136-
'delay': self.plugin_config('interval'),
137-
'delta': Plugin.DELTA.simple_change
154+
"key": self.right_type(self.key, self.Items[3][0]),
155+
"name": self.name.format(self.Items[3][1]),
156+
"value_type": self.VALUE_TYPE.numeric_unsigned,
157+
"delay": self.plugin_config("interval"),
158+
"delta": Plugin.DELTA.simple_change
138159
})
139160
if not dashboard:
140161
return result
141162
else:
142-
return [{'dashboard': {'name': self.right_type(self.key, self.Items[1][0]),
143-
'page': ZbxTemplate.dashboard_page_wal['name'],
144-
'size': ZbxTemplate.dashboard_widget_size_medium,
145-
'position': 3}},
146-
{'dashboard': {'name': self.right_type(self.key, self.Items[2][0]),
147-
'page': ZbxTemplate.dashboard_page_wal['name'],
148-
'size': ZbxTemplate.dashboard_widget_size_medium,
149-
'position': 4}}]
163+
return [{
164+
"dashboard": {"name": self.right_type(self.key, self.Items[1][0]),
165+
"page": ZbxTemplate.dashboard_page_wal["name"],
166+
"size": ZbxTemplate.dashboard_widget_size_medium,
167+
"position": 3}
168+
},
169+
{
170+
"dashboard": {"name": self.right_type(self.key, self.Items[2][0]),
171+
"page": ZbxTemplate.dashboard_page_wal["name"],
172+
"size": ZbxTemplate.dashboard_widget_size_medium,
173+
"position": 4}
174+
}]
150175

151176
def graphs(self, template, dashboard=False):
152177
graph = []
153-
result = ''
178+
result = ""
154179
for item in self.Items:
155180
if item[4] == 1:
156181
graph.append({
157-
'key': self.right_type(self.key, item[0]), 'color': item[2], 'yaxisside': item[3]
182+
"key": self.right_type(self.key, item[0]), "color": item[2], "yaxisside": item[3]
158183
})
159-
result += template.graph({'name': self.name.format("") + ' archive status ', 'items': graph})
184+
result += template.graph({
185+
"name": self.name.format("") + " archive status ",
186+
"items": graph
187+
})
160188
if not dashboard:
161189
return result
162190
else:
163-
return [{'dashboard': {'name': self.name.format("") + ' archive status ',
164-
'page': ZbxTemplate.dashboard_page_wal['name'],
165-
'size': ZbxTemplate.dashboard_widget_size_medium,
166-
'position': 1}}]
191+
return [{
192+
"dashboard": {"name": self.name.format("") + " archive status ",
193+
"page": ZbxTemplate.dashboard_page_wal["name"],
194+
"size": ZbxTemplate.dashboard_widget_size_medium,
195+
"position": 1}
196+
}]
167197

168198
def triggers(self, template, dashboard=False):
169199
return template.trigger({
170-
'name': 'PostgreSQL count files in ./archive_status on {HOSTNAME} more than 2',
171-
'expression': '{#TEMPLATE:' + self.right_type(self.key, self.Items[0][0]) +
172-
'.last()}>' + self.plugin_config('max_count_files')
200+
"name": "PostgreSQL count files in ./archive_status on {HOSTNAME} more than 2",
201+
"expression": "{#TEMPLATE:" + self.right_type(self.key,
202+
self.Items[0][0]) + ".last()}>" + self.plugin_config(
203+
"max_count_files")
173204
})
174205

175206
def keys_and_queries(self, template_zabbix):
176207
result = []
177-
if LooseVersion(self.VersionPG) >= LooseVersion('10'):
178-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[0][0]),
179-
self.query_agent_count_files.format('wal_lsn', 'walfile')))
180-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[1][0]),
181-
self.query_agent_size_files.format('wal_lsn', 'walfile')))
208+
if LooseVersion(self.VersionPG) >= LooseVersion("10"):
209+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[0][0]),
210+
self.query_agent_count_files.format("wal_lsn", "walfile")))
211+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[1][0]),
212+
self.query_agent_size_files.format("wal_lsn", "walfile")))
182213
else:
183-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[0][0]),
184-
self.query_agent_count_files.format('xlog_location', 'xlogfile')))
185-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[1][0]),
186-
self.query_agent_size_files.format('xlog_location', 'xlogfile')))
187-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[2][0]),
188-
self.query_agent_archived_count))
189-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + self.Items[3][0]),
190-
self.query_agent_failed_count))
214+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[0][0]),
215+
self.query_agent_count_files.format("xlog_location",
216+
"xlogfile")))
217+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[1][0]),
218+
self.query_agent_size_files.format("xlog_location",
219+
"xlogfile")))
220+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[2][0]),
221+
self.query_agent_archived_count))
222+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[3][0]),
223+
self.query_agent_failed_count))
191224
return template_zabbix.key_and_query(result)

0 commit comments

Comments
 (0)