-
Notifications
You must be signed in to change notification settings - Fork 42
Fetch metrics for multiple databases #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
01474e1
fd1a376
3e2aae4
404fb81
53334f5
2b7e80e
fb711d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
# | ||
|
@@ -29,6 +30,7 @@ | |
# | ||
|
||
require 'sensu-plugins-postgres/pgpass' | ||
require 'sensu-plugins-postgres/pgdatabases' | ||
require 'sensu-plugin/metric/cli' | ||
require 'pg' | ||
require 'socket' | ||
|
@@ -60,10 +62,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 ";"', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking it might be safer to pass it with a comma separated list? Is it because we are passing this as raw SQL? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, using "," as separator is better than ";". I've updated the PR with patches for both. |
||
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 +80,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 +101,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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
# | ||
|
@@ -29,6 +30,7 @@ | |
# | ||
|
||
require 'sensu-plugins-postgres/pgpass' | ||
require 'sensu-plugins-postgres/pgdatabases' | ||
require 'sensu-plugin/metric/cli' | ||
require 'pg' | ||
require 'socket' | ||
|
@@ -60,10 +62,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 +80,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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why only the first database? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first database from the list is simply used to make a valid connection and the locks statistics are then queried through SQL statements for all databases. It is simply not necessary to make individual connections for each database. Please see line 99-103 for the queries run for each database. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah gotcha, that makes sense, would not hurt to add a comment about that. I will review this in greater detail when I have more time. Is there any way we can provide backwards compatibility? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I currently see two possible options: Option 1: Option 2: Also Option 1 has the advantage over Option 2 that it is easier to parameterize the check through tokens if there is no option to invalidate/override another. Let me know what you think or if you have another idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its a trade off between backwards compatibility and code readability I tend to prefer to not break users regardless of how small the numbers are unless there is a compelling reason. |
||
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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.