Skip to content

Commit f4179d4

Browse files
csmithATsquizsstarcher
authored andcommitted
add optional timeouts to all the scripts. without being able to speci… (#13)
* add optional timeouts to all the scripts. without being able to specify these, the scripts all sit there trying to connect. * update the changelog * tidy up pg.connect calls to make them consistent. * tidy up robocop errors
1 parent 76f3942 commit f4179d4

14 files changed

+155
-28
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 at [Keep A Changelog](http://keepachangelog.com/)
55

66
## [Unreleased]
7+
### Changed
8+
- Updated all the scripts to add an optional timeout setting.
79

810
## [0.1.1] - 2016-03-24
911
### Added

bin/check-postgres-alive.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@ class CheckPostgres < Sensu::Plugin::Check::CLI
5858
long: '--port PORT',
5959
default: 5432
6060

61+
option :timeout,
62+
description: 'Connection timeout (seconds)',
63+
short: '-T TIMEOUT',
64+
long: '--timeout TIMEOUT',
65+
default: nil
66+
6167
def run
62-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, config[:database], config[:user], config[:password])
68+
con = PG.connect(host: config[:hostname],
69+
dbname: config[:database],
70+
user: config[:user],
71+
password: config[:password],
72+
connect_timeout: config[:timeout])
6373
res = con.exec('select version();')
6474
info = res.first
6575

bin/check-postgres-connections.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CheckPostgresConnections < Sensu::Plugin::Check::CLI
5858
long: '--port PORT',
5959
default: 5432
6060

61-
option :db,
61+
option :database,
6262
description: 'Database name',
6363
short: '-d DB',
6464
long: '--db DB',
@@ -85,9 +85,19 @@ class CheckPostgresConnections < Sensu::Plugin::Check::CLI
8585
boolean: true,
8686
default: false
8787

88+
option :timeout,
89+
description: 'Connection timeout (seconds)',
90+
short: '-T TIMEOUT',
91+
long: '--timeout TIMEOUT',
92+
default: nil
93+
8894
def run
8995
begin
90-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, config[:db], config[:user], config[:password])
96+
con = PG.connect(host: config[:hostname],
97+
dbname: config[:database],
98+
user: config[:user],
99+
password: config[:password],
100+
connect_timeout: config[:timeout])
91101
max_conns = con.exec('SHOW max_connections').getvalue(0, 0).to_i
92102
current_conns = con.exec('SELECT count(*) from pg_stat_activity').getvalue(0, 0).to_i
93103
rescue PG::Error => e

bin/check-postgres-query.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ class CheckPostgresQuery < Sensu::Plugin::Check::CLI
8888
long: '--critical CRITICAL',
8989
default: nil
9090

91+
option :timeout,
92+
description: 'Connection timeout (seconds)',
93+
short: '-T TIMEOUT',
94+
long: '--timeout TIMEOUT',
95+
default: nil
96+
9197
def run
9298
begin
93-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, config[:db], config[:user], config[:password])
99+
con = PG.connect(host: config[:hostname],
100+
dbname: config[:database],
101+
user: config[:user],
102+
password: config[:password],
103+
connect_timeout: config[:timeout])
94104
res = con.exec(config[:query].to_s)
95105
rescue PG::Error => e
96106
unknown "Unable to query PostgreSQL: #{e.message}"

bin/check-postgres-replication.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ class CheckPostgresReplicationStatus < Sensu::Plugin::Check::CLI
7878
# #YELLOW
7979
proc: lambda { |s| s.to_i }) # rubocop:disable Lambda
8080

81+
option(:timeout,
82+
short: '-T',
83+
long: '--timeout',
84+
default: nil,
85+
description: 'Connection timeout (seconds)')
86+
8187
def compute_lag(master, slave, m_segbytes)
8288
m_segment, m_offset = master.split('/')
8389
s_segment, s_offset = slave.split('/')
@@ -92,7 +98,8 @@ def run
9298
dbname: config[:database],
9399
user: config[:user],
94100
password: config[:password],
95-
sslmode: ssl_mode)
101+
sslmode: ssl_mode,
102+
connect_timeout: config[:timeout])
96103

97104
master = conn_master.exec('SELECT pg_current_xlog_location()').getvalue(0, 0)
98105
m_segbytes = conn_master.exec('SHOW wal_segment_size').getvalue(0, 0).sub(/\D+/, '').to_i << 20
@@ -103,7 +110,8 @@ def run
103110
dbname: config[:database],
104111
user: config[:user],
105112
password: config[:password],
106-
sslmode: ssl_mode)
113+
sslmode: ssl_mode,
114+
connect_timeout: config[:timeout])
107115

108116
slave = conn_slave.exec('SELECT pg_last_xlog_receive_location()').getvalue(0, 0)
109117
conn_slave.close

bin/metric-postgres-connections.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,20 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
6666
long: '--scheme SCHEME',
6767
default: "#{Socket.gethostname}.postgresql"
6868

69+
option :timeout,
70+
description: 'Connection timeout (seconds)',
71+
short: '-T TIMEOUT',
72+
long: '--timeout TIMEOUT',
73+
default: nil
74+
6975
def run
7076
timestamp = Time.now.to_i
7177

