3
3
from mamonsu .plugins .pgsql .plugin import PgsqlPlugin as Plugin
4
4
from .pool import Pooler
5
5
from mamonsu .lib .zbx_template import ZbxTemplate
6
+ import logging
7
+
6
8
7
9
class Autovacuum (Plugin ):
8
10
9
11
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
+ }
11
22
12
23
def run (self , zbx ):
13
24
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'" ])
15
27
else :
16
- result = Pooler .run_sql_type ("count_autovacuum" , args = [
28
+ result_count = Pooler .run_sql_type ("count_autovacuum" , args = [
17
29
"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
+
20
35
def items (self , template , dashboard = False ):
36
+ result = ""
21
37
if not dashboard :
22
- return template .item ({
38
+ result += ( template .item ({
23
39
"name" : "PostgreSQL Autovacuum: Count of Autovacuum Workers" ,
24
- "key" : self .right_type (self .key ),
40
+ "key" : self .right_type (self .key_count ),
25
41
"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
27
78
else :
28
79
return []
29
80
30
81
def graphs (self , template , dashboard = False ):
31
82
result = template .graph ({
32
83
"name" : "PostgreSQL Autovacuum: Count of Autovacuum Workers" ,
33
84
"items" : [{
34
- "key" : self .right_type (self .key ),
85
+ "key" : self .right_type (self .key_count ),
35
86
"color" : "87C2B9" ,
36
87
"drawtype" : 2
37
88
}]
@@ -49,9 +100,15 @@ def graphs(self, template, dashboard=False):
49
100
def keys_and_queries (self , template_zabbix ):
50
101
result = []
51
102
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 ("[*]" ),
53
104
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'" )))
54
108
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 ("[*]" ),
56
110
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()" )))
57
114
return template_zabbix .key_and_query (result )
0 commit comments