@@ -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 } #{ max_conns }  
113+       message  =  "PostgreSQL connections at #{ percent } #{ current_conns } #{ available_conns }  
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 } #{ max_conns }  
119+         ok  "PostgreSQL connections under threshold: #{ percent } #{ current_conns } #{ available_conns }  
118120      end 
119121    else 
120-       message  =  "PostgreSQL connections at #{ current_conns } #{ max_conns }  
122+       message  =  "PostgreSQL connections at #{ current_conns } #{ available_conns }  
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 } #{ max_conns }  
128+         ok  "PostgreSQL connections under threshold: #{ current_conns } #{ available_conns }  
127129      end 
128130    end 
129131  end 
0 commit comments