Skip to content

Commit 5ee6c58

Browse files
committed
feature: drafted new autovacuum utilization metrics
(cherry picked from commit 3f937df)
1 parent 7f16ffb commit 5ee6c58

12 files changed

+137
-55
lines changed

Makefile.pkg

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ prepare_builddir: clean
1212
chown -R root.root $(BUILDDIR)
1313

1414
deb: prepare_builddir
15-
cd $(BUILDDIR) && cp -a $(WORKDIR)/packaging/debian . && dpkg-buildpackage -b
15+
cd $(BUILDDIR) && cp -a $(WORKDIR)/packaging/debian . && dpkg-buildpackage -b --no-sign
1616
cp -av build/mamonsu*.deb .
1717

1818
rpm: prepare_builddir $(BUILDDIR)/packaging/rpm/SPECS/$(SPEC)

github-actions-tests/sources/metrics-linux-10.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

github-actions-tests/sources/metrics-linux-11.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

github-actions-tests/sources/metrics-linux-12.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

github-actions-tests/sources/metrics-linux-13.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

github-actions-tests/sources/metrics-linux-14.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

github-actions-tests/sources/metrics-linux-9.6.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

github-actions-tests/sources/metrics-linux-full-list.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pgsql.archive_command[archived_files]
55
pgsql.archive_command[count_files_to_archive]
66
pgsql.archive_command[failed_trying_to_archive]
77
pgsql.archive_command[size_files_to_archive]
8-
pgsql.autovacumm.count[]
8+
pgsql.autovacuum.count[]
9+
pgsql.autovacuum.utilization[]
910
pgsql.bgwriter[buffers_alloc]
1011
pgsql.bgwriter[buffers_backend]
1112
pgsql.bgwriter[buffers_backend_fsync]

mamonsu/plugins/pgsql/autovacuum.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
from distutils.version import LooseVersion
3+
from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin
4+
from .pool import Pooler
5+
from mamonsu.lib.zbx_template import ZbxTemplate
6+
7+
class Autovacuum(Plugin):
8+
9+
AgentPluginType = "pg"
10+
key = "pgsql.autovacuum.count{0}"
11+
12+
def run(self, zbx):
13+
if Pooler.server_version_greater("10.0"):
14+
result = Pooler.run_sql_type("count_autovacuum", args=["backend_type = 'autovacuum worker'"])
15+
else:
16+
result = Pooler.run_sql_type("count_autovacuum", args=[
17+
"query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()"])
18+
zbx.send("pgsql.autovacuum.count[]", int(result[0][0]))
19+
20+
def items(self, template, dashboard=False):
21+
if not dashboard:
22+
return template.item({
23+
"name": "PostgreSQL Autovacuum: Count of Autovacuum Workers",
24+
"key": self.right_type(self.key),
25+
"delay": self.plugin_config("interval")
26+
})
27+
else:
28+
return []
29+
30+
def graphs(self, template, dashboard=False):
31+
result = template.graph({
32+
"name": "PostgreSQL Autovacuum: Count of Autovacuum Workers",
33+
"items": [{
34+
"key": self.right_type(self.key),
35+
"color": "87C2B9",
36+
"drawtype": 2
37+
}]
38+
})
39+
if not dashboard:
40+
return result
41+
else:
42+
return [{
43+
"dashboard": {"name": "PostgreSQL Autovacuum: Count of Autovacuum Workers",
44+
"page": ZbxTemplate.dashboard_page_overview["name"],
45+
"size": ZbxTemplate.dashboard_widget_size_medium,
46+
"position": 5}
47+
}]
48+
49+
def keys_and_queries(self, template_zabbix):
50+
result = []
51+
if LooseVersion(self.VersionPG) >= LooseVersion("10"):
52+
result.append("{0},$2 $1 -c \"{1}\"".format(self.key.format("[*]"),
53+
Pooler.SQL["count_autovacuum"][0].format("backend_type = 'autovacuum worker'")))
54+
else:
55+
result.append("{0},$2 $1 -c \"{1}\"".format(self.key.format("[*]"),
56+
Pooler.SQL["count_autovacuum"][0].format("query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()")))
57+
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/databases.py

+3-47
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin
55
from .pool import Pooler
66
from mamonsu.lib.zbx_template import ZbxTemplate
7+
from mamonsu.plugins.pgsql.autovacuum import Autovacuum
78

89

910
class Databases(Plugin):
@@ -50,7 +51,6 @@ class Databases(Plugin):
5051
key_db_size = "pgsql.database.size{0}"
5152
key_db_age = "pgsql.database.max_age{0}"
5253
key_db_bloating_tables = "pgsql.database.bloating_tables{0}"
53-
key_autovacumm = "pgsql.autovacumm.count{0}"
5454
key_invalid_indexes = "pgsql.database.invalid_indexes{0}"
5555

