Skip to content

Commit be40e75

Browse files
committed
fix: fixed default agent macros and returned default psql command options to plugins
1 parent bd3965d commit be40e75

15 files changed

+67
-67
lines changed

mamonsu/lib/zbx_template.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import re
3+
34
import mamonsu.lib.platform as platform
45
from mamonsu.lib.const import Template
56
from mamonsu.lib.plugin import Plugin
@@ -174,9 +175,9 @@ def xml(self, plg_type, plugins=None):
174175
template_data = {'template': self.Template, 'application': self.Application}
175176
template_data['items'] = self._get_all('items', plugins)
176177
template_data['discovery_rules'] = self._get_all('discovery_rules', plugins)
177-
if Plugin.Type == 'agent':
178-
template_data['macros'] = self.agent_macro()
179178
template_data['macros'] = self._get_all('macros', plugins)
179+
if Plugin.Type == 'agent':
180+
template_data['macros'] += self.agent_macro()
180181
template_data['screens'] = self.screen(plugins)
181182
template_data['triggers'] = self._get_all('triggers', plugins)
182183
template_data['graphs'] = self._get_all('graphs', plugins)
@@ -301,9 +302,9 @@ def screen(self, plugins=None, xml_key='screen'):
301302

302303
def agent_macro(self, xml_key='macro'):
303304
result = ''
304-
value = {'value': '-qAt -p 5433 -U postgres ', 'macro': "{$PG_CONNINFO}"}
305+
value = {'value': '-qAt -p 5432 -U postgres ', 'macro': "{$PG_CONNINFO}"}
305306
result += '<{1}>{0}</{1}>'.format(self._format_args(self.macro_defaults, value), xml_key)
306-
value = {'value': '/opt/pgpro/std-10/bin/psql', 'macro': "{$PG_PATH}"}
307+
value = {'value': '/usr/bin/psql', 'macro': "{$PG_PATH}"}
307308
result += '<{1}>{0}</{1}>'.format(self._format_args(self.macro_defaults, value), xml_key)
308309
return result
309310

mamonsu/plugins/pgsql/archive_command.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,20 @@ def triggers(self, template, dashboard=False):
201201
def keys_and_queries(self, template_zabbix):
202202
result = []
203203
if LooseVersion(self.VersionPG) >= LooseVersion("10"):
204-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + self.Items[0][0]),
204+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[0][0]),
205205
self.query_agent_count_files.format("wal_lsn",
206206
"walfile")))
207-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + self.Items[1][0]),
207+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[1][0]),
208208
self.query_agent_size_files.format("wal_lsn", "walfile")))
209209
else:
210-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + self.Items[0][0]),
210+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[0][0]),
211211
self.query_agent_count_files.format("xlog_location",
212212
"xlogfile")))
213-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + self.Items[1][0]),
213+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[1][0]),
214214
self.query_agent_size_files.format("xlog_location",
215215
"xlogfile")))
216-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + self.Items[2][0]),
216+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[2][0]),
217217
self.query_agent_archived_count))
218-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + self.Items[3][0]),
218+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + self.Items[3][0]),
219219
self.query_agent_failed_count))
220220
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/autovacuum.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def items(self, template, dashboard=False):
5252
"value_type": Plugin.VALUE_TYPE.numeric_float,
5353
"units": Plugin.UNITS.percent,
5454
"type": Plugin.TYPE.CALCULATED,
55-
"params": "avg({item}, 5m)".format(item=self.key_utilization.format("[]")),
55+
"params": "avg(//{item}, 5m)".format(item=self.right_type(self.key_utilization)),
5656
"delay": self.plugin_config("interval")
5757
}))
5858
result += (template.item({
@@ -61,7 +61,7 @@ def items(self, template, dashboard=False):
6161
"value_type": Plugin.VALUE_TYPE.numeric_float,
6262
"units": Plugin.UNITS.percent,
6363
"type": Plugin.TYPE.CALCULATED,
64-
"params": "avg({item}, 15m)".format(item=self.key_utilization.format("[]")),
64+
"params": "avg(//{item}, 15m)".format(item=self.right_type(self.key_utilization)),
6565
"delay": self.plugin_config("interval")
6666
}))
6767
result += (template.item({
@@ -70,7 +70,7 @@ def items(self, template, dashboard=False):
7070
"value_type": Plugin.VALUE_TYPE.numeric_float,
7171
"units": Plugin.UNITS.percent,
7272
"type": Plugin.TYPE.CALCULATED,
73-
"params": "avg({item}, 30m)".format(item=self.key_utilization.format("[]")),
73+
"params": "avg(//{item}, 30m)".format(item=self.right_type(self.key_utilization)),
7474
"delay": self.plugin_config("interval")
7575
}))
7676
return result
@@ -100,18 +100,18 @@ def keys_and_queries(self, template_zabbix):
100100
result = []
101101
if LooseVersion(self.VersionPG) >= LooseVersion("10"):
102102
# TODO: define another metric key because it duplicates native zabbix agents keys
103-
# result.append("{0},$2 $1 -Aqtc \"{1}\"".format(self.key_count.format("[*]"),
103+
# result.append("{0},$2 $1 -c \"{1}\"".format(self.key_count.format("[*]"),
104104
# Pooler.SQL["count_autovacuum"][0].format(
105105
# "backend_type = 'autovacuum worker'")))
106-
result.append("{0},$2 $1 -Aqtc \"{1}\"".format(self.key_utilization.format("[*]"),
106+
result.append("{0},$2 $1 -c \"{1}\"".format(self.key_utilization.format("[*]"),
107107
Pooler.SQL["autovacuum_utilization"][0].format(
108108
"backend_type = 'autovacuum worker'")))
109109
else:
110110
# TODO: define another metric key because it duplicates native zabbix agents keys
111-
# result.append("{0},$2 $1 -Aqtc \"{1}\"".format(self.key_count.format("[*]"),
111+
# result.append("{0},$2 $1 -c \"{1}\"".format(self.key_count.format("[*]"),
112112
# Pooler.SQL["count_autovacuum"][0].format(
113113
# "query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()")))
114-
result.append("{0},$2 $1 -Aqtc \"{1}\"".format(self.key_utilization.format("[*]"),
114+
result.append("{0},$2 $1 -c \"{1}\"".format(self.key_utilization.format("[*]"),
115115
Pooler.SQL["autovacuum_utilization"][0].format(
116116
"query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()")))
117117
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/bgwriter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,5 @@ def keys_and_queries(self, template_zabbix):
120120
for item in self.Items:
121121
# delete from key '[' and ']' in Item for zabbix agent
122122
result.append(
123-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + item[0]), self.query.format(item[0])))
123+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + item[0]), self.query.format(item[0])))
124124
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/checkpoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ def keys_and_queries(self, template_zabbix):
142142
for num, item in enumerate(self.Items):
143143
if num > 1:
144144
result.append(
145-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + item[1]), self.query.format(item[0])))
145+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + item[1]), self.query.format(item[0])))
146146
else:
147147
result.append(
148-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + item[1]),
148+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + item[1]),
149149
self.query_interval.format(item[0])))
150150
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/connections.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,31 +239,31 @@ def triggers(self, template, dashboard=False):
239239
def keys_and_queries(self, template_zabbix):
240240
result = []
241241
for item in self.Items:
242-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format("." + item[1]),
242+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format("." + item[1]),
243243
self.query_agent.format(item[1],
244244
"AND (backend_type = 'client backend' OR backend_type = 'parallel worker')" if LooseVersion(
245245
self.VersionPG) >= LooseVersion(
246246
"10") else "")))
247-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".total"), self.query_agent_total.format(
247+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".total"), self.query_agent_total.format(
248248
"(backend_type = 'client backend' OR backend_type = 'parallel worker')" if LooseVersion(
249249
self.VersionPG) >= LooseVersion(
250250
"10") else "state IS NOT NULL")))
251251
if LooseVersion(self.VersionPG) < LooseVersion("9.6"):
252252
result.append(
253-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".waiting"), self.query_agent_waiting_old_v.format(
253+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".waiting"), self.query_agent_waiting_old_v.format(
254254
"(backend_type = 'client backend' OR backend_type = 'parallel worker')" if LooseVersion(
255255
self.VersionPG) >= LooseVersion(
256256
"10") else "state IS NOT NULL")))
257257
else:
258258
result.append(
259-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".waiting"), self.query_agent_waiting_new_v.format(
259+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".waiting"), self.query_agent_waiting_new_v.format(
260260
"(backend_type = 'client backend' OR backend_type = 'parallel worker')" if LooseVersion(
261261
self.VersionPG) >= LooseVersion(
262262
"10") else "state IS NOT NULL")))
263263
if LooseVersion(self.VersionPG) >= LooseVersion("10"):
264-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".other"),
264+
result.append("{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".other"),
265265
self.query_other_connections.format(
266266
"', '".join(self.default_backend_types))))
267267
result.append(
268-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".max_connections"), self.query_agent_max_conn))
268+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".max_connections"), self.query_agent_max_conn))
269269
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/databases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ def discovery_rules(self, template, dashboard=False):
152152
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs, triggers=triggers)
153153

