-
Notifications
You must be signed in to change notification settings - Fork 43
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 all 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 |
---|---|---|
|
@@ -20,6 +20,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: | ||
# | ||
|
@@ -31,6 +32,7 @@ | |
# | ||
|
||
require 'sensu-plugins-postgres/pgpass' | ||
require 'sensu-plugins-postgres/pgdatabases' | ||
require 'sensu-plugin/metric/cli' | ||
require 'pg' | ||
require 'socket' | ||
|
@@ -62,10 +64,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', | ||
|
@@ -79,33 +82,39 @@ 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 | ||
databases = pgdatabases | ||
# connect only to first database and get information for all databases through SQL queries | ||
con = PG.connect(host: config[:hostname], | ||
dbname: config[:database], | ||
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. I think this still needs to be updated right? 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. Updated in which way? It connects to the first DB and then queries the information for all databases. There is no need to connect to each DB individually. |
||
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.each do |database| | ||
request = [ | ||
'SELECT mode, count(mode) AS count FROM pg_locks', | ||
'WHERE database = (SELECT oid FROM pg_database WHERE datname = $1)', | ||
'GROUP BY mode' | ||
] | ||
|
||
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'] | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this still needs to be updated right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in which way? It connects to the first DB and then queries the information for all databases. There is no need to connect to each DB individually.