33from mamonsu .plugins .pgsql .plugin import PgsqlPlugin as Plugin
44from .pool import Pooler
55from mamonsu .lib .zbx_template import ZbxTemplate
6+ import logging
7+
68
79class Autovacuum (Plugin ):
810
911 AgentPluginType = "pg"
10- key = "pgsql.autovacuum.count{0}"
12+ # TODO: unify keys and remove its direct mentioning in zbx.send() functions
13+ key_count = "pgsql.autovacuum.count{0}"
14+ key_utilization = "pgsql.autovacuum.utilization{0}"
15+ key_utilization_avg5 = "pgsql.autovacuum.utilization.avg5{0}"
16+ key_utilization_avg10 = "pgsql.autovacuum.utilization.avg10{0}"
17+ key_utilization_avg15 = "pgsql.autovacuum.utilization.avg15{0}"
18+
19+ DEFAULT_CONFIG = {
20+ "interval" : str (60 )
21+ }
1122
1223 def run (self , zbx ):
1324 if Pooler .server_version_greater ("10.0" ):
14- result = Pooler .run_sql_type ("count_autovacuum" , args = ["backend_type = 'autovacuum worker'" ])
25+ result_count = Pooler .run_sql_type ("count_autovacuum" , args = ["backend_type = 'autovacuum worker'" ])
26+ result_utilization = Pooler .run_sql_type ("autovacuum_utilization" , args = ["backend_type = 'autovacuum worker'" ])
1527 else :
16- result = Pooler .run_sql_type ("count_autovacuum" , args = [
28+ result_count = Pooler .run_sql_type ("count_autovacuum" , args = [
1729 "query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()" ])
18- zbx .send ("pgsql.autovacuum.count[]" , int (result [0 ][0 ]))
19-
30+ result_utilization = Pooler .run_sql_type ("autovacuum_utilization" , args = [
31+ "query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()" ])
32+ zbx .send ("pgsql.autovacuum.count[]" , int (result_count [0 ][0 ]))
33+ zbx .send ("pgsql.autovacuum.utilization[]" , int (result_utilization [0 ][0 ]))
34+
2035 def items (self , template , dashboard = False ):
36+ result = ""
2137 if not dashboard :
22- return template .item ({
38+ result += ( template .item ({
2339 "name" : "PostgreSQL Autovacuum: Count of Autovacuum Workers" ,
24- "key" : self .right_type (self .key ),
40+ "key" : self .right_type (self .key_count ),
2541 "delay" : self .plugin_config ("interval" )
26- })
42+ }))
43+ result += (template .item ({
44+ "name" : "PostgreSQL Autovacuum: Utilization per {0} seconds" .format (self .plugin_config ("interval" )),
45+ "key" : self .right_type (self .key_utilization ),
46+ "value_type" : Plugin .VALUE_TYPE .numeric_float ,
47+ "units" : Plugin .UNITS .percent ,
48+ "delay" : self .plugin_config ("interval" )
49+ }))
50+ result += (template .item ({
51+ "name" : "PostgreSQL Autovacuum: Average Utilization per 5 minutes" ,
52+ "key" : self .right_type (self .key_utilization_avg5 ),
53+ "value_type" : Plugin .VALUE_TYPE .numeric_float ,
54+ "units" : Plugin .UNITS .percent ,
55+ "type" : Plugin .TYPE .CALCULATED ,
56+ "params" : "avg(pgsql.autovacuum.utilization[], 5m)" ,
57+ "delay" : self .plugin_config ("interval" )
58+ }))
59+ result += (template .item ({
60+ "name" : "PostgreSQL Autovacuum: Average Utilization per 10 minutes" ,
61+ "key" : self .right_type (self .key_utilization_avg10 ),
62+ "value_type" : Plugin .VALUE_TYPE .numeric_float ,
63+ "units" : Plugin .UNITS .percent ,
64+ "type" : Plugin .TYPE .CALCULATED ,
65+ "params" : "avg(pgsql.autovacuum.utilization[], 10m)" ,
66+ "delay" : self .plugin_config ("interval" )
67+ }))
68+ result += (template .item ({
69+ "name" : "PostgreSQL Autovacuum: Average Utilization per 15 minutes" ,
70+ "key" : self .right_type (self .key_utilization_avg15 ),
71+ "value_type" : Plugin .VALUE_TYPE .numeric_float ,
72+ "units" : Plugin .UNITS .percent ,
73+ "type" : Plugin .TYPE .CALCULATED ,
74+ "params" : "avg(pgsql.autovacuum.utilization[], 15m)" ,
75+ "delay" : self .plugin_config ("interval" )
76+ }))
77+ return result
2778 else :
2879 return []
2980
3081 def graphs (self , template , dashboard = False ):
3182 result = template .graph ({
3283 "name" : "PostgreSQL Autovacuum: Count of Autovacuum Workers" ,
3384 "items" : [{
34- "key" : self .right_type (self .key ),
85+ "key" : self .right_type (self .key_count ),
3586 "color" : "87C2B9" ,
3687 "drawtype" : 2
3788 }]
@@ -49,9 +100,15 @@ def graphs(self, template, dashboard=False):
49100 def keys_and_queries (self , template_zabbix ):
50101 result = []
51102 if LooseVersion (self .VersionPG ) >= LooseVersion ("10" ):
52- result .append ("{0},$2 $1 -c \" {1}\" " .format (self .key .format ("[*]" ),
103+ result .append ("{0},$2 $1 -c \" {1}\" " .format (self .key_count .format ("[*]" ),
53104 Pooler .SQL ["count_autovacuum" ][0 ].format ("backend_type = 'autovacuum worker'" )))
105+ result .append ("{0},$2 $1 -c \" {1}\" " .format (self .key_utilization .format ("[*]" ),
106+ Pooler .SQL ["autovacuum_utilization" ][0 ].format (
107+ "backend_type = 'autovacuum worker'" )))
54108 else :
55- result .append ("{0},$2 $1 -c \" {1}\" " .format (self .key .format ("[*]" ),
109+ result .append ("{0},$2 $1 -c \" {1}\" " .format (self .key_count .format ("[*]" ),
56110 Pooler .SQL ["count_autovacuum" ][0 ].format ("query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()" )))
111+ result .append ("{0},$2 $1 -c \" {1}\" " .format (self .key_utilization .format ("[*]" ),
112+ Pooler .SQL ["autovacuum_utilization" ][0 ].format (
113+ "query LIKE '%%autovacuum%%' AND state <> 'idle' AND pid <> pg_catalog.pg_backend_pid()" )))
57114 return template_zabbix .key_and_query (result )
0 commit comments