Skip to content

Commit d3c57e3

Browse files
committed
feature: upgraded Autovacuum Count metric for PG 10+
1 parent 72a3187 commit d3c57e3

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

mamonsu/plugins/pgsql/databases.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ def run(self, zbx):
6464
zbx.send('pgsql.database.discovery[]', zbx.json({'data': dbs}))
6565
del dbs, bloat_count, invalid_indexes_count
6666

67-
result = Pooler.run_sql_type('count_autovacuum')
67+
if Pooler.server_version_greater('10.0'):
68+
result = Pooler.run_sql_type('count_autovacuum', args=["backend_type = 'autovacuum worker'"])
69+
else:
70+
result = Pooler.run_sql_type('count_autovacuum', args=["query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()"])
6871
zbx.send('pgsql.autovacumm.count[]', int(result[0][0]))
6972

7073
def items(self, template, dashboard=False):
@@ -152,8 +155,7 @@ def discovery_rules(self, template, dashboard=False):
152155
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs, triggers=triggers)
153156

154157
def keys_and_queries(self, template_zabbix):
155-
result = ['{0},$2 $1 -c "{1}"'.format(self.key_autovacumm.format("[*]"), Pooler.SQL['count_autovacuum'][0]),
156-
'{0},$2 $1 -c "{1}"'.format(self.key_db_discovery.format("[*]"), self.query_agent_discovery),
158+
result = ['{0},$2 $1 -c "{1}"'.format(self.key_db_discovery.format("[*]"), self.query_agent_discovery),
157159
'{0},echo "{1}" | $3 $2 -v p1="$1"'.format(self.key_db_size.format("[*]"), self.query_size),
158160
'{0},echo "{1}" | $3 $2 -v p1="$1"'.format(self.key_db_age.format("[*]"), self.query_age),
159161
'{0},$3 $2 -d "$1" -c "{1}"'.format(self.key_db_bloating_tables.format("[*]"),
@@ -162,4 +164,8 @@ def keys_and_queries(self, template_zabbix):
162164
self.plugin_config('min_rows'))),
163165
'{0},$3 $2 -d "$1" -c "{1}"'.format(self.key_invalid_indexes.format("[*]"),
164166
self.query_invalid_indexes)]
167+
if LooseVersion(self.VersionPG) >= LooseVersion('10'):
168+
result.append('{0},$2 $1 -c "{1}"'.format(self.key_autovacumm.format("[*]"), Pooler.SQL['count_autovacuum'][0].format("backend_type = 'autovacuum worker'")))
169+
else:
170+
result.append('{0},$2 $1 -c "{1}"'.format(self.key_autovacumm.format("[*]"), Pooler.SQL['count_autovacuum'][0].format("query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()")))
165171
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/driver/pool.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ class Pool(object):
2323
'select mamonsu.count_{0}_files()'
2424
),
2525
'count_autovacuum': (
26-
"select count(*) from pg_catalog.pg_stat_activity where "
27-
"query like '%%autovacuum%%' and state <> 'idle' "
28-
"and pid <> pg_catalog.pg_backend_pid() ",
26+
"SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE {0};",
2927
'select mamonsu.count_autovacuum()'
3028
),
3129
'buffer_cache': (

mamonsu/tools/bootstrap/sql.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,26 @@
5858
WHERE id = 1 LIMIT 1;
5959
$$ LANGUAGE SQL SECURITY DEFINER;
6060
61-
CREATE OR REPLACE FUNCTION mamonsu.count_autovacuum()
62-
RETURNS BIGINT AS $$
63-
SELECT
64-
count(*)::BIGINT
65-
FROM pg_catalog.pg_stat_activity
66-
WHERE
67-
query like '%%autovacuum%%'
68-
and state <> 'idle'
69-
and pid <> pg_catalog.pg_backend_pid()
70-
$$ LANGUAGE SQL SECURITY DEFINER;
61+
DO
62+
$do$
63+
BEGIN
64+
IF (SELECT setting::integer FROM pg_settings WHERE name = 'server_version_num') >= 10000 THEN
65+
CREATE OR REPLACE FUNCTION mamonsu.count_autovacuum()
66+
RETURNS BIGINT AS $$
67+
SELECT count(*)::bigint FROM pg_catalog.pg_stat_activity
68+
WHERE backend_type = 'autovacuum worker'
69+
$$ LANGUAGE SQL SECURITY DEFINER;
70+
ELSE
71+
CREATE OR REPLACE FUNCTION mamonsu.count_autovacuum()
72+
RETURNS BIGINT AS $$
73+
SELECT count(*)::bigint FROM pg_catalog.pg_stat_activity
74+
WHERE query LIKE '%%autovacuum%%'
75+
AND state <> 'idle'
76+
AND pid <> pg_catalog.pg_backend_pid()
77+
$$ LANGUAGE SQL SECURITY DEFINER;
78+
END IF;
79+
END
80+
$do$;
7181
7282
CREATE OR REPLACE FUNCTION mamonsu.get_connections_states()
7383
RETURNS TABLE(state text, waiting boolean) AS $$

0 commit comments

Comments
 (0)