154154
def keys_and_queries(self, template_zabbix):
155-
result = ["{0},$2 $1 -Aqtc \"{1}\"".format(self.key_db_discovery.format("[*]"), self.query_agent_discovery),
155+
result = ["{0},$2 $1 -c \"{1}\"".format(self.key_db_discovery.format("[*]"), self.query_agent_discovery),
156156
"{0},echo \"{1}\" | $3 $2 -v p1=\"$1\"".format(self.key_db_size.format("[*]"), self.query_size),
157157
"{0},echo \"{1}\" | $3 $2 -v p1=\"$1\"".format(self.key_db_age.format("[*]"), self.query_age),
158-
"{0},$3 $2 -d \"$1\" -Aqtc \"{1}\"".format(self.key_db_bloating_tables.format("[*]"),
158+
"{0},$3 $2 -d \"$1\" -c \"{1}\"".format(self.key_db_bloating_tables.format("[*]"),
159159
self.query_bloating_tables.format(
160160
self.plugin_config("bloat_scale"),
161161
self.plugin_config("min_rows"))),
162-
"{0},$3 $2 -d \"$1\" -Aqtc \"{1}\"".format(self.key_invalid_indexes.format("[*]"),
162+
"{0},$3 $2 -d \"$1\" -c \"{1}\"".format(self.key_invalid_indexes.format("[*]"),
163163
self.query_invalid_indexes)]
164164
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/health.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from .pool import Pooler
55
import time
66
from mamonsu.lib.zbx_template import ZbxTemplate
7-
from mamonsu.plugins.pgsql.instance import Instance
8-
97

108
class PgHealth(Plugin):
119
AgentPluginType = "pg"
@@ -59,9 +57,10 @@ def items(self, template, dashboard=False):
5957
"value_type": Plugin.VALUE_TYPE.numeric_float,
6058
"units": Plugin.UNITS.percent,
6159
"type": Plugin.TYPE.CALCULATED,
62-
"params": "last({blocks_hit})*100/(last({blocks_hit})+last({blocks_read}))".format(
63-
blocks_hit=Instance.key + Instance.Items[2][1],
64-
blocks_read=Instance.key + Instance.Items[3][1])
60+
"params": "last(//{blocks_hit})*100/(last(//{blocks_hit})+last(//{blocks_read}))".format(
61+
# TODO: hardcoded params
62+
blocks_hit=self.right_type("pgsql.blocks.hit{0}"),
63+
blocks_read=self.right_type("pgsql.blocks.read{0}"))
6564
}) + template.item({
6665
"name": "PostgreSQL Health: Service Uptime",
6766
"key": self.right_type(self.key_uptime),
@@ -123,8 +122,8 @@ def triggers(self, template, dashboard=False):
123122

124123
def keys_and_queries(self, template_zabbix):
125124
# TODO: define another metric key because it duplicates native zabbix agents keys
126-
# result = ["{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key_ping.format(""), self.query_health),
127-
# "{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key_uptime.format(""), self.query_uptime),
128-
# "{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key_version.format(""), self.query_version)]
129-
result = ["{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key_version.format(""), self.query_version)]
125+
# result = ["{0}[*],$2 $1 -c \"{1}\"".format(self.key_ping.format(""), self.query_health),
126+
# "{0}[*],$2 $1 -c \"{1}\"".format(self.key_uptime.format(""), self.query_uptime),
127+
# "{0}[*],$2 $1 -c \"{1}\"".format(self.key_version.format(""), self.query_version)]
128+
result = ["{0}[*],$2 $1 -c \"{1}\"".format(self.key_version.format(""), self.query_version)]
130129
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/instance.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Instance(Plugin):
7070
Plugin.UNITS.none, Plugin.DELTA.simple_change)
7171
]
7272

73-
key_server_mode = "pgsql.server_mode"
73+
key_server_mode = "pgsql.server_mode{0}"
7474
query_server_mode = """
7575
SELECT CASE WHEN pg_is_in_recovery() THEN 'STANDBY'
7676
ELSE 'MASTER'
@@ -98,7 +98,7 @@ def run(self, zbx):
9898
zbx_key, value = "pgsql.{0}".format(all_items[key][1]), int(value)
9999
zbx.send(zbx_key, value, all_items[key][5], only_positive_speed=True)
100100
result_server_mode = Pooler.query(self.query_server_mode)[0][0]
101-
zbx.send(self.key_server_mode, result_server_mode)
101+
zbx.send(self.right_type(self.key_server_mode), result_server_mode)
102102
del columns, result, result_server_mode
103103

104104
def items(self, template, dashboard=False):
@@ -209,7 +209,7 @@ def keys_and_queries(self, template_zabbix):
209209
for item in all_items:
210210
# split each item to get values for keys of both agent type and mamonsu type
211211
keys = item[1].split("[")
212-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format("{0}{1}.{2}".format(self.key, keys[0], keys[1][:-1]),
213-
self.query_agent.format(format(item[0]))))
214-
result.append("{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key_server_mode, self.query_server_mode))
212+
result.append("{0}[*],$2 $1 -c \"{1}\"".format("{0}{1}.{2}".format(self.key, keys[0], keys[1][:-1]),
213+
self.query_agent.format(format(item[0]))))
214+
result.append("{0},$2 $1 -c \"{1}\"".format(self.key_server_mode.format("[*]"), self.query_server_mode))
215215
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/oldest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def triggers(self, template, dashboard=False):
108108
})
109109

110110
def keys_and_queries(self, template_zabbix):
111-
result = ["{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".xid_age"), self.OldestXidSql),
112-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format(self.key.format(".transaction_time"), self.OldestTransactionSql),
113-
"{0}[*],$2 $1 -Aqtc \"{1}\"".format("pgsql.parallel{0}".format(".queries"), self.ParallelQueries)]
111+
result = ["{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".xid_age"), self.OldestXidSql),
112+
"{0}[*],$2 $1 -c \"{1}\"".format(self.key.format(".transaction_time"), self.OldestTransactionSql),
113+
"{0}[*],$2 $1 -c \"{1}\"".format("pgsql.parallel{0}".format(".queries"), self.ParallelQueries)]
114114
return template_zabbix.key_and_query(result)

0 commit comments

Comments
 (0)