bin/check-postgres-replication.rb in sensu-plugins-postgres-2.0.0 vs bin/check-postgres-replication.rb in sensu-plugins-postgres-2.1.0

- old
+ new

@@ -25,10 +25,11 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. # require 'sensu-plugins-postgres/pgpass' +require 'sensu-plugins-postgres/pgutil' require 'sensu-plugin/check/cli' require 'pg' class CheckPostgresReplicationStatus < Sensu::Plugin::Check::CLI option :pgpass, @@ -95,23 +96,12 @@ long: '--timeout', default: nil, description: 'Connection timeout (seconds)') include Pgpass + include PgUtil - def compute_lag(master, slave, m_segbytes) - m_segment, m_offset = master.split('/') - s_segment, s_offset = slave.split('/') - ((m_segment.hex - s_segment.hex) * m_segbytes) + (m_offset.hex - s_offset.hex) - end - - def check_vsn(conn) - pg_vsn = conn.exec("SELECT current_setting('server_version')").getvalue(0, 0) - pg_vsn = pg_vsn.split(' ')[0] - Gem::Version.new(pg_vsn) < Gem::Version.new('10.0') && Gem::Version.new(pg_vsn) >= Gem::Version.new('9.0') - end - def run ssl_mode = config[:ssl] ? 'require' : 'prefer' # Establishing connection to the master pgpass @@ -121,11 +111,11 @@ password: config[:password], port: config[:port], sslmode: ssl_mode, connect_timeout: config[:timeout]) - master = if check_vsn(conn_master) + master = if check_vsn_newer_than_postgres9(conn_master) conn_master.exec('SELECT pg_current_xlog_location()').getvalue(0, 0) else conn_master.exec('SELECT pg_current_wal_lsn()').getvalue(0, 0) end m_segbytes = conn_master.exec('SHOW wal_segment_size').getvalue(0, 0).sub(/\D+/, '').to_i << 20 @@ -138,10 +128,10 @@ password: config[:password], port: config[:port], sslmode: ssl_mode, connect_timeout: config[:timeout]) - slave = if check_vsn(conn_slave) + slave = if check_vsn_newer_than_postgres9(conn_slave) conn_slave.exec('SELECT pg_last_xlog_receive_location()').getvalue(0, 0) else conn_slave.exec('SELECT pg_last_wal_replay_lsn()').getvalue(0, 0) end conn_slave.close