Module: Pgmonitor::PS
Constant Summary collapse
- @@last_renice =
Time.now
Instance Method Summary collapse
- #add_cummulative_data(cdata) ⇒ Object
- #command_to_data(command) ⇒ Object
- #cummulate_data(data, timeslice) ⇒ Object
- #line_to_data(line) ⇒ Object
- #log(level, *args) ⇒ Object
- #need_to_renice? ⇒ Boolean
- #processes(&blk) ⇒ Object
- #queue ⇒ Object
- #renice(&blk) ⇒ Object
- #renice_writer_process ⇒ Object
- #run ⇒ Object
- #scrape(data, &blk) ⇒ Object
Instance Method Details
#add_cummulative_data(cdata) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/pgmonitor/ps.rb', line 101 def add_cummulative_data(cdata) cdata.each do |database, data| ::Pgmonitor::UsageData.add(database, data) end ::Pgmonitor::UsageData.clean end |
#command_to_data(command) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/pgmonitor/ps.rb', line 148 def command_to_data(command) regex = /^postgres: ([\w\d]+) ([\w\d]+) ((:?\d{1,3}\.){3}(?:\d{1,3}))\((\d+)\)/ if m = regex.match(command) { 'database' => m[1], 'ip' => m[3], 'port' => m[4] } end end |
#cummulate_data(data, timeslice) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/pgmonitor/ps.rb', line 108 def cummulate_data(data, timeslice) databases = {} data.each do |d| db = d['database'] databases[db] ||= { 'pids' => [], 'cpu' => 0, 'mem' => 0, 'connections' => 0, 'sources' => [], 'timeslice' => timeslice, } databases[db]['pids'] << d['pid'] databases[db]['cpu'] += d['cpu'].to_i databases[db]['mem'] += d['mem'].to_i databases[db]['connections'] += 1 databases[db]['sources'] << "#{d['ip']}:#{d['port']}" end databases end |
#line_to_data(line) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/pgmonitor/ps.rb', line 130 def line_to_data(line) tokens = line.split(/\s+/) pdata = { } pdata['user'] = tokens.shift pdata['pid'] = tokens.shift pdata['nice'] = tokens.shift pdata['cpu'] = tokens.shift pdata['mem'] = tokens.shift command = tokens.join(' ') data = command_to_data(command) return unless data data.merge!(pdata) data end |
#log(level, *args) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/pgmonitor/ps.rb', line 8 def log(level, *args) if level == :debug $stderr.puts(*args) if ::Pgmonitor.debug? else if level == :error $stderr.puts(*args) else $stdout.puts(*args) end end end |
#need_to_renice? ⇒ Boolean
33 34 35 |
# File 'lib/pgmonitor/ps.rb', line 33 def need_to_renice? @@last_renice < Time.now - 60 end |
#processes(&blk) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pgmonitor/ps.rb', line 20 def processes(&blk) ::EM.system("ps h -u postgres -o user,pid,ni,cp,rss,command") do |output, status| data = [] output.split("\n").each do |line| d = line_to_data(line) data << d if d end blk.call(data) end end |
#queue ⇒ Object
84 85 86 |
# File 'lib/pgmonitor/ps.rb', line 84 def queue EM.add_timer(::Pgmonitor.delay) { ::Pgmonitor::PS.run } end |
#renice(&blk) ⇒ Object
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 |
# File 'lib/pgmonitor/ps.rb', line 37 def renice(&blk) unless need_to_renice? blk.call if blk return end databases = ::Pgmonitor::UsageData.avg databases.delete('postgres') databases.each do |db, d| if d['cpu'] > 200 d['nice'] = 19 elsif d['cpu'] > 100 d['nice'] = 15 else d['nice'] = 5 end # if the number of connections is "high" # you're probably stressing the machine just by # having open connections if d['connections'] > 20 && d['nice'] < 15 d['nice'] = 15 end end databases.each do |db, d| log(:notice, "usage db_name='#{db}' cpu=#{d['cpu']} mem=#{d['mem']} nice=#{d['nice']} elapsed=#{d['elapsed_time']} connections=#{d['connections']}#{::Pgmonitor.log_items}") end processes do |psdata| @@last_renice = Time.now psdata.each do |ps| db = ps['database'] next unless databases.has_key?(db) nice = databases[db]['nice'] next if nice == ps['nice'].to_i log(:debug, "renicing db_name='#{db}' pid=#{ps['pid']} from=#{ps['nice']} to=#{nice}") `renice #{nice} -p #{ps['pid']} > /dev/null 2>&1` end renice_writer_process EM.next_tick(&blk) if blk end end |
#renice_writer_process ⇒ Object
155 156 157 158 |
# File 'lib/pgmonitor/ps.rb', line 155 def renice_writer_process `ps aux | grep 'postgres: writer process' | awk '{print $2}' | xargs renice 4 -p > /dev/null 2>&1` `ps aux | grep 'postgres: wal writer process' | awk '{print $2}' | xargs renice 4 -p > /dev/null 2>&1` end |
#run ⇒ Object
88 89 90 91 92 |
# File 'lib/pgmonitor/ps.rb', line 88 def run processes do |data| ::Pgmonitor::PS.scrape(data) { renice { ::Pgmonitor::PS.queue } } end end |
#scrape(data, &blk) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/pgmonitor/ps.rb', line 94 def scrape(data, &blk) t1 = Time.now cdata = cummulate_data(data, t1) add_cummulative_data(cdata) blk.call end |