Skip to content

Commit d8a659d

Browse files
committed
Split the run code from the actual logic
This allows us to run this tool along other tools in a single process using riemann-wrapper.
1 parent f7dfaeb commit d8a659d

File tree

2 files changed

+87
-77
lines changed

2 files changed

+87
-77
lines changed

bin/riemann-postgresql

+4-77
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,8 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

3-
# Reports PostgreSQL statistics to Riemann.
4-
# http://www.postgresql.org/docs/9.2/static/monitoring-stats.html
4+
Process.setproctitle($PROGRAM_NAME)
55

6-
require 'riemann/tools'
6+
require 'riemann/tools/postgresql'
77

8-
class Riemann::Tools::PostgreSQL
9-
include Riemann::Tools
10-
require 'pg'
11-
12-
opt :postgresql_host, "PostgreSQL Server Hostname", :type => String, :default => "localhost"
13-
opt :postgresql_port, "PostgreSQL Server Port", :default => 5432
14-
opt :postgresql_username, "Authenticated username", :type => String, :default => "postgres"
15-
opt :postgresql_password, "User's password", :type => String, :default => "postgres"
16-
opt :postgresql_database, "Database to connect", :type => String, :default => "postgres"
17-
18-
def initialize
19-
begin
20-
@conn = PG.connect(:host => opts[:postgresql_host],
21-
:port => opts[:postgresql_port],
22-
:user => opts[:postgresql_username],
23-
:password => opts[:postgresql_password],
24-
:dbname => opts[:postgresql_database])
25-
rescue
26-
PG::Error
27-
puts "Error: Unable to connect with PostgreSQL server."
28-
exit 1
29-
end
30-
end
31-
32-
def tick
33-
@conn.transaction do
34-
35-
# General DB statistics.
36-
@conn.exec("DECLARE general CURSOR FOR SELECT pg_stat_database.*, pg_database_size \
37-
(pg_database.datname) AS size FROM pg_database JOIN pg_stat_database ON \
38-
pg_database.datname = pg_stat_database.datname WHERE pg_stat_database.datname \
39-
NOT IN ('template0', 'template1', 'postgres')")
40-
41-
result = @conn.exec("FETCH ALL IN general")
42-
43-
keys = result.fields.collect.to_a
44-
result.values.collect do |row|
45-
vals = row.collect.to_a
46-
vals.each_with_index {|val, index|
47-
if index > 1
48-
report(
49-
:host => opts[:postgresql_host].dup,
50-
:service => "DB #{vals[1]} #{keys[index]}",
51-
:metric => vals[index].to_f,
52-
:state => 'ok',
53-
:description => "PostgreSQL DB #{keys[index]}".gsub("_", " "),
54-
:tags => ['postgresql']
55-
)
56-
end
57-
}
58-
end
59-
60-
# Each DB specific connection counts.
61-
@conn.exec("DECLARE connection CURSOR FOR SELECT datname, count(datname) FROM pg_stat_activity \
62-
GROUP BY pg_stat_activity.datname")
63-
64-
result = @conn.exec("FETCH ALL in connection")
65-
result.values.collect do |row|
66-
vals = row.collect.to_a
67-
report(
68-
:host => opts[:postgresql_host].dup,
69-
:service => "DB #{vals[0]} connections",
70-
:metric => vals[1].to_f,
71-
:state => 'ok',
72-
:description => "PostgreSQL DB Connections",
73-
:tags => ['postgresql']
74-
)
75-
end
76-
77-
end
78-
end
79-
end
80-
81-
Riemann::Tools::PostgreSQL.run
8+
Riemann::Tools::Postgresql.run

lib/riemann/tools/postgresql.rb

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
require 'riemann/tools'
4+
5+
# Reports PostgreSQL statistics to Riemann.
6+
# http://www.postgresql.org/docs/9.2/static/monitoring-stats.html
7+
8+
module Riemann
9+
module Tools
10+
class Postgresql
11+
include Riemann::Tools
12+
require 'pg'
13+
14+
opt :postgresql_host, "PostgreSQL Server Hostname", :type => String, :default => "localhost"
15+
opt :postgresql_port, "PostgreSQL Server Port", :default => 5432
16+
opt :postgresql_username, "Authenticated username", :type => String, :default => "postgres"
17+
opt :postgresql_password, "User's password", :type => String, :default => "postgres"
18+
opt :postgresql_database, "Database to connect", :type => String, :default => "postgres"
19+
20+
def initialize
21+
begin
22+
@conn = PG.connect(:host => opts[:postgresql_host],
23+
:port => opts[:postgresql_port],
24+
:user => opts[:postgresql_username],
25+
:password => opts[:postgresql_password],
26+
:dbname => opts[:postgresql_database])
27+
rescue
28+
PG::Error
29+
puts "Error: Unable to connect with PostgreSQL server."
30+
exit 1
31+
end
32+
end
33+
34+
def tick
35+
@conn.transaction do
36+
37+
# General DB statistics.
38+
@conn.exec("DECLARE general CURSOR FOR SELECT pg_stat_database.*, pg_database_size \
39+
(pg_database.datname) AS size FROM pg_database JOIN pg_stat_database ON \
40+
pg_database.datname = pg_stat_database.datname WHERE pg_stat_database.datname \
41+
NOT IN ('template0', 'template1', 'postgres')")
42+
43+
result = @conn.exec("FETCH ALL IN general")
44+
45+
keys = result.fields.collect.to_a
46+
result.values.collect do |row|
47+
vals = row.collect.to_a
48+
vals.each_with_index {|val, index|
49+
if index > 1
50+
report(
51+
:host => opts[:postgresql_host].dup,
52+
:service => "DB #{vals[1]} #{keys[index]}",
53+
:metric => vals[index].to_f,
54+
:state => 'ok',
55+
:description => "PostgreSQL DB #{keys[index]}".gsub("_", " "),
56+
:tags => ['postgresql']
57+
)
58+
end
59+
}
60+
end
61+
62+
# Each DB specific connection counts.
63+
@conn.exec("DECLARE connection CURSOR FOR SELECT datname, count(datname) FROM pg_stat_activity \
64+
GROUP BY pg_stat_activity.datname")
65+
66+
result = @conn.exec("FETCH ALL in connection")
67+
result.values.collect do |row|
68+
vals = row.collect.to_a
69+
report(
70+
:host => opts[:postgresql_host].dup,
71+
:service => "DB #{vals[0]} connections",
72+
:metric => vals[1].to_f,
73+
:state => 'ok',
74+
:description => "PostgreSQL DB Connections",
75+
:tags => ['postgresql']
76+
)
77+
end
78+
79+
end
80+
end
81+
end
82+
end
83+
end

0 commit comments

Comments
 (0)