lib/heartbeat-client.rb in heartbeat-client-0.2.0 vs lib/heartbeat-client.rb in heartbeat-client-0.2.1

- old
+ new

@@ -19,34 +19,41 @@ def self.log Logger.new('/tmp/heartbeat.log') end def self.create(apikey) - procs = {'total' => 0, 'running' => 0, 'stuck' => 0, 'sleeping' => 0, 'threads' => 0} + procs = {'total' => 0, 'running' => 0, 'stuck' => 0, 'sleeping' => 0, 'threads' => 0, 'stopped' => 0, 'zombie' => 0} load_avg = [] cpu_usage = {'user' => 0, 'sys' => 0, 'idle' => 0} processes = [] - if self.is_linux? + if is_linux? `top -b -n1 > /tmp/top.out` else `top -l 1 > /tmp/top.out` end if File.exists?('/tmp/top.out') counter = 0; proc_count = 0 File.open("/tmp/top.out", "r") do |infile| while (line = infile.gets) - processes(procs, line) if line.include?('Processes') - load_averages(load_avg, line) if line.include?('Load Avg') - cpu_usages(cpu_usage, line) if line.include?('CPU usage') - proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND') + if is_linux? + processes(procs, line) if line.include?('Task') + load_averages(load_avg, line) if line.include?('load average') + cpu_usages(cpu_usage, line) if line.include?('Cpu') + proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND') + else + processes(procs, line) if line.include?('Processes') + load_averages(load_avg, line) if line.include?('Load Avg') + cpu_usages(cpu_usage, line) if line.include?('CPU usage') + proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND') + end process(processes, line) if proc_count > 0 and counter >= proc_count counter += 1 end end - + options = { :body => { :heartbeat => { :apikey => apikey, :host => `hostname`.chomp, @@ -59,56 +66,72 @@ } } } } - # puts procs.inspect - # puts load_avg.inspect - # puts cpu_usage.inspect - # puts processes.inspect + #puts procs.inspect + #puts load_avg.inspect + #puts cpu_usage.inspect + #puts processes.inspect pp Heartbeat.post('/heartbeat', options) else put "No top output found." end end def self.processes(procs, str) proc = str.split(':') - if proc and proc[0] and proc[0].include?('Processes') + if proc and proc[0] proc[1].split(',').each do |pr| procs['total'] = pr.split(' ')[0].strip.to_i if pr.include?('total') procs['running'] = pr.split(' ')[0].strip.to_i if pr.include?('running') procs['stuck'] = pr.split(' ')[0].strip.to_i if pr.include?('stuck') procs['sleeping'] = pr.split(' ')[0].strip.to_i if pr.include?('sleeping') procs['threads'] = pr.split(' ')[0].strip.to_i if pr.include?('threads') + procs['stopped'] = pr.split(' ')[0].strip.to_i if pr.include?('stopped') + procs['zombie'] = pr.split(' ')[0].strip.to_i if pr.include?('zombie') end end end def self.load_averages(load_avg, str) - avg = str.split(':') - if avg and avg[0] and avg[0].include?('Load Avg') + if is_linux? + avg = str.split('load average:') + else + avg = str.split(':') + end + if avg and avg[0] avg[1].split(',').each do |a| load_avg << a.strip.to_f end end end def self.cpu_usages(cpu_usage, str) cpu = str.split(':') - if cpu and cpu[0] and cpu[0].include?('CPU usage') + if cpu and cpu[0] cpu[1].split(',').each do |cp| - cpu_usage['user'] = cp.split(' ')[0].strip.to_f if cp.include?('user') - cpu_usage['sys'] = cp.split(' ')[0].strip.to_f if cp.include?('sys') - cpu_usage['idle'] = cp.split(' ')[0].strip.to_f if cp.include?('idle') + if is_linux? + cpu_usage['us'] = cp.split(' ')[0].strip.to_f if cp.include?('us') + cpu_usage['sy'] = cp.split(' ')[0].strip.to_f if cp.include?('sy') + cpu_usage['id'] = cp.split(' ')[0].strip.to_f if cp.include?('id') + else + cpu_usage['user'] = cp.split(' ')[0].strip.to_f if cp.include?('user') + cpu_usage['sys'] = cp.split(' ')[0].strip.to_f if cp.include?('sys') + cpu_usage['idle'] = cp.split(' ')[0].strip.to_f if cp.include?('idle') + end end end end def self.process(processes, line) procs = line.split(' ') if procs and procs.size > 0 - processes << {'pid' => procs[0].strip.to_i, 'command' => procs[1].strip, 'cpu' => procs[2].strip.to_f} + if is_linux? + processes << {'pid' => procs[0].strip.to_i, 'command' => procs[11].strip, 'cpu' => procs[8].strip.to_f} # debian + else + processes << {'pid' => procs[0].strip.to_i, 'command' => procs[1].strip, 'cpu' => procs[2].strip.to_f} + end end end end