Skip to content

Commit a859493

Browse files
committed
Merge pull request #10 from eheydrick/check_conns
Add new plugin for checking postgres connections
2 parents 3b5c1c4 + 12edd15 commit a859493

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
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+
### Added
8+
- Add new plugin `check-postgres-connections` that checks the number of connections to a DB
79

810
## [0.0.7] - 2015-12-10
911
### Changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Sensu-Plugins-postgres
22

3-
[ ![Build Status](https://travis-ci.org/sensu-plugins/sensu-plugins-postgres.svg?branch=master)](https://travis-ci.org/sensu-plugins/sensu-plugins-postgres)
3+
[![Build Status](https://travis-ci.org/sensu-plugins/sensu-plugins-postgres.svg?branch=master)](https://travis-ci.org/sensu-plugins/sensu-plugins-postgres)
44
[![Gem Version](https://badge.fury.io/rb/sensu-plugins-postgres.svg)](http://badge.fury.io/rb/sensu-plugins-postgres)
55
[![Code Climate](https://codeclimate.com/github/sensu-plugins/sensu-plugins-postgres/badges/gpa.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-postgres)
66
[![Test Coverage](https://codeclimate.com/github/sensu-plugins/sensu-plugins-postgres/badges/coverage.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-postgres)
@@ -10,6 +10,7 @@
1010

1111
## Files
1212
* bin/check-postgres-alive.rb
13+
* bin/check-postgres-connections.rb
1314
* bin/metric-postgres-dbsize.rb
1415
* bin/metric-postgres-statsbgwriter.rb
1516
* bin/metric-postgres-statstable.rb

bin/check-postgres-connections.rb

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)