Skip to content

Instantly share code, notes, and snippets.

@Andor
Created April 18, 2012 11:53
Show Gist options
  • Save Andor/2413097 to your computer and use it in GitHub Desktop.
Save Andor/2413097 to your computer and use it in GitHub Desktop.
Nagios plugin to check PostgreSQL 9 streaming replication lag.
#!/usr/bin/env python
"""
Nagios plugin to check PostgreSQL 9 streaming replication lag.
Requires psycopg2 and nagiosplugin (both installable with pip/easy_install).
See: http://bucardo.org/check_postgres/check_postgres.pl.html#hot_standby_delay
MIT licensed:
Copyright (c) 2010 Jacob Kaplan-Moss. All rights reserved.
Modified April 4th, 2012 Thomas Kishel.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import nagiosplugin
import psycopg2
__version__ = '1.1'
class StreamingReplicationCheck(nagiosplugin.Check):
def __init__(self, optparser, log):
optparser.description = 'Check PostgreSQL streaming replication lag.'
optparser.version = __version__
optparser.add_option('-M', '--master',
metavar='DSN',
help='DSN for the master connection (e.g. "host=foo user=bar")'
)
optparser.add_option('-S', '--slave',
metavar='DSN',
help='DSN for the slave connection (e.g. "host=foo user=bar")'
)
optparser.add_option('-w', '--warning',
default='1024',
metavar='WARN',
help='warn if replication (receive,replay) lags by WARN kbytes ' \
'(default: 1024,1024).'
)
optparser.add_option('-c', '--critical',
default='4096',
metavar='CRIT',
help='warn if replication (receive,replay) lags by CRIT kbytes ' \
'(default: 4096,4096).'
)
def process_args(self, opts, args):
self.master_conn = psycopg2.connect(opts.master)
self.slave_conn = psycopg2.connect(opts.slave)
self.warning = opts.warning.split(',')
self.critical = opts.critical.split(',')
def obtain_data(self):
mc = self.master_conn.cursor()
mc.execute('SELECT pg_current_xlog_location()')
self.master_xlog_loc = mc.fetchone()[0]
self.master_loc = xlog_to_bytes(self.master_xlog_loc)
self.master_conn.commit()
self.master_conn.close()
sc = self.slave_conn.cursor()
sc.execute('SELECT pg_last_xlog_receive_location()')
self.slave_xlog_receive_loc = sc.fetchone()[0]
self.slave_receive_loc = xlog_to_bytes(self.slave_xlog_receive_loc)
sc.execute('SELECT pg_last_xlog_replay_location()')
self.slave_xlog_replay_loc = sc.fetchone()[0]
self.slave_replay_loc = xlog_to_bytes(self.slave_xlog_replay_loc)
self.slave_conn.commit()
self.slave_conn.close()
"""
The mathematics sometimes rounds to zero.
And sometimes returns negative values.
"""
self.receive_lag = (self.master_loc - self.slave_receive_loc) / 1024
self.replay_lag = (self.master_loc - self.slave_replay_loc) / 1024
self.data = nagiosplugin.Measure.array(
names=['receive_lag', 'replay_lag'],
values=[self.receive_lag, self.replay_lag],
uoms=['kB'],
warnings=self.warning,
criticals=self.critical)
def states(self):
try:
return [m.state() for m in self.data]
except AttributeError:
return []
def performances(self):
try:
loc = "pg_current_xlog_location=%s " \
"pg_last_xlog_receive_location=%s " \
"pg_last_xlog_replay_location=%s" % (
self.master_xlog_loc,
self.slave_xlog_receive_loc,
self.slave_xlog_replay_loc)
lag = "receive_lag=%skB;%s;%s " \
"replay_lag=%skB;%s;%s" % (
self.receive_lag, self.warning[0], self.critical[0],
self.replay_lag, self.warning[1], self.critical[1])
return ["%s %s" % (loc, lag)]
except AttributeError:
return []
def default_message(self):
return "receive_lag %s kB, replay_lag %s kB" % (
self.receive_lag, self.replay_lag)
def xlog_to_bytes(xlog):
"""
Convert an xlog number like '0/C6321D98' to an integer representing
the number of bytes into the xlog.
Logic here is taken from:
https://github.com/mhagander/munin-plugins/blob/master/postgres/postgres_streaming_.in
I assume it's correct...
"""
logid, offset = xlog.split('/')
return (int('ffffffff', 16) * int(logid, 16)) + int(offset, 16)
if __name__ == '__main__':
nagiosplugin.Controller(StreamingReplicationCheck)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment