Skip to content

Commit f03f484

Browse files
committed
Merge pull request #5 from eheydrick/query_plugins
Add postgres query plugins
2 parents c76966e + 3747dc1 commit f03f484

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ 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+
### Changed
8+
- standardized headers
9+
10+
### Added
11+
- added plugins for querying postgres
712

813
## [0.0.5] - 2015-10-06
914
### Changed
1015
- updated pg gem to 0.18.3
11-
Added port cli option to postgres-graphite.rb.
16+
- Added port cli option to postgres-graphite.rb.
1217

1318
## [0.0.4] - 2015-08-04
1419
### Changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* bin/metric-postgres-connections.rb
2121
* bin/metric-postgres-locks.rb
2222
* bin/metric-postgres-statsio.rb
23+
* bin/check-postgres-query.rb
24+
* bin/metrics-postgres-query.rb
2325

2426
## Usage
2527

bin/check-postgres-query.rb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#! /usr/bin/env ruby
2+
#
3+
# check-postgres-query
4+
#
5+
# DESCRIPTION:
6+
# This plugin queries a PostgreSQL database. It alerts when the numeric
7+
# result hits a threshold. Can optionally alert on the number of tuples
8+
# (rows) returned by the query.
9+
#
10+
# OUTPUT:
11+
# plain-text
12+
#
13+
# PLATFORMS:
14+
# Linux
15+
#
16+
# DEPENDENCIES:
17+
# gem: pg
18+
# gem: sensu-plugin
19+
# gem: dentaku
20+
#
21+
# USAGE:
22+
# check-postgres-query.rb -u db_user -p db_pass -h db_host -d db -q 'select foo from bar' -w 'value > 5' -c 'value > 10'
23+
#
24+
# NOTES:
25+
#
26+
# LICENSE:
27+
# Copyright 2015, Eric Heydrick <eheydrick@gmail.com>
28+
# Released under the same terms as Sensu (the MIT license); see LICENSE
29+
# for details.
30+
#
31+
32+
require 'sensu-plugin/check/cli'
33+
require 'pg'
34+
require 'dentaku'
35+
36+
# Check PostgresSQL Query
37+
class CheckPostgresQuery < Sensu::Plugin::Check::CLI
38+
option :user,
39+
description: 'Postgres User',
40+
short: '-u USER',
41+
long: '--user USER'
42+
43+
option :password,
44+
description: 'Postgres Password',
45+
short: '-p PASS',
46+
long: '--password PASS'
47+
48+
option :hostname,
49+
description: 'Hostname to login to',
50+
short: '-h HOST',
51+
long: '--hostname HOST',
52+
default: 'localhost'
53+
54+
option :port,
55+
description: 'Database port',
56+
short: '-P PORT',
57+
long: '--port PORT',
58+
default: 5432
59+
60+
option :db,
61+
description: 'Database name',
62+
short: '-d DB',
63+
long: '--db DB',
64+
default: 'postgres'
65+
66+
option :query,
67+
description: 'Database query to execute',
68+
short: '-q QUERY',
69+
long: '--query QUERY',
70+
required: true
71+
72+
option :check_tuples,
73+
description: 'Check against the number of tuples (rows) returned by the query',
74+
short: '-t',
75+
long: '--tuples',
76+
boolean: true,
77+
default: false
78+
79+
option :warning,
80+
description: 'Warning threshold expression',
81+
short: '-w WARNING',
82+
long: '--warning WARNING',
83+
default: nil
84+
85+
option :critical,
86+
description: 'Critical threshold expression',
87+
short: '-c CRITICAL',
88+
long: '--critical CRITICAL',
89+
default: nil
90+
91+
def run
92+
begin
93+
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, config[:db], config[:user], config[:password])
94+
res = con.exec("#{config[:query]}")
95+
rescue PG::Error => e
96+
unknown "Unable to query PostgreSQL: #{e.message}"
97+
end
98+
99+
if config[:check_tuples]
100+
value = res.ntuples
101+
else
102+
value = res.first.values.first.to_f
103+
end
104+
105+
calc = Dentaku::Calculator.new
106+
if config[:critical] && calc.evaluate(config[:critical], value: value)
107+
critical "Results: #{res.values}"
108+
elsif config[:warning] && calc.evaluate(config[:warning], value: value)
109+
warning "Results: #{res.values}"
110+
else
111+
ok 'Query OK'
112+
end
113+
end
114+
end

bin/metrics-postgres-query.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#! /usr/bin/env ruby
2+
#
3+
# metrics-postgres-query
4+
#
5+
# DESCRIPTION:
6+
# This plugin collects metrics from the results of a postgres query. Can optionally
7+
# count the number of tuples (rows) returned by the query.
8+
#
9+
# OUTPUT:
10+
# metric data
11+
#
12+
# PLATFORMS:
13+
# Linux
14+
#
15+
# DEPENDENCIES:
16+
# gem: pg
17+
# gem: sensu-plugin
18+
#
19+
# USAGE:
20+
# metrics-postgres-query.rb -u db_user -p db_pass -h db_server -d db -q 'select foo from bar'
21+
#
22+
# NOTES:
23+
#
24+
# LICENSE:
25+
# Copyright 2015, Eric Heydrick <eheydrick@gmail.com>
26+
# Released under the same terms as Sensu (the MIT license); see LICENSE
27+
# for details.
28+
#
29+
30+
require 'sensu-plugin/metric/cli'
31+
require 'pg'
32+
33+
class MetricsPostgresQuery < Sensu::Plugin::Metric::CLI::Graphite
34+
option :user,
35+
description: 'Postgres User',
36+
short: '-u USER',
37+
long: '--user USER'
38+
39+
option :password,
40+
description: 'Postgres Password',
41+
short: '-p PASS',
42+
long: '--password PASS'
43+
44+
option :hostname,
45+
description: 'Hostname to login to',
46+
short: '-h HOST',
47+
long: '--hostname HOST',
48+
default: 'localhost'
49+
50+
option :port,
51+
description: 'Database port',
52+
short: '-P PORT',
53+
long: '--port PORT',
54+
default: 5432
55+
56+
option :db,
57+
description: 'Database name',
58+
short: '-d DB',
59+
long: '--db DB',
60+
default: 'postgres'
61+
62+
option :query,
63+
description: 'Database query to execute',
64+
short: '-q QUERY',
65+
long: '--query QUERY',
66+
required: true
67+
68+
option :count_tuples,
69+
description: 'Count the number of tuples (rows) returned by the query',
70+
short: '-t',
71+
long: '--tuples',
72+
boolean: true,
73+
default: false
74+
75+
option :scheme,
76+
description: 'Metric naming scheme, text to prepend to metric',
77+
short: '-s SCHEME',
78+
long: '--scheme SCHEME',
79+
default: 'postgres'
80+
81+
def run
82+
begin
83+
con = PG::Connection.new(config[:hostname], config[:port], nil, nil, config[:db], config[:user], config[:password])
84+
res = con.exec("#{config[:query]}")
85+
rescue PG::Error => e
86+
unknown "Unable to query PostgreSQL: #{e.message}"
87+
end
88+
89+
if config[:check_tuples]
90+
value = res.ntuples
91+
else
92+
value = res.first.values.first
93+
end
94+
95+
output config[:scheme], value
96+
ok
97+
end
98+
end

sensu-plugins-postgres.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Gem::Specification.new do |s|
4444

4545
s.add_runtime_dependency 'sensu-plugin', '1.2.0'
4646
s.add_runtime_dependency 'pg', '0.18.3'
47+
s.add_runtime_dependency 'dentaku', '2.0.4'
4748

4849
s.add_development_dependency 'bundler', '~> 1.7'
4950
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'

0 commit comments

Comments
 (0)