forked from lmarburger/pg_stats_aggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgstats.rb
57 lines (48 loc) · 1.5 KB
/
pgstats.rb
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
require 'librato/metrics'
class PGStats
def initialize(db, counters = Hash.new, interval = 60, client = Librato::Metrics.client)
@db = db
@counters = counters
@interval = interval
@client = client
end
def stats
@db[<<-SQL].first
SELECT sum(seq_scan) AS sequence_scans,
sum(idx_scan) AS index_scans,
sum(n_tup_ins) AS inserts,
sum(n_tup_upd) AS updates,
sum(n_tup_del) AS deletes
FROM pg_stat_user_tables;
SQL
end
def ratios
@db[<<-SQL].first
SELECT (sum(idx_blks_hit) - sum(idx_blks_read)) / sum(idx_blks_hit) AS cache_hit_ratio
FROM pg_statio_user_indexes AS cache_hit_ratio;
SQL
end
def submit
queue = @client.new_queue
measure_time = now_floored
stats.each do |name, current_counter|
current_counter = current_counter.to_i
last_counter = @counters[name]
if last_counter && current_counter >= last_counter
value = current_counter - last_counter
queue.add("postgres.#{name}" => { :value => value,
:measure_time => measure_time })
end
@counters[name] = current_counter
end
ratios.each do |name, value|
queue.add("postgres.#{name}" => { :value => value,
:measure_time => measure_time })
end
queue.submit unless queue.empty?
end
def now_floored
time = Time.now.to_i
time - (time % @interval)
end
end