Skip to content

Commit e67ef15

Browse files
Jordan Finebergmajormoses
authored andcommitted
refactor metric-postgres-graphite.rb to use common methods library (#56)
1 parent 522b95e commit e67ef15

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

bin/metric-postgres-graphite.rb

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#
2828

2929
require 'sensu-plugins-postgres/pgpass'
30-
require 'pg'
30+
require 'sensu-plugins-postgres/pgutil'
3131
require 'sensu-plugin/metric/cli'
32+
require 'pg'
3233
require 'socket'
3334

3435
class CheckpostgresReplicationStatus < Sensu::Plugin::Metric::CLI::Graphite
@@ -64,6 +65,12 @@ class CheckpostgresReplicationStatus < Sensu::Plugin::Metric::CLI::Graphite
6465
long: '--password=VALUE',
6566
description: 'Database password'
6667

68+
option :ssl,
69+
short: '-S',
70+
long: '--ssl',
71+
boolean: true,
72+
description: 'Require SSL'
73+
6774
option :scheme,
6875
description: 'Metric naming scheme, text to prepend to metric',
6976
short: '-g SCHEME',
@@ -84,36 +91,44 @@ class CheckpostgresReplicationStatus < Sensu::Plugin::Metric::CLI::Graphite
8491
include Pgpass
8592

8693
def run
94+
ssl_mode = config[:ssl] ? 'require' : 'prefer'
95+
8796
# Establishing connections to the master
8897
pgpass
8998
conn_master = PG.connect(host: config[:master_host],
9099
dbname: config[:database],
91100
user: config[:user],
92101
password: config[:password],
93102
port: config[:port],
103+
sslmode: ssl_mode,
94104
connect_timeout: config[:timeout])
95-
res1 = conn_master.exec('SELECT pg_current_xlog_location()').getvalue(0, 0)
105+
106+
master = if check_vsn_newer_than_postgres9(conn_master)
107+
conn_master.exec('SELECT pg_current_xlog_location()').getvalue(0, 0)
108+
else
109+
conn_master.exec('SELECT pg_current_wal_lsn()').getvalue(0, 0)
110+
end
96111
m_segbytes = conn_master.exec('SHOW wal_segment_size').getvalue(0, 0).sub(/\D+/, '').to_i << 20
97112
conn_master.close
98113

99-
def lag_compute(res1, res, m_segbytes) # rubocop:disable NestedMethodDefinition
100-
m_segment, m_offset = res1.split(/\//)
101-
s_segment, s_offset = res.split(/\//)
102-
((m_segment.hex - s_segment.hex) * m_segbytes) + (m_offset.hex - s_offset.hex)
103-
end
104-
105114
# Establishing connections to the slave
106115
conn_slave = PG.connect(host: config[:slave_host],
107116
dbname: config[:database],
108117
user: config[:user],
109118
password: config[:password],
110119
port: config[:port],
120+
sslmode: ssl_mode,
111121
connect_timeout: config[:timeout])
112-
res = conn_slave.exec('SELECT pg_last_xlog_receive_location()').getvalue(0, 0)
122+
123+
slave = if check_vsn_newer_than_postgres9(conn_slave)
124+
conn_slave.exec('SELECT pg_last_xlog_receive_location()').getvalue(0, 0)
125+
else
126+
conn_slave.exec('SELECT pg_last_wal_replay_lsn()').getvalue(0, 0)
127+
end
113128
conn_slave.close
114129

115130
# Compute lag
116-
lag = lag_compute(res1, res, m_segbytes)
131+
lag = compute_lag(master, slave, m_segbytes)
117132
output config[:scheme].to_s, lag
118133

119134
ok

0 commit comments

Comments
 (0)