Skip to content

Instantly share code, notes, and snippets.

@phil-monroe
Created June 18, 2013 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phil-monroe/5808200 to your computer and use it in GitHub Desktop.
Save phil-monroe/5808200 to your computer and use it in GitHub Desktop.
Simple ruby script to view Postgres stats
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