72-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, 'postgres', config[:user], config[:password])
78+
con = PG.connect(host: config[:hostname],
79+
dbname: config[:database],
80+
user: config[:user],
81+
password: config[:password],
82+
connect_timeout: config[:timeout])
7383
request = [
7484
'select count(*), waiting from pg_stat_activity',
7585
"where datname = '#{config[:db]}' group by waiting"

bin/metric-postgres-dbsize.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
5555
long: '--port PORT',
5656
default: 5432
5757

58-
option :db,
58+
option :database,
5959
description: 'Database name',
6060
short: '-d DB',
6161
long: '--db DB',
@@ -66,10 +66,20 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
6666
long: '--scheme SCHEME',
6767
default: "#{Socket.gethostname}.postgresql"
6868

69+
option :timeout,
70+
description: 'Connection timeout (seconds)',
71+
short: '-T TIMEOUT',
72+
long: '--timeout TIMEOUT',
73+
default: nil
74+
6975
def run
7076
timestamp = Time.now.to_i
7177

72-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, 'postgres', config[:user], config[:password])
78+
con = PG.connect(host: config[:hostname],
79+
dbname: config[:database],
80+
user: config[:user],
81+
password: config[:password],
82+
connect_timeout: config[:timeout])
7383
request = [
7484
"select pg_database_size('#{config[:db]}')"
7585
]

bin/metric-postgres-graphite.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,19 @@ class CheckpostgresReplicationStatus < Sensu::Plugin::Metric::CLI::Graphite
7070
long: '--port PORT',
7171
default: 5432
7272

73-
def run
74-
@dbmaster = config[:master_host]
75-
@dbslave = config[:slave_host]
76-
@dbport = config[:port]
77-
@dbname = config[:database]
78-
@dbusername = config[:user]
79-
@password = config[:pass]
73+
option :timeout,
74+
description: 'Connection timeout (seconds)',
75+
short: '-T TIMEOUT',
76+
long: '--timeout TIMEOUT',
77+
default: nil
8078

79+
def run
8180
# Establishing connections to the master
82-
conn_master = PGconn.connect(@dbmaster, @dbport, '', '', @dbname, @dbusername, @password)
81+
conn_master = PG.connect(host: config[:master_host],
82+
dbname: config[:database],
83+
user: config[:user],
84+
password: config[:password],
85+
connect_timeout: config[:timeout])
8386
res1 = conn_master.exec('SELECT pg_current_xlog_location()').getvalue(0, 0)
8487
m_segbytes = conn_master.exec('SHOW wal_segment_size').getvalue(0, 0).sub(/\D+/, '').to_i << 20
8588
conn_master.close
@@ -91,7 +94,11 @@ def lag_compute(res1, res, m_segbytes) # rubocop:disable NestedMethodDefinition
9194
end
9295

9396
# Establishing connections to the slave
94-
conn_slave = PGconn.connect(@dbslave, @dbport, '', '', @dbname, @dbusername, @password)
97+
conn_slave = PG.connect(host: config[:slave_host],
98+
dbname: config[:database],
99+
user: config[:user],
100+
password: config[:password],
101+
connect_timeout: config[:timeout])
95102
res = conn_slave.exec('SELECT pg_last_xlog_receive_location()').getvalue(0, 0)
96103
conn_slave.close
97104

bin/metric-postgres-locks.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,22 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
6666
long: '--scheme SCHEME',
6767
default: "#{Socket.gethostname}.postgresql"
6868

69+
option :timeout,
70+
description: 'Connection timeout (seconds)',
71+
short: '-T TIMEOUT',
72+
long: '--timeout TIMEOUT',
73+
default: nil
74+
6975
def run
7076
timestamp = Time.now.to_i
7177

7278
locks_per_type = Hash.new(0)
7379

74-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, 'postgres', config[:user], config[:password])
80+
con = PG.connect(host: config[:hostname],
81+
dbname: config[:database],
82+
user: config[:user],
83+
password: config[:password],
84+
connect_timeout: config[:timeout])
7585
request = [
7686
'SELECT mode, count(mode) FROM pg_locks',
7787
"where database = (select oid from pg_database where datname = '#{config[:db]}')",

bin/metric-postgres-statsbgwriter.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,20 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
6060
long: '--scheme SCHEME',
6161
default: "#{Socket.gethostname}.postgresql"
6262

63+
option :timeout,
64+
description: 'Connection timeout (seconds)',
65+
short: '-T TIMEOUT',
66+
long: '--timeout TIMEOUT',
67+
default: nil
68+
6369
def run
6470
timestamp = Time.now.to_i
6571

66-
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, 'postgres', config[:user], config[:password])
72+
con = PG.connect(host: config[:hostname],
73+
dbname: 'postgres',
74+
user: config[:user],
75+
password: config[:password],
76+
connect_timeout: config[:timeout])
6777
request = [
6878
'select checkpoints_timed, checkpoints_req,',
6979
'buffers_checkpoint, buffers_clean,',

0 commit comments

Comments
 (0)