Skip to content

Commit 7964f0e

Browse files
Evesymajormoses
authored andcommitted
Account for reserved connections (#34)
* Take into account reserved connections
1 parent 5e0bff0 commit 7964f0e

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ 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+
### Fixed
8+
- Take into account reserved superuser connections in check-postgres-connections.rb
9+
710
### Added
811
- Ruby 2.4.1 testing
912

bin/check-postgres-connections.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def run
100100
port: config[:port],
101101
connect_timeout: config[:timeout])
102102
max_conns = con.exec('SHOW max_connections').getvalue(0, 0).to_i
103+
superuser_conns = con.exec('SHOW superuser_reserved_connections').getvalue(0, 0).to_i
104+
available_conns = max_conns - superuser_conns
103105
current_conns = con.exec('SELECT count(*) from pg_stat_activity').getvalue(0, 0).to_i
104106
rescue PG::Error => e
105107
unknown "Unable to query PostgreSQL: #{e.message}"
@@ -108,22 +110,22 @@ def run
108110
percent = (current_conns.to_f / max_conns.to_f * 100).to_i
109111

110112
if config[:use_percentage]
111-
message = "PostgreSQL connections at #{percent}%, #{current_conns} out of #{max_conns} connections"
113+
message = "PostgreSQL connections at #{percent}%, #{current_conns} out of #{available_conns} connections"
112114
if percent >= config[:critical]
113115
critical message
114116
elsif percent >= config[:warning]
115117
warning message
116118
else
117-
ok "PostgreSQL connections under threshold: #{percent}%, #{current_conns} out of #{max_conns} connections"
119+
ok "PostgreSQL connections under threshold: #{percent}%, #{current_conns} out of #{available_conns} connections"
118120
end
119121
else
120-
message = "PostgreSQL connections at #{current_conns} out of #{max_conns} connections"
122+
message = "PostgreSQL connections at #{current_conns} out of #{available_conns} connections"
121123
if current_conns >= config[:critical]
122124
critical message
123125
elsif current_conns >= config[:warning]
124126
warning message
125127
else
126-
ok "PostgreSQL connections under threshold: #{current_conns} out of #{max_conns} connections"
128+
ok "PostgreSQL connections under threshold: #{current_conns} out of #{available_conns} connections"
127129
end
128130
end
129131
end

0 commit comments

Comments
 (0)