From 01474e17afa5d86ee3976f0907d188a69f3361af Mon Sep 17 00:00:00 2001 From: Florian Schwab Date: Tue, 30 Apr 2019 13:41:18 +0200 Subject: [PATCH 1/6] allow to get metrics for multiple databases --- bin/metric-postgres-connections.rb | 54 +++++++++++++---------- bin/metric-postgres-dbsize.rb | 37 +++++++++------- bin/metric-postgres-locks.rb | 51 ++++++++++++--------- bin/metric-postgres-statsio.rb | 50 +++++++++++++-------- bin/metric-postgres-statstable.rb | 54 ++++++++++++++--------- lib/sensu-plugins-postgres/pgdatabases.rb | 20 +++++++++ 6 files changed, 166 insertions(+), 100 deletions(-) create mode 100644 lib/sensu-plugins-postgres/pgdatabases.rb diff --git a/bin/metric-postgres-connections.rb b/bin/metric-postgres-connections.rb index 068bd11..8d859c8 100755 --- a/bin/metric-postgres-connections.rb +++ b/bin/metric-postgres-connections.rb @@ -29,6 +29,7 @@ # require 'sensu-plugins-postgres/pgpass' +require 'sensu-plugins-postgres/pgdatabases' require 'sensu-plugin/metric/cli' require 'pg' require 'socket' @@ -60,10 +61,11 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite short: '-P PORT', long: '--port PORT' - option :database, - description: 'Database name', + option :databases, + description: 'Database names, separated by ";"', short: '-d DB', - long: '--db DB' + long: '--db DB', + default: nil option :scheme, description: 'Metric naming scheme, text to prepend to $queue_name.$metric', @@ -77,12 +79,14 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite default: nil include Pgpass + include Pgdatabases def run timestamp = Time.now.to_i pgpass + databases = pgdatabases con = PG.connect(host: config[:hostname], - dbname: config[:database], + dbname: databases.first, user: config[:user], password: config[:password], port: config[:port], @@ -96,30 +100,32 @@ def run ] wait_col = con.exec(request.join(' ')).first['wait_col'] - request = [ - "select count(*), #{wait_col} as waiting from pg_stat_activity", - "where datname = '#{config[:database]}' group by #{wait_col}" - ] - - metrics = { - active: 0, - waiting: 0, - total: 0 - } - con.exec(request.join(' ')) do |result| - result.each do |row| - if row['waiting'] == 't' - metrics[:waiting] = row['count'] - elsif row['waiting'] == 'f' - metrics[:active] = row['count'] + databases.each do |database| + request = [ + "select count(*), #{wait_col} as waiting from pg_stat_activity", + "where datname = '#{database}' group by #{wait_col}" + ] + + metrics = { + active: 0, + waiting: 0, + total: 0 + } + con.exec(request.join(' ')) do |result| + result.each do |row| + if row['waiting'] == 't' + metrics[:waiting] = row['count'] + elsif row['waiting'] == 'f' + metrics[:active] = row['count'] + end end end - end - metrics[:total] = (metrics[:waiting].to_i + metrics[:active].to_i) + metrics[:total] = (metrics[:waiting].to_i + metrics[:active].to_i) - metrics.each do |metric, value| - output "#{config[:scheme]}.connections.#{config[:database]}.#{metric}", value, timestamp + metrics.each do |metric, value| + output "#{config[:scheme]}.connections.#{database}.#{metric}", value, timestamp + end end ok diff --git a/bin/metric-postgres-dbsize.rb b/bin/metric-postgres-dbsize.rb index 3e94417..1dd25c6 100755 --- a/bin/metric-postgres-dbsize.rb +++ b/bin/metric-postgres-dbsize.rb @@ -29,6 +29,7 @@ # require 'sensu-plugins-postgres/pgpass' +require 'sensu-plugins-postgres/pgdatabases' require 'sensu-plugin/metric/cli' require 'pg' require 'socket' @@ -60,10 +61,11 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite short: '-P PORT', long: '--port PORT' - option :database, - description: 'Database name', + option :databases, + description: 'Database names, separated by ";"', short: '-d DB', - long: '--db DB' + long: '--db DB', + default: nil option :scheme, description: 'Metric naming scheme, text to prepend to $queue_name.$metric', @@ -77,23 +79,28 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite default: nil include Pgpass + include Pgdatabases def run timestamp = Time.now.to_i pgpass - con = PG.connect(host: config[:hostname], - dbname: config[:database], - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) - request = [ - "select pg_database_size('#{config[:database]}')" - ] + databases = pgdatabases + con = PG.connect(host: config[:hostname], + dbname: databases.first, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) + + databases.each do |database| + request = [ + "select pg_database_size('#{database}')" + ] - con.exec(request.join(' ')) do |result| - result.each do |row| - output "#{config[:scheme]}.size.#{config[:database]}", row['pg_database_size'], timestamp + con.exec(request.join(' ')) do |result| + result.each do |row| + output "#{config[:scheme]}.size.#{database}", row['pg_database_size'], timestamp + end end end diff --git a/bin/metric-postgres-locks.rb b/bin/metric-postgres-locks.rb index 869a841..62626e6 100755 --- a/bin/metric-postgres-locks.rb +++ b/bin/metric-postgres-locks.rb @@ -29,6 +29,7 @@ # require 'sensu-plugins-postgres/pgpass' +require 'sensu-plugins-postgres/pgdatabases' require 'sensu-plugin/metric/cli' require 'pg' require 'socket' @@ -60,10 +61,11 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite short: '-P PORT', long: '--port PORT' - option :database, - description: 'Database name', + option :databases, + description: 'Database names, separated by ";"', short: '-d DB', - long: '--db DB' + long: '--db DB', + default: nil option :scheme, description: 'Metric naming scheme, text to prepend to $queue_name.$metric', @@ -77,33 +79,38 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite default: nil include Pgpass + include Pgdatabases def run timestamp = Time.now.to_i locks_per_type = Hash.new(0) pgpass - con = PG.connect(host: config[:hostname], - dbname: config[:database], - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) - request = [ - 'SELECT mode, count(mode) AS count FROM pg_locks', - "WHERE database = (SELECT oid FROM pg_database WHERE datname = '#{config[:database]}')", - 'GROUP BY mode' - ] - - con.exec(request.join(' ')) do |result| - result.each do |row| - lock_name = row['mode'].downcase.to_sym - locks_per_type[lock_name] = row['count'] + databases = pgdatabases + con = PG.connect(host: config[:hostname], + dbname: databases.first, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) + + databases.each do |database| + request = [ + 'SELECT mode, count(mode) AS count FROM pg_locks', + "WHERE database = (SELECT oid FROM pg_database WHERE datname = '#{database}')", + 'GROUP BY mode' + ] + + con.exec(request.join(' ')) do |result| + result.each do |row| + lock_name = row['mode'].downcase.to_sym + locks_per_type[lock_name] = row['count'] + end end - end - locks_per_type.each do |lock_type, count| - output "#{config[:scheme]}.locks.#{config[:database]}.#{lock_type}", count, timestamp + locks_per_type.each do |lock_type, count| + output "#{config[:scheme]}.locks.#{database}.#{lock_type}", count, timestamp + end end ok diff --git a/bin/metric-postgres-statsio.rb b/bin/metric-postgres-statsio.rb index 41a838b..5418d2a 100755 --- a/bin/metric-postgres-statsio.rb +++ b/bin/metric-postgres-statsio.rb @@ -30,6 +30,7 @@ # require 'sensu-plugins-postgres/pgpass' +require 'sensu-plugins-postgres/pgdatabases' require 'sensu-plugin/metric/cli' require 'pg' require 'socket' @@ -61,10 +62,11 @@ class PostgresStatsIOMetrics < Sensu::Plugin::Metric::CLI::Graphite short: '-P PORT', long: '--port PORT' - option :database, - description: 'Database name', + option :databases, + description: 'Database names, separated by ";"', short: '-d DB', - long: '--db DB' + long: '--db DB', + default: nil option :scope, description: 'Scope, see http://www.postgresql.org/docs/9.2/static/monitoring-stats.html', @@ -84,16 +86,28 @@ class PostgresStatsIOMetrics < Sensu::Plugin::Metric::CLI::Graphite default: nil include Pgpass + include Pgdatabases def run timestamp = Time.now.to_i pgpass - con = PG.connect(host: config[:hostname], - dbname: config[:database], - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) + + pgdatabases.each do |database| + output_database(database, timestamp) + end + + ok + end + + private + + def output_database(database) + con = PG.connect(host: config[:hostname], + dbname: database, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) request = [ 'select sum(heap_blks_read) as heap_blks_read, sum(heap_blks_hit) as heap_blks_hit,', 'sum(idx_blks_read) as idx_blks_read, sum(idx_blks_hit) as idx_blks_hit,', @@ -103,17 +117,17 @@ def run ] con.exec(request.join(' ')) do |result| result.each do |row| - output "#{config[:scheme]}.statsio.#{config[:database]}.heap_blks_read", row['heap_blks_read'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.heap_blks_hit", row['heap_blks_hit'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.idx_blks_read", row['idx_blks_read'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.idx_blks_hit", row['idx_blks_hit'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.toast_blks_read", row['toast_blks_read'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.toast_blks_hit", row['toast_blks_hit'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.tidx_blks_read", row['tidx_blks_read'], timestamp - output "#{config[:scheme]}.statsio.#{config[:database]}.tidx_blks_hit", row['tidx_blks_hit'], timestamp + output "#{config[:scheme]}.statsio.#{database}.heap_blks_read", row['heap_blks_read'], timestamp + output "#{config[:scheme]}.statsio.#{database}.heap_blks_hit", row['heap_blks_hit'], timestamp + output "#{config[:scheme]}.statsio.#{database}.idx_blks_read", row['idx_blks_read'], timestamp + output "#{config[:scheme]}.statsio.#{database}.idx_blks_hit", row['idx_blks_hit'], timestamp + output "#{config[:scheme]}.statsio.#{database}.toast_blks_read", row['toast_blks_read'], timestamp + output "#{config[:scheme]}.statsio.#{database}.toast_blks_hit", row['toast_blks_hit'], timestamp + output "#{config[:scheme]}.statsio.#{database}.tidx_blks_read", row['tidx_blks_read'], timestamp + output "#{config[:scheme]}.statsio.#{database}.tidx_blks_hit", row['tidx_blks_hit'], timestamp end end - ok + con.close end end diff --git a/bin/metric-postgres-statstable.rb b/bin/metric-postgres-statstable.rb index a4aab16..caaf569 100755 --- a/bin/metric-postgres-statstable.rb +++ b/bin/metric-postgres-statstable.rb @@ -30,6 +30,7 @@ # require 'sensu-plugins-postgres/pgpass' +require 'sensu-plugins-postgres/pgdatabases' require 'sensu-plugin/metric/cli' require 'pg' require 'socket' @@ -61,10 +62,11 @@ class PostgresStatsTableMetrics < Sensu::Plugin::Metric::CLI::Graphite short: '-P PORT', long: '--port PORT' - option :database, - description: 'Database name', + option :databases, + description: 'Database names, separated by ";"', short: '-d DB', - long: '--db DB' + long: '--db DB', + default: nil option :scope, description: 'Scope, see http://www.postgresql.org/docs/9.2/static/monitoring-stats.html', @@ -84,16 +86,28 @@ class PostgresStatsTableMetrics < Sensu::Plugin::Metric::CLI::Graphite default: nil include Pgpass + include Pgdatabases def run timestamp = Time.now.to_i pgpass - con = PG.connect(host: config[:hostname], - dbname: config[:database], - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) + + pgdatabases.each do |database| + output_database(database, timestamp) + end + + ok + end + + private + + def output_database(database) + con = PG.connect(host: config[:hostname], + dbname: database, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) request = [ 'select sum(seq_scan) as seq_scan, sum(seq_tup_read) as seq_tup_read,', 'sum(idx_scan) as idx_scan, sum(idx_tup_fetch) as idx_tup_fetch,', @@ -103,19 +117,17 @@ def run ] con.exec(request.join(' ')) do |result| result.each do |row| - output "#{config[:scheme]}.statstable.#{config[:database]}.seq_scan", row['seq_scan'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.seq_tup_read", row['seq_tup_read'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.idx_scan", row['idx_scan'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.idx_tup_fetch", row['idx_tup_fetch'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.n_tup_ins", row['n_tup_ins'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.n_tup_upd", row['n_tup_upd'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.n_tup_del", row['n_tup_del'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.n_tup_hot_upd", row['n_tup_hot_upd'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.n_live_tup", row['n_live_tup'], timestamp - output "#{config[:scheme]}.statstable.#{config[:database]}.n_dead_tup", row['n_dead_tup'], timestamp + output "#{config[:scheme]}.statstable.#{database}.seq_scan", row['seq_scan'], timestamp + output "#{config[:scheme]}.statstable.#{database}.seq_tup_read", row['seq_tup_read'], timestamp + output "#{config[:scheme]}.statstable.#{database}.idx_scan", row['idx_scan'], timestamp + output "#{config[:scheme]}.statstable.#{database}.idx_tup_fetch", row['idx_tup_fetch'], timestamp + output "#{config[:scheme]}.statstable.#{database}.n_tup_ins", row['n_tup_ins'], timestamp + output "#{config[:scheme]}.statstable.#{database}.n_tup_upd", row['n_tup_upd'], timestamp + output "#{config[:scheme]}.statstable.#{database}.n_tup_del", row['n_tup_del'], timestamp + output "#{config[:scheme]}.statstable.#{database}.n_tup_hot_upd", row['n_tup_hot_upd'], timestamp + output "#{config[:scheme]}.statstable.#{database}.n_live_tup", row['n_live_tup'], timestamp + output "#{config[:scheme]}.statstable.#{database}.n_dead_tup", row['n_dead_tup'], timestamp end end - - ok end end diff --git a/lib/sensu-plugins-postgres/pgdatabases.rb b/lib/sensu-plugins-postgres/pgdatabases.rb new file mode 100644 index 0000000..a4d0680 --- /dev/null +++ b/lib/sensu-plugins-postgres/pgdatabases.rb @@ -0,0 +1,20 @@ +module Pgdatabases + def pgdatabases + if config[:databases].nil? + con = PG.connect(host: config[:hostname], + dbname: config[:database], + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) + + dbs = con.exec('SELECT datname FROM pg_database').map { |r| r['datname'] } - %w[template0] + + con.close + + dbs + else + config[:databases].split(';').map(&:strip) + end + end +end From fd1a376335f4475b43f3eadb985823aef6c9a41e Mon Sep 17 00:00:00 2001 From: Florian Schwab Date: Tue, 30 Apr 2019 13:50:14 +0200 Subject: [PATCH 2/6] * add usage examples for multiple databases * add info to changelog --- CHANGELOG.md | 4 ++++ bin/metric-postgres-connections.rb | 1 + bin/metric-postgres-dbsize.rb | 1 + bin/metric-postgres-locks.rb | 1 + bin/metric-postgres-statsdb.rb | 1 + bin/metric-postgres-statsio.rb | 1 + bin/metric-postgres-statstable.rb | 1 + 7 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95a22d1..2e7b0df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md). ## [Unreleased] +### Changed +- Option `database (-d, --db)` for metrics now supports semicolon separated list of databases to fetch metrics of multiple databases at once. +### Breaking Changes +- If no `database (-d, --db)` argument is supplied for the metric bins through the command line now metrics for all databases are returned. ## [2.3.2] - 2019-03-12 ### Fixed diff --git a/bin/metric-postgres-connections.rb b/bin/metric-postgres-connections.rb index 8d859c8..de760eb 100755 --- a/bin/metric-postgres-connections.rb +++ b/bin/metric-postgres-connections.rb @@ -18,6 +18,7 @@ # # USAGE: # ./metric-postgres-connections.rb -u db_user -p db_pass -h db_host -d db +# ./metric-postgres-connections.rb -u db_user -p db_pass -h db_host -d 'db1;db2' # # NOTES: # diff --git a/bin/metric-postgres-dbsize.rb b/bin/metric-postgres-dbsize.rb index 1dd25c6..2ddafc9 100755 --- a/bin/metric-postgres-dbsize.rb +++ b/bin/metric-postgres-dbsize.rb @@ -18,6 +18,7 @@ # # USAGE: # ./metric-postgres-dbsize.rb -u db_user -p db_pass -h db_host -d db +# ./metric-postgres-dbsize.rb -u db_user -p db_pass -h db_host -d 'db1;db2' # # NOTES: # diff --git a/bin/metric-postgres-locks.rb b/bin/metric-postgres-locks.rb index 62626e6..a54bc3e 100755 --- a/bin/metric-postgres-locks.rb +++ b/bin/metric-postgres-locks.rb @@ -18,6 +18,7 @@ # # USAGE: # ./metric-postgres-locks.rb -u db_user -p db_pass -h db_host -d db +# ./metric-postgres-locks.rb -u db_user -p db_pass -h db_host -d 'db1;db2' # # NOTES: # diff --git a/bin/metric-postgres-statsdb.rb b/bin/metric-postgres-statsdb.rb index 9a0560f..75953df 100755 --- a/bin/metric-postgres-statsdb.rb +++ b/bin/metric-postgres-statsdb.rb @@ -18,6 +18,7 @@ # # USAGE: # ./metric-postgres-statsdb.rb -u db_user -p db_pass -h db_host -d db +# ./metric-postgres-statsdb.rb -u db_user -p db_pass -h db_host -d 'db1;db2;db3' # # NOTES: # Requires PSQL `track_counts` `track_io_timing` for some metrics enabled diff --git a/bin/metric-postgres-statsio.rb b/bin/metric-postgres-statsio.rb index 5418d2a..69f754b 100755 --- a/bin/metric-postgres-statsio.rb +++ b/bin/metric-postgres-statsio.rb @@ -18,6 +18,7 @@ # # USAGE: # ./metric-postgres-statsio.rb -u db_user -p db_pass -h db_host -d db -s scope +# ./metric-postgres-statsio.rb -u db_user -p db_pass -h db_host -d 'db1;db2' -s scope # # NOTES: # Requires PSQL `track_io_timing` enabled diff --git a/bin/metric-postgres-statstable.rb b/bin/metric-postgres-statstable.rb index caaf569..274dcdb 100755 --- a/bin/metric-postgres-statstable.rb +++ b/bin/metric-postgres-statstable.rb @@ -18,6 +18,7 @@ # # USAGE: # ./metric-postgres-statstable.rb -u db_user -p db_pass -h db_host -d db -s scope +# ./metric-postgres-statstable.rb -u db_user -p db_pass -h db_host -d 'db1;db2' -s scope # # NOTES: # Requires PSQL `track_counts` enabled From 3e2aae470437fe598e012e8f3b70068f36fb7c0d Mon Sep 17 00:00:00 2001 From: Florian Schwab Date: Tue, 14 Jan 2020 14:11:20 +0100 Subject: [PATCH 3/6] * use "," as list separator for database names * add escaping of query parameters where possible --- bin/metric-postgres-connections.rb | 18 +++++++++--------- bin/metric-postgres-dbsize.rb | 22 +++++++++------------- bin/metric-postgres-locks.rb | 21 +++++++++++---------- bin/metric-postgres-statsbgwriter.rb | 12 ++++++------ bin/metric-postgres-statsio.rb | 4 ++-- bin/metric-postgres-statstable.rb | 4 ++-- lib/sensu-plugins-postgres/pgdatabases.rb | 2 +- 7 files changed, 40 insertions(+), 43 deletions(-) diff --git a/bin/metric-postgres-connections.rb b/bin/metric-postgres-connections.rb index de760eb..14b1cb6 100755 --- a/bin/metric-postgres-connections.rb +++ b/bin/metric-postgres-connections.rb @@ -18,7 +18,7 @@ # # USAGE: # ./metric-postgres-connections.rb -u db_user -p db_pass -h db_host -d db -# ./metric-postgres-connections.rb -u db_user -p db_pass -h db_host -d 'db1;db2' +# ./metric-postgres-connections.rb -u db_user -p db_pass -h db_host -d 'db1,db2' # # NOTES: # @@ -63,7 +63,7 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite long: '--port PORT' option :databases, - description: 'Database names, separated by ";"', + description: 'Database names, separated by ","', short: '-d DB', long: '--db DB', default: nil @@ -86,12 +86,12 @@ def run timestamp = Time.now.to_i pgpass databases = pgdatabases - con = PG.connect(host: config[:hostname], - dbname: databases.first, - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) + con = PG.connect(host: config[:hostname], + dbname: databases.first, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) request = [ "select case when count(*) = 1 then 'waiting' else", "'case when wait_event_type is null then false else true end' end as wait_col", @@ -104,7 +104,7 @@ def run databases.each do |database| request = [ "select count(*), #{wait_col} as waiting from pg_stat_activity", - "where datname = '#{database}' group by #{wait_col}" + "where datname = '#{con.escape_string(database)}' group by #{wait_col}" ] metrics = { diff --git a/bin/metric-postgres-dbsize.rb b/bin/metric-postgres-dbsize.rb index 2ddafc9..0dd188b 100755 --- a/bin/metric-postgres-dbsize.rb +++ b/bin/metric-postgres-dbsize.rb @@ -18,7 +18,7 @@ # # USAGE: # ./metric-postgres-dbsize.rb -u db_user -p db_pass -h db_host -d db -# ./metric-postgres-dbsize.rb -u db_user -p db_pass -h db_host -d 'db1;db2' +# ./metric-postgres-dbsize.rb -u db_user -p db_pass -h db_host -d 'db1,db2' # # NOTES: # @@ -63,7 +63,7 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite long: '--port PORT' option :databases, - description: 'Database names, separated by ";"', + description: 'Database names, separated by ","', short: '-d DB', long: '--db DB', default: nil @@ -86,19 +86,15 @@ def run timestamp = Time.now.to_i pgpass databases = pgdatabases - con = PG.connect(host: config[:hostname], - dbname: databases.first, - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) + con = PG.connect(host: config[:hostname], + dbname: databases.first, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) databases.each do |database| - request = [ - "select pg_database_size('#{database}')" - ] - - con.exec(request.join(' ')) do |result| + con.exec_params('select pg_database_size($1)', [database]) do |result| result.each do |row| output "#{config[:scheme]}.size.#{database}", row['pg_database_size'], timestamp end diff --git a/bin/metric-postgres-locks.rb b/bin/metric-postgres-locks.rb index a54bc3e..2e49f57 100755 --- a/bin/metric-postgres-locks.rb +++ b/bin/metric-postgres-locks.rb @@ -18,7 +18,7 @@ # # USAGE: # ./metric-postgres-locks.rb -u db_user -p db_pass -h db_host -d db -# ./metric-postgres-locks.rb -u db_user -p db_pass -h db_host -d 'db1;db2' +# ./metric-postgres-locks.rb -u db_user -p db_pass -h db_host -d 'db1,db2' # # NOTES: # @@ -63,7 +63,7 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite long: '--port PORT' option :databases, - description: 'Database names, separated by ";"', + description: 'Database names, separated by ","', short: '-d DB', long: '--db DB', default: nil @@ -88,21 +88,22 @@ def run locks_per_type = Hash.new(0) pgpass databases = pgdatabases - con = PG.connect(host: config[:hostname], - dbname: databases.first, - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) + # connect only to first database and get information for all databases through SQL queries + con = PG.connect(host: config[:hostname], + dbname: databases.first, + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) databases.each do |database| request = [ 'SELECT mode, count(mode) AS count FROM pg_locks', - "WHERE database = (SELECT oid FROM pg_database WHERE datname = '#{database}')", + 'WHERE database = (SELECT oid FROM pg_database WHERE datname = $1)', 'GROUP BY mode' ] - con.exec(request.join(' ')) do |result| + con.exec_params(request.join(' '), [database]) do |result| result.each do |row| lock_name = row['mode'].downcase.to_sym locks_per_type[lock_name] = row['count'] diff --git a/bin/metric-postgres-statsbgwriter.rb b/bin/metric-postgres-statsbgwriter.rb index ab57cdd..0bc44f5 100755 --- a/bin/metric-postgres-statsbgwriter.rb +++ b/bin/metric-postgres-statsbgwriter.rb @@ -76,12 +76,12 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite def run timestamp = Time.now.to_i pgpass - con = PG.connect(host: config[:hostname], - dbname: 'postgres', - user: config[:user], - password: config[:password], - port: config[:port], - connect_timeout: config[:timeout]) + con = PG.connect(host: config[:hostname], + dbname: 'postgres', + user: config[:user], + password: config[:password], + port: config[:port], + connect_timeout: config[:timeout]) request = [ 'select checkpoints_timed, checkpoints_req,', 'checkpoint_write_time, checkpoint_sync_time,', diff --git a/bin/metric-postgres-statsio.rb b/bin/metric-postgres-statsio.rb index 69f754b..666fb43 100755 --- a/bin/metric-postgres-statsio.rb +++ b/bin/metric-postgres-statsio.rb @@ -18,7 +18,7 @@ # # USAGE: # ./metric-postgres-statsio.rb -u db_user -p db_pass -h db_host -d db -s scope -# ./metric-postgres-statsio.rb -u db_user -p db_pass -h db_host -d 'db1;db2' -s scope +# ./metric-postgres-statsio.rb -u db_user -p db_pass -h db_host -d 'db1,db2' -s scope # # NOTES: # Requires PSQL `track_io_timing` enabled @@ -64,7 +64,7 @@ class PostgresStatsIOMetrics < Sensu::Plugin::Metric::CLI::Graphite long: '--port PORT' option :databases, - description: 'Database names, separated by ";"', + description: 'Database names, separated by ","', short: '-d DB', long: '--db DB', default: nil diff --git a/bin/metric-postgres-statstable.rb b/bin/metric-postgres-statstable.rb index 274dcdb..a12a119 100755 --- a/bin/metric-postgres-statstable.rb +++ b/bin/metric-postgres-statstable.rb @@ -18,7 +18,7 @@ # # USAGE: # ./metric-postgres-statstable.rb -u db_user -p db_pass -h db_host -d db -s scope -# ./metric-postgres-statstable.rb -u db_user -p db_pass -h db_host -d 'db1;db2' -s scope +# ./metric-postgres-statstable.rb -u db_user -p db_pass -h db_host -d 'db1,db2' -s scope # # NOTES: # Requires PSQL `track_counts` enabled @@ -64,7 +64,7 @@ class PostgresStatsTableMetrics < Sensu::Plugin::Metric::CLI::Graphite long: '--port PORT' option :databases, - description: 'Database names, separated by ";"', + description: 'Database names, separated by ","', short: '-d DB', long: '--db DB', default: nil diff --git a/lib/sensu-plugins-postgres/pgdatabases.rb b/lib/sensu-plugins-postgres/pgdatabases.rb index a4d0680..34ebc16 100644 --- a/lib/sensu-plugins-postgres/pgdatabases.rb +++ b/lib/sensu-plugins-postgres/pgdatabases.rb @@ -14,7 +14,7 @@ def pgdatabases dbs else - config[:databases].split(';').map(&:strip) + config[:databases].split(',').map(&:strip) end end end From 404fb8142ebaa3088668d83dcd14971817eb6af4 Mon Sep 17 00:00:00 2001 From: Florian Schwab Date: Tue, 14 Jan 2020 14:12:05 +0100 Subject: [PATCH 4/6] fix missing wrong number of arguments for statsio and statstable metrics --- bin/metric-postgres-statsio.rb | 4 ++-- bin/metric-postgres-statstable.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/metric-postgres-statsio.rb b/bin/metric-postgres-statsio.rb index 666fb43..d45ef2e 100755 --- a/bin/metric-postgres-statsio.rb +++ b/bin/metric-postgres-statsio.rb @@ -90,11 +90,10 @@ class PostgresStatsIOMetrics < Sensu::Plugin::Metric::CLI::Graphite include Pgdatabases def run - timestamp = Time.now.to_i pgpass pgdatabases.each do |database| - output_database(database, timestamp) + output_database(database) end ok @@ -103,6 +102,7 @@ def run private def output_database(database) + timestamp = Time.now.to_i con = PG.connect(host: config[:hostname], dbname: database, user: config[:user], diff --git a/bin/metric-postgres-statstable.rb b/bin/metric-postgres-statstable.rb index a12a119..1143d10 100755 --- a/bin/metric-postgres-statstable.rb +++ b/bin/metric-postgres-statstable.rb @@ -90,11 +90,10 @@ class PostgresStatsTableMetrics < Sensu::Plugin::Metric::CLI::Graphite include Pgdatabases def run - timestamp = Time.now.to_i pgpass pgdatabases.each do |database| - output_database(database, timestamp) + output_database(database) end ok @@ -103,6 +102,7 @@ def run private def output_database(database) + timestamp = Time.now.to_i con = PG.connect(host: config[:hostname], dbname: database, user: config[:user], From 2b7e80e636ecc63fa0ca358495fad2e4f4a13f8f Mon Sep 17 00:00:00 2001 From: Florian Schwab <231497+ydkn@users.noreply.github.com> Date: Wed, 15 Jan 2020 11:07:31 +0100 Subject: [PATCH 5/6] Update CHANGELOG.md Co-Authored-By: Ben Abrams --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7853fd..235a305 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins ## [Unreleased] ### Changed -- Option `database (-d, --db)` for metrics now supports semicolon separated list of databases to fetch metrics of multiple databases at once. +- On `bin/metric-*`: option `database (-d, --db)` now supports semicolon separated list of databases to fetch metrics of multiple databases at once. ### Breaking Changes - If no `database (-d, --db)` argument is supplied for the metric bins through the command line now metrics for all databases are returned. From fb711d328f6f4068aa8fcd9b7c3ce628eb6e872b Mon Sep 17 00:00:00 2001 From: Florian Schwab <231497+ydkn@users.noreply.github.com> Date: Wed, 15 Jan 2020 11:07:52 +0100 Subject: [PATCH 6/6] Update CHANGELOG.md Co-Authored-By: Ben Abrams --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 235a305..7e722ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins ### Changed - On `bin/metric-*`: option `database (-d, --db)` now supports semicolon separated list of databases to fetch metrics of multiple databases at once. ### Breaking Changes -- If no `database (-d, --db)` argument is supplied for the metric bins through the command line now metrics for all databases are returned. +- On `bin/metric-*`: if no `database (-d, --db)` argument is supplied through the command line now metrics for all databases are returned. ## [4.0.0] - 2020-01-09