|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# |
| 3 | +# check-postgres-connections |
| 4 | +# |
| 5 | +# DESCRIPTION: |
| 6 | +# This plugin checks the number of connections to a postgresql database and |
| 7 | +# alerts when the number or percent crosses a threshold. Defaults to checking |
| 8 | +# number of connections unless the --percentage flag is used in which case the |
| 9 | +# percentage of max connections is checked. |
| 10 | +# |
| 11 | +# OUTPUT: |
| 12 | +# plain-text |
| 13 | +# |
| 14 | +# PLATFORMS: |
| 15 | +# Linux |
| 16 | +# |
| 17 | +# DEPENDENCIES: |
| 18 | +# gem: pg |
| 19 | +# gem: sensu-plugin |
| 20 | +# |
| 21 | +# USAGE: |
| 22 | +# # warn when connections hit 250, critical when 400 |
| 23 | +# check-postgres-connections.rb -u db_user -p db_pass -h db_host -d db -w 250 -c 400 |
| 24 | +# # warn when connections hit 80%, critical when 95% |
| 25 | +# check-postgres-connections.rb -u db_user -p db_pass -h db_host -d db -w 80 -c 95 --percentage |
| 26 | +# |
| 27 | +# NOTES: |
| 28 | +# |
| 29 | +# LICENSE: |
| 30 | +# Copyright 2016, Eric Heydrick <eheydrick@gmail.com> |
| 31 | +# Released under the same terms as Sensu (the MIT license); see LICENSE |
| 32 | +# for details. |
| 33 | +# |
| 34 | + |
| 35 | +require 'sensu-plugin/check/cli' |
| 36 | +require 'pg' |
| 37 | + |
| 38 | +class CheckPostgresConnections < Sensu::Plugin::Check::CLI |
| 39 | + option :user, |
| 40 | + description: 'Postgres User', |
| 41 | + short: '-u USER', |
| 42 | + long: '--user USER' |
| 43 | + |
| 44 | + option :password, |
| 45 | + description: 'Postgres Password', |
| 46 | + short: '-p PASS', |
| 47 | + long: '--password PASS' |
| 48 | + |
| 49 | + option :hostname, |
| 50 | + description: 'Hostname to login to', |
| 51 | + short: '-h HOST', |
| 52 | + long: '--hostname HOST', |
| 53 | + default: 'localhost' |
| 54 | + |
| 55 | + option :port, |
| 56 | + description: 'Database port', |
| 57 | + short: '-P PORT', |
| 58 | + long: '--port PORT', |
| 59 | + default: 5432 |
| 60 | + |
| 61 | + option :db, |
| 62 | + description: 'Database name', |
| 63 | + short: '-d DB', |
| 64 | + long: '--db DB', |
| 65 | + default: 'postgres' |
| 66 | + |
| 67 | + option :warning, |
| 68 | + description: 'Warning threshold number or % of connections. (default: 200 connections)', |
| 69 | + short: '-w WARNING', |
| 70 | + long: '--warning CRITICAL', |
| 71 | + default: 200, |
| 72 | + proc: proc(&:to_i) |
| 73 | + |
| 74 | + option :critical, |
| 75 | + description: 'Critical threshold number or % of connections. (default: 250 connections)', |
| 76 | + short: '-c CRITICAL', |
| 77 | + long: '--critical CRITICAL', |
| 78 | + default: 250, |
| 79 | + proc: proc(&:to_i) |
| 80 | + |
| 81 | + option :use_percentage, |
| 82 | + description: 'Use percentage of max connections used instead of the absolute number of connections', |
| 83 | + short: '-a', |
| 84 | + long: '--percentage', |
| 85 | + boolean: true, |
| 86 | + default: false |
| 87 | + |
| 88 | + def run |
| 89 | + begin |
| 90 | + con = PG::Connection.new(config[:hostname], config[:port], nil, nil, config[:db], config[:user], config[:password]) |
| 91 | + max_conns = con.exec('SHOW max_connections').getvalue(0, 0).to_i |
| 92 | + current_conns = con.exec('SELECT count(*) from pg_stat_activity').getvalue(0, 0).to_i |
| 93 | + rescue PG::Error => e |
| 94 | + unknown "Unable to query PostgreSQL: #{e.message}" |
| 95 | + end |
| 96 | + |
| 97 | + percent = (current_conns.to_f / max_conns.to_f * 100).to_i |
| 98 | + |
| 99 | + if config[:use_percentage] |
| 100 | + message = "PostgreSQL connections at #{percent}%, #{current_conns} out of #{max_conns} connections" |
| 101 | + if percent >= config[:warning] |
| 102 | + warning message |
| 103 | + elsif percent >= config[:critical] |
| 104 | + critical message |
| 105 | + else |
| 106 | + ok "PostgreSQL connections under threshold: #{percent}%, #{current_conns} out of #{max_conns} connections" |
| 107 | + end |
| 108 | + else |
| 109 | + message = "PostgreSQL connections at #{current_conns} out of #{max_conns} connections" |
| 110 | + if current_conns >= config[:warning] |
| 111 | + warning message |
| 112 | + elsif current_conns >= config[:critical] |
| 113 | + critical message |
| 114 | + else |
| 115 | + ok "PostgreSQL connections under threshold: #{current_conns} out of #{max_conns} connections" |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments