27
27
#
28
28
29
29
require 'sensu-plugins-postgres/pgpass'
30
- require 'pg '
30
+ require 'sensu-plugins-postgres/pgutil '
31
31
require 'sensu-plugin/metric/cli'
32
+ require 'pg'
32
33
require 'socket'
33
34
34
35
class CheckpostgresReplicationStatus < Sensu ::Plugin ::Metric ::CLI ::Graphite
@@ -64,6 +65,12 @@ class CheckpostgresReplicationStatus < Sensu::Plugin::Metric::CLI::Graphite
64
65
long : '--password=VALUE' ,
65
66
description : 'Database password'
66
67
68
+ option :ssl ,
69
+ short : '-S' ,
70
+ long : '--ssl' ,
71
+ boolean : true ,
72
+ description : 'Require SSL'
73
+
67
74
option :scheme ,
68
75
description : 'Metric naming scheme, text to prepend to metric' ,
69
76
short : '-g SCHEME' ,
@@ -84,36 +91,44 @@ class CheckpostgresReplicationStatus < Sensu::Plugin::Metric::CLI::Graphite
84
91
include Pgpass
85
92
86
93
def run
94
+ ssl_mode = config [ :ssl ] ? 'require' : 'prefer'
95
+
87
96
# Establishing connections to the master
88
97
pgpass
89
98
conn_master = PG . connect ( host : config [ :master_host ] ,
90
99
dbname : config [ :database ] ,
91
100
user : config [ :user ] ,
92
101
password : config [ :password ] ,
93
102
port : config [ :port ] ,
103
+ sslmode : ssl_mode ,
94
104
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
96
111
m_segbytes = conn_master . exec ( 'SHOW wal_segment_size' ) . getvalue ( 0 , 0 ) . sub ( /\D +/ , '' ) . to_i << 20
97
112
conn_master . close
98
113
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
-
105
114
# Establishing connections to the slave
106
115
conn_slave = PG . connect ( host : config [ :slave_host ] ,
107
116
dbname : config [ :database ] ,
108
117
user : config [ :user ] ,
109
118
password : config [ :password ] ,
110
119
port : config [ :port ] ,
120
+ sslmode : ssl_mode ,
111
121
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
113
128
conn_slave . close
114
129
115
130
# Compute lag
116
- lag = lag_compute ( res1 , res , m_segbytes )
131
+ lag = compute_lag ( master , slave , m_segbytes )
117
132
output config [ :scheme ] . to_s , lag
118
133
119
134
ok
0 commit comments