11# -*- coding: utf-8 -*-
22
33from mamonsu .plugins .pgsql .plugin import PgsqlPlugin as Plugin
4+ from distutils .version import LooseVersion
45from .pool import Pooler
56
67
@@ -33,30 +34,53 @@ class PgStatStatement(Plugin):
3334 'sum(blk_write_time)/float4(100)' ,
3435 'write io time' , Plugin .UNITS .s , Plugin .DELTA .speed_per_second ,
3536 ('PostgreSQL statements: spend time' , '0000CC' , 0 )),
36- ( 'stat[other_time]' ,
37- 'sum(total_time -blk_read_time-blk_write_time)/float4(100)' ,
37+ [ 'stat[other_time]' ,
38+ 'sum({0} -blk_read_time-blk_write_time)/float4(100)' ,
3839 'other (mostly cpu) time' , Plugin .UNITS .s , Plugin .DELTA .speed_per_second ,
39- ('PostgreSQL statements: spend time' , 'BBBB00' , 0 ))
40+ ('PostgreSQL statements: spend time' , 'BBBB00' , 0 )]]
41+
42+ Items_pg_13 = [
43+
44+ # PostgreSQL 13 specific metrics:
45+ ('stat[wal_bytes]' ,
46+ 'sum(wal_bytes)' ,
47+ 'amount of wal files' , Plugin .UNITS .bytes , Plugin .DELTA .speed_per_second ,
48+ ('PostgreSQL statements: wal statistics' , 'BCC000' , 0 )),
49+ ('stat[wal_records]' ,
50+ 'sum(wal_records)' ,
51+ 'amount of wal records' , Plugin .UNITS .none , Plugin .DELTA .speed_per_second ,
52+ ('PostgreSQL statements: wal statistics' , 'CC6600' , 0 )),
53+ ('stat[wal_fpi]' ,
54+ 'sum(wal_fpi)' ,
55+ 'full page writes' , Plugin .UNITS .none , Plugin .DELTA .speed_per_second ,
56+ ('PostgreSQL statements: wal statistics' , '00CCCC' , 0 ))
4057 ]
4158
4259 def run (self , zbx ):
4360 if not self .extension_installed ('pg_stat_statements' ):
4461 return
45- params = [x [1 ] for x in self .Items ]
62+ if Pooler .server_version_greater ('13' ):
63+ self .Items [5 ][1 ] = self .Items [5 ][1 ].format ("total_exec_time+total_plan_time" )
64+ all_items = self .Items + self .Items_pg_13
65+ params = [x [1 ] for x in all_items ]
66+ else :
67+ self .Items [5 ][1 ] = self .Items [5 ][1 ].format ("total_time" )
68+ all_items = self .Items
69+ params = [x [1 ] for x in all_items ]
4670 result = Pooler .query (self .query .format (
4771 ', ' .join (params )))
4872 for idx , val in enumerate (result [0 ]):
4973 key , val = 'pgsql.{0}' .format (
50- self . Items [idx ][0 ]), int (val )
51- zbx .send (key , val , self . Items [idx ][4 ])
74+ all_items [idx ][0 ]), int (val )
75+ zbx .send (key , val , all_items [idx ][4 ])
5276
5377 def items (self , template ):
5478 result = ''
5579 if self .Type == "mamonsu" :
5680 delta = Plugin .DELTA .as_is
5781 else :
5882 delta = Plugin .DELTA .speed_per_second
59- for item in self .Items :
83+ for item in self .Items + self . Items_pg_13 :
6084 # split each item to get values for keys of both agent type and mamonsu type
6185 keys = item [0 ].split ('[' )
6286 result += template .item ({
@@ -71,11 +95,12 @@ def items(self, template):
7195 def graphs (self , template ):
7296 all_graphs = [
7397 ('PostgreSQL statements: bytes' , None ),
74- ('PostgreSQL statements: spend time' , 1 )]
98+ ('PostgreSQL statements: spend time' , 1 ),
99+ ('PostgreSQL statements: wal statistics' , None )]
75100 result = ''
76101 for graph_item in all_graphs :
77102 items = []
78- for item in self .Items :
103+ for item in self .Items + self . Items_pg_13 :
79104 if item [5 ][0 ] == graph_item [0 ]:
80105 keys = item [0 ].split ('[' )
81106 items .append ({
@@ -91,7 +116,14 @@ def graphs(self, template):
91116
92117 def keys_and_queries (self , template_zabbix ):
93118 result = []
94- for i , item in enumerate (self .Items ):
119+ if LooseVersion (self .VersionPG ) < LooseVersion ('13' ):
120+ self .Items [5 ][1 ] = self .Items [5 ][1 ].format ("total_time" )
121+ all_items = self .Items
122+ else :
123+ self .Items [5 ][1 ] = self .Items [5 ][1 ].format ("total_exec_time+total_plan_time" )
124+ all_items = self .Items + self .Items_pg_13
125+
126+ for i , item in enumerate (all_items ):
95127 keys = item [0 ].split ('[' )
96128 result .append ('{0}[*],$2 $1 -c "{1}"' .format ('{0}{1}.{2}' .format (self .key , keys [0 ], keys [1 ][:- 1 ]),
97129 self .query .format (item [1 ])))
0 commit comments