5656
DEFAULT_CONFIG = {
@@ -81,42 +81,6 @@ def run(self, zbx):
8181
zbx.send("pgsql.database.discovery[]", zbx.json({"data": dbs}))
8282
del dbs, bloat_count, invalid_indexes_count
8383

84-
if Pooler.server_version_greater("10.0"):
85-
result = Pooler.run_sql_type("count_autovacuum", args=["backend_type = 'autovacuum worker'"])
86-
else:
87-
result = Pooler.run_sql_type("count_autovacuum", args=[
88-
"query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()"])
89-
zbx.send("pgsql.autovacumm.count[]", int(result[0][0]))
90-
91-
def items(self, template, dashboard=False):
92-
if not dashboard:
93-
return template.item({
94-
"name": "PostgreSQL Autovacuum: Count of Autovacuum Workers",
95-
"key": self.right_type(self.key_autovacumm),
96-
"delay": self.plugin_config("interval")
97-
})
98-
else:
99-
return []
100-
101-
def graphs(self, template, dashboard=False):
102-
result = template.graph({
103-
"name": "PostgreSQL Autovacuum: Count of Autovacuum Workers",
104-
"items": [{
105-
"key": self.right_type(self.key_autovacumm),
106-
"color": "87C2B9",
107-
"drawtype": 2
108-
}]
109-
})
110-
if not dashboard:
111-
return result
112-
else:
113-
return [{
114-
"dashboard": {"name": "PostgreSQL Autovacuum: Count of Autovacuum Workers",
115-
"page": ZbxTemplate.dashboard_page_overview["name"],
116-
"size": ZbxTemplate.dashboard_widget_size_medium,
117-
"position": 5}
118-
}]
119-
12084
def discovery_rules(self, template, dashboard=False):
12185
rule = {
12286
"name": "PostgreSQL Databases Discovery",
@@ -166,7 +130,7 @@ def discovery_rules(self, template, dashboard=False):
166130
"key": self.right_type(self.key_db_bloating_tables, var_discovery="{#DATABASE},"),
167131
"drawtype": 2},
168132
{"color": "793F5D",
169-
"key": self.right_type(self.key_autovacumm),
133+
"key": self.right_type(Autovacuum.key_count),
170134
"yaxisside": 1,
171135
"drawtype": 2}]
172136
},
@@ -177,7 +141,7 @@ def discovery_rules(self, template, dashboard=False):
177141
"key": self.right_type(self.key_db_age, var_discovery="{#DATABASE},"),
178142
"drawtype": 2},
179143
{"color": "793F5D",
180-
"key": self.right_type(self.key_autovacumm),
144+
"key": self.right_type(Autovacuum.key_count),
181145
"yaxisside": 1,
182146
"drawtype": 2}]
183147
}]
@@ -197,12 +161,4 @@ def keys_and_queries(self, template_zabbix):
197161
self.plugin_config("min_rows"))),
198162
"{0},$3 $2 -d \"$1\" -c \"{1}\"".format(self.key_invalid_indexes.format("[*]"),
199163
self.query_invalid_indexes)]
200-
if LooseVersion(self.VersionPG) >= LooseVersion("10"):
201-
result.append("{0},$2 $1 -c \"{1}\"".format(self.key_autovacumm.format("[*]"),
202-
Pooler.SQL["count_autovacuum"][0].format(
203-
"backend_type = 'autovacuum worker'")))
204-
else:
205-
result.append("{0},$2 $1 -c \"{1}\"".format(self.key_autovacumm.format("[*]"),
206-
Pooler.SQL["count_autovacuum"][0].format(
207-
"query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()")))
208164
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/driver/pool.py

+19
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ class Pool(object):
4747
SELECT mamonsu.count_autovacuum();
4848
"""
4949
),
50+
"autovacuum_utilization": (
51+
"""
52+
WITH count_tb AS (
53+
SELECT count(*)::float AS count
54+
FROM pg_catalog.pg_stat_activity
55+
WHERE {0}
56+
),
57+
settings_tb AS (
58+
SELECT setting::float
59+
FROM pg_catalog.pg_settings
60+
WHERE name = 'autovacuum_max_workers'
61+
)
62+
SELECT count_tb.count*100/settings_tb.setting
63+
FROM count_tb, settings_tb;
64+
""",
65+
"""
66+
SELECT mamonsu.autovacuum_utilization();
67+
"""
68+
),
5069
"buffer_cache": (
5170
"""
5271
SELECT sum(1) * (current_setting('block_size')::int8) AS size,

mamonsu/tools/bootstrap/sql.py

+43
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,49 @@
8181
END
8282
$do$;
8383
84+
DO
85+
$do$
86+
BEGIN
87+
IF (SELECT setting::integer FROM pg_settings WHERE name = 'server_version_num') >= 100000 THEN
88+
DROP FUNCTION IF EXISTS mamonsu.autovacuum_utilization();
89+
CREATE OR REPLACE FUNCTION mamonsu.autovacuum_utilization()
90+
RETURNS FLOAT AS $$
91+
WITH count_tb AS (
92+
SELECT count(*)::float AS count
93+
FROM pg_catalog.pg_stat_activity
94+
WHERE backend_type = 'autovacuum worker'
95+
),
96+
settings_tb AS (
97+
SELECT setting::float
98+
FROM pg_catalog.pg_settings
99+
WHERE name = 'autovacuum_max_workers'
100+
)
101+
SELECT count_tb.count*100/settings_tb.setting
102+
FROM count_tb, settings_tb
103+
$$ LANGUAGE SQL SECURITY DEFINER;
104+
ELSE
105+
DROP FUNCTION IF EXISTS mamonsu.count_autovacuum();
106+
CREATE OR REPLACE FUNCTION mamonsu.count_autovacuum()
107+
RETURNS FLOAT AS $$
108+
WITH count_tb AS (
109+
SELECT count(*)::float AS count
110+
FROM pg_catalog.pg_stat_activity
111+
WHERE query LIKE '%%autovacuum%%'
112+
AND state <> 'idle'
113+
AND pid <> pg_catalog.pg_backend_pid()
114+
),
115+
settings_tb AS (
116+
SELECT setting::float
117+
FROM pg_catalog.pg_settings
118+
WHERE name = 'autovacuum_max_workers'
119+
)
120+
SELECT count_tb.count*100/settings_tb.setting
121+
FROM count_tb, settings_tb
122+
$$ LANGUAGE SQL SECURITY DEFINER;
123+
END IF;
124+
END
125+
$do$;
126+
84127
DO
85128
$do$
86129
BEGIN

0 commit comments

Comments
 (0)