lib/heartbeat-client.rb in heartbeat-client-0.3.2 vs lib/heartbeat-client.rb in heartbeat-client-0.4.0

- old
+ new

@@ -21,20 +21,26 @@ def self.log @@log end - def self.create(apikey, endpoint, name = nil) + def self.create(config) log.info("#create - Collecting data...") + apikey = config['apikey'] + endpoint = config['endpoint'] + name = config['name'] + apache_status = config['apache_status'] + 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 = [] memory = {'free' => 0, 'used' => 0} disks = {} swap = {'free' => 0, 'used' => 0} + apache = {} log.debug("Dumping top output...") if is_linux? `top -b -n1 > /tmp/top.out` else @@ -42,10 +48,14 @@ end log.debug("Dumping df output...") `df -m > /tmp/dfm.out` + if apache_status + `curl #{apache_status} > /tmp/apache.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) if is_linux? @@ -74,10 +84,20 @@ disk_usage(disks, line) if counter > 0 counter += 1 end end end + + if File.exists?('/tmp/apache.out') + File.open("/tmp/apache.out", "r") do |infile| + counter = 0; lines = [] + while (line = infile.gets) + apache_status(apache, line) + counter += 1 + end + end + end options = { :body => { :heartbeat => { :apikey => apikey, @@ -90,16 +110,17 @@ :load_avg => load_avg, :cpu_usage => cpu_usage, :processes => processes, :memory => memory, :disks => disks, - :swap => swap + :swap => swap, + :apache_status => apache } } } } - + log.info("#create - Sending data to endpoint...") res = Heartbeat.post(endpoint + '/heartbeat', options) log.debug("Response: #{res.response.inspect}") if res log.info("Finished iteration.") else @@ -166,10 +187,19 @@ swap['free'] = s.split(' ')[0].strip if s.include?('free') swap['used'] = s.split(' ')[0].strip if s.include?('used') end end end - + + def self.apache_status(apache, str) + ap = str.split(':') + if ap and ap[0] + apache['requests'] = ap[1].strip.to_f if ap[0].include?('ReqPerSec') + apache['busy_workers'] = ap[1].strip.to_i if ap[0].include?('BusyWorkers') + apache['idle_workers'] = ap[1].strip.to_i if ap[0].include?('IdleWorkers') + end + end + def self.disk_usage(disks, str) ds = str.split(' ') disks[ds[0].strip] = {'used' => ds[2].strip, 'available' => ds[3].strip} end end