Skip to content

Commit 478da6d

Browse files
committed
fix: added pooler custom parameters for replication lag query
1 parent 65ca1e3 commit 478da6d

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

mamonsu/plugins/pgsql/driver/pool.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Pool(object):
1010
'replication_lag_slave_query': (
1111
"SELECT"
1212
" CASE WHEN pg_last_{1}() = pg_last_{2}() THEN 0"
13-
" ELSE extract (epoch FROM now() - coalesce(pg_last_xact_replay_timestamp(), now() - INTERVAL '{0} seconds'))"
14-
" END",
13+
" ELSE extract (epoch FROM now() - coalesce(pg_last_xact_replay_timestamp(), now() - INTERVAL '{0} seconds'))"
14+
" END",
1515
'select mamonsu.timestamp_get()'
1616
),
1717
'count_xlog_files': (
@@ -183,18 +183,24 @@ def databases(self):
183183
databases.append(row[0])
184184
return databases
185185

186-
def get_sql(self, typ, db=None):
186+
def fill_query_params(slf, query, params):
187+
if params:
188+
return query.format(*params)
189+
else:
190+
return query
191+
192+
def get_sql(self, typ, args=None, db=None):
187193
db = self._normalize_db(db)
188194
if typ not in self.SQL:
189195
raise LookupError("Unknown SQL type: '{0}'".format(typ))
190196
result = self.SQL[typ]
191197
if self.is_bootstraped(db):
192-
return result[1]
198+
return self.fill_query_params(result[1], args)
193199
else:
194-
return result[0]
200+
return self.fill_query_params(result[0], args)
195201

196-
def run_sql_type(self, typ, db=None):
197-
return self.query(self.get_sql(typ, db), db)
202+
def run_sql_type(self, typ, args=None, db=None):
203+
return self.query(self.get_sql(typ, args, db), db)
198204

199205
def _normalize_db(self, db=None):
200206
if db is None:

mamonsu/plugins/pgsql/xlog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ def run(self, zbx):
5555

5656
if Pooler.in_recovery():
5757
if Pooler.server_version_greater('10.0'):
58-
lag = Pooler.run_sql_type('replication_lag_slave_query'.format(self.plugin_config('interval'),
58+
lag = Pooler.run_sql_type('replication_lag_slave_query', args=[self.plugin_config('interval'),
5959
'wal_receive_lsn',
60-
'wal_replay_lsn'))
60+
'wal_replay_lsn'])
6161
else:
62-
lag = Pooler.run_sql_type('replication_lag_slave_query'.format(self.plugin_config('interval'),
62+
lag = Pooler.run_sql_type('replication_lag_slave_query', args=[self.plugin_config('interval'),
6363
'xlog_receive_location',
64-
'xlog_replay_location'))
64+
'xlog_replay_location'])
6565
if lag[0][0] is not None:
6666
zbx.send('pgsql.replication_lag[sec]', float(lag[0][0]))
6767
else:

0 commit comments

Comments
 (0)