-
Notifications
You must be signed in to change notification settings - Fork 40
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 ";"', | ||
|
||
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, | ||
|
||
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.