|
1 | 1 | #!/usr/bin/env ruby
|
| 2 | +# frozen_string_literal: true |
2 | 3 |
|
3 |
| -# Reports PostgreSQL statistics to Riemann. |
4 |
| -# http://www.postgresql.org/docs/9.2/static/monitoring-stats.html |
| 4 | +Process.setproctitle($PROGRAM_NAME) |
5 | 5 |
|
6 |
| -require 'riemann/tools' |
| 6 | +require 'riemann/tools/postgresql' |
7 | 7 |
|
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 |
0 commit comments