Skip to content

Commit 9f0b655

Browse files
phumpalmajormoses
authored andcommitted
Move common methods to library (#56)
* Move common methods to library * method name should match behavior of method
1 parent 870c805 commit 9f0b655

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44
This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md).
55

66
## [Unreleased]
7+
### Added
8+
- Moves check_vsn and compute_lag to library method
79

810
## [2.0.0] - 2018-10-15
911
### Breaking Changes

bin/check-postgres-replication.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#
2828

2929
require 'sensu-plugins-postgres/pgpass'
30+
require 'sensu-plugins-postgres/pgutil'
3031
require 'sensu-plugin/check/cli'
3132
require 'pg'
3233

@@ -97,18 +98,7 @@ class CheckPostgresReplicationStatus < Sensu::Plugin::Check::CLI
9798
description: 'Connection timeout (seconds)')
9899

99100
include Pgpass
100-
101-
def compute_lag(master, slave, m_segbytes)
102-
m_segment, m_offset = master.split('/')
103-
s_segment, s_offset = slave.split('/')
104-
((m_segment.hex - s_segment.hex) * m_segbytes) + (m_offset.hex - s_offset.hex)
105-
end
106-
107-
def check_vsn(conn)
108-
pg_vsn = conn.exec("SELECT current_setting('server_version')").getvalue(0, 0)
109-
pg_vsn = pg_vsn.split(' ')[0]
110-
Gem::Version.new(pg_vsn) < Gem::Version.new('10.0') && Gem::Version.new(pg_vsn) >= Gem::Version.new('9.0')
111-
end
101+
include PgUtil
112102

113103
def run
114104
ssl_mode = config[:ssl] ? 'require' : 'prefer'
@@ -123,7 +113,7 @@ def run
123113
sslmode: ssl_mode,
124114
connect_timeout: config[:timeout])
125115

126-
master = if check_vsn(conn_master)
116+
master = if check_vsn_newer_than_postgres9(conn_master)
127117
conn_master.exec('SELECT pg_current_xlog_location()').getvalue(0, 0)
128118
else
129119
conn_master.exec('SELECT pg_current_wal_lsn()').getvalue(0, 0)
@@ -140,7 +130,7 @@ def run
140130
sslmode: ssl_mode,
141131
connect_timeout: config[:timeout])
142132

143-
slave = if check_vsn(conn_slave)
133+
slave = if check_vsn_newer_than_postgres9(conn_slave)
144134
conn_slave.exec('SELECT pg_last_xlog_receive_location()').getvalue(0, 0)
145135
else
146136
conn_slave.exec('SELECT pg_last_wal_replay_lsn()').getvalue(0, 0)

lib/sensu-plugins-postgres/pgutil.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module PgUtil
2+
def check_vsn_newer_than_postgres9(conn)
3+
pg_vsn = conn.exec("SELECT current_setting('server_version')").getvalue(0, 0)
4+
pg_vsn = pg_vsn.split(' ')[0]
5+
Gem::Version.new(pg_vsn) < Gem::Version.new('10.0') && Gem::Version.new(pg_vsn) >= Gem::Version.new('9.0')
6+
end
7+
8+
def compute_lag(master, slave, m_segbytes)
9+
m_segment, m_offset = master.split('/')
10+
s_segment, s_offset = slave.split('/')
11+
((m_segment.hex - s_segment.hex) * m_segbytes) + (m_offset.hex - s_offset.hex)
12+
end
13+
end

0 commit comments

Comments
 (0)