1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
# -*- ruby -*-
# vim: set noet nosta sw=4 ts=4 :
#
# PostgreSQL statistic gatherer.
# Mahlon E. Smith <mahlon@martini.nu>
#
# Based on queries by Kenny Gorman.
# http://www.kennygorman.com/wordpress/?page_id=491
#
# An example gnuplot input script is included in the __END__ block
# of this script. Using it, you can feed the output this script
# generates to gnuplot (after removing header lines) to generate
# some nice performance charts.
#
require 'ostruct'
require 'optparse'
require 'etc'
require 'pg'
### PostgreSQL Stats. Fetch information from pg_stat_* tables.
### Optionally run in a continuous loop, displaying deltas.
###
class Stats
VERSION = %q$Id$
def initialize( opts )
@opts = opts
@db = PG.connect(
:dbname => opts.database,
:host => opts.host,
:port => opts.port,
:user => opts.user,
:password => opts.pass,
:sslmode => 'prefer'
)
@last = nil
end
######
public
######
### Primary loop. Gather statistics and generate deltas.
###
def run
run_count = 0
loop do
current_stat = self.get_stats
# First run, store and continue
#
if @last.nil?
@last = current_stat
sleep @opts.interval
next
end
# headers
#
if run_count == 0 || run_count % 50 == 0
puts "%-20s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s" % %w[
time commits rollbks blksrd blkshit bkends seqscan
seqtprd idxscn idxtrd ins upd del locks activeq
]
end
# calculate deltas
#
delta = current_stat.inject({}) do |h, pair|
stat, val = *pair
if %w[ activeq locks bkends ].include?( stat )
h[stat] = current_stat[stat].to_i
else
h[stat] = current_stat[stat].to_i - @last[stat].to_i
end
h
end
delta[ 'time' ] = Time.now.strftime('%F %T')
# new values
#
puts "%-20s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s" % [
delta['time'], delta['commits'], delta['rollbks'], delta['blksrd'],
delta['blkshit'], delta['bkends'], delta['seqscan'],
delta['seqtprd'], delta['idxscn'], delta['idxtrd'],
delta['ins'], delta['upd'], delta['del'], delta['locks'], delta['activeq']
]
@last = current_stat
run_count += 1
sleep @opts.interval
end
end
### Query the database for performance measurements. Returns a hash.
###
def get_stats
res = @db.exec %Q{
SELECT
MAX(stat_db.xact_commit) AS commits,
MAX(stat_db.xact_rollback) AS rollbks,
MAX(stat_db.blks_read) AS blksrd,
MAX(stat_db.blks_hit) AS blkshit,
MAX(stat_db.numbackends) AS bkends,
SUM(stat_tables.seq_scan) AS seqscan,
SUM(stat_tables.seq_tup_read) AS seqtprd,
SUM(stat_tables.idx_scan) AS idxscn,
SUM(stat_tables.idx_tup_fetch) AS idxtrd,
SUM(stat_tables.n_tup_ins) AS ins,
SUM(stat_tables.n_tup_upd) AS upd,
SUM(stat_tables.n_tup_del) AS del,
MAX(stat_locks.locks) AS locks,
MAX(activity.sess) AS activeq
FROM
pg_stat_database AS stat_db,
pg_stat_user_tables AS stat_tables,
(SELECT COUNT(*) AS locks FROM pg_locks ) AS stat_locks,
(SELECT COUNT(*) AS sess FROM pg_stat_activity WHERE current_query <> '<IDLE>') AS activity
WHERE
stat_db.datname = '%s';
} % [ @opts.database ]
return res[0]
end
end
### Parse command line arguments. Return a struct of global options.
###
def parse_args( args )
options = OpenStruct.new
options.database = Etc.getpwuid( Process.uid ).name
options.host = '127.0.0.1'
options.port = 5432
options.user = Etc.getpwuid( Process.uid ).name
options.sslmode = 'disable'
options.interval = 5
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.separator ''
opts.separator 'Connection options:'
opts.on( '-d', '--database DBNAME',
"specify the database to connect to (default: \"#{options.database}\")" ) do |db|
options.database = db
end
opts.on( '-h', '--host HOSTNAME', 'database server host' ) do |host|
options.host = host
end
opts.on( '-p', '--port PORT', Integer,
"database server port (default: \"#{options.port}\")" ) do |port|
options.port = port
end
opts.on( '-U', '--user NAME',
"database user name (default: \"#{options.user}\")" ) do |user|
options.user = user
end
opts.on( '-W', 'force password prompt' ) do |pw|
print 'Password: '
begin
system 'stty -echo'
options.pass = gets.chomp
ensure
system 'stty echo'
puts
end
end
opts.separator ''
opts.separator 'Other options:'
opts.on( '-i', '--interval SECONDS', Integer,
"refresh interval in seconds (default: \"#{options.interval}\")") do |seconds|
options.interval = seconds
end
opts.on_tail( '--help', 'show this help, then exit' ) do
$stderr.puts opts
exit
end
opts.on_tail( '--version', 'output version information, then exit' ) do
puts Stats::VERSION
exit
end
end
opts.parse!( args )
return options
end
### Go!
###
if __FILE__ == $0
$stdout.sync = true
Stats.new( parse_args( ARGV ) ).run
end
__END__
######################################################################
### T E R M I N A L O P T I O N S
######################################################################
#set terminal png nocrop enhanced font arial 8 size '800x600' x000000 xffffff x444444
#set output 'graph.png'
set terminal pdf linewidth 4 size 11,8
set output 'graph.pdf'
#set terminal aqua
######################################################################
### O P T I O N S F O R A L L G R A P H S
######################################################################
set multiplot layout 2,1 title "PostgreSQL Statistics\n5 second sample rate (smoothed)"
set grid x y
set key right vertical outside
set key nobox
set xdata time
set timefmt "%Y-%m-%d.%H:%M:%S"
set format x "%l%p"
set xtic rotate by -45
input_file = "database_stats.txt"
# edit to taste!
set xrange ["2012-04-16.00:00:00":"2012-04-17.00:00:00"]
######################################################################
### G R A P H 1
######################################################################
set title "Database Operations and Connection Totals"
set yrange [0:200]
plot \
input_file using 1:2 title "Commits" with lines smooth bezier, \
input_file using 1:3 title "Rollbacks" with lines smooth bezier, \
input_file using 1:11 title "Inserts" with lines smooth bezier, \
input_file using 1:12 title "Updates" with lines smooth bezier, \
input_file using 1:13 title "Deletes" with lines smooth bezier, \
input_file using 1:6 title "Backends (total)" with lines, \
input_file using 1:15 title "Active queries (total)" with lines smooth bezier
######################################################################
### G R A P H 2
######################################################################
set title "Backend Performance"
set yrange [0:10000]
plot \
input_file using 1:4 title "Block (cache) reads" with lines smooth bezier, \
input_file using 1:5 title "Block (cache) hits" with lines smooth bezier, \
input_file using 1:7 title "Sequence scans" with lines smooth bezier, \
input_file using 1:8 title "Sequence tuple reads" with lines smooth bezier, \
input_file using 1:9 title "Index scans" with lines smooth bezier, \
input_file using 1:10 title "Index tuple reads" with lines smooth bezier
######################################################################
### C L E A N U P
######################################################################
unset multiplot
reset
|