Created
June 18, 2013 18:58
-
-
Save phil-monroe/5808200 to your computer and use it in GitHub Desktop.
Simple ruby script to view Postgres stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require 'terminal-table' | |
loop do | |
# Get Running Processes | |
rows = %w(pid client_addr state waiting query_start query) | |
stats = `psql identified_production -t -c 'select #{rows.join(', ')} from pg_stat_activity order by state DESC'` | |
stats = stats.split("\n") | |
stats.map! { |s| s.split('|').map(&:strip) } | |
data = {} | |
stats.map do |s| | |
pid = s[0] | |
data[pid] = {} | |
rows.each_with_index { |row, idx| | |
data[pid][row] = s[idx][0..128] | |
} | |
end | |
# Get CPU Usage | |
ps = `ps -o pid,pcpu #{data.keys.join(' ')}`.split("\n")[1..-1].map(&:split) | |
ps.map do |(pid, pcpu)| | |
data[pid]['pcpu'] = pcpu+'%' | |
end | |
table = Terminal::Table.new :headings => data.values.first.keys, :rows => data.values.map(&:values) | |
system('clear') | |
puts table | |
sleep 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment