@@ -100,6 +100,8 @@ def run
100
100
port : config [ :port ] ,
101
101
connect_timeout : config [ :timeout ] )
102
102
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
103
105
current_conns = con . exec ( 'SELECT count(*) from pg_stat_activity' ) . getvalue ( 0 , 0 ) . to_i
104
106
rescue PG ::Error => e
105
107
unknown "Unable to query PostgreSQL: #{ e . message } "
@@ -108,22 +110,22 @@ def run
108
110
percent = ( current_conns . to_f / max_conns . to_f * 100 ) . to_i
109
111
110
112
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"
112
114
if percent >= config [ :critical ]
113
115
critical message
114
116
elsif percent >= config [ :warning ]
115
117
warning message
116
118
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"
118
120
end
119
121
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"
121
123
if current_conns >= config [ :critical ]
122
124
critical message
123
125
elsif current_conns >= config [ :warning ]
124
126
warning message
125
127
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"
127
129
end
128
130
end
129
131
end
0 commit comments