lib/heartbeat-client.rb in heartbeat-client-0.4.0 vs lib/heartbeat-client.rb in heartbeat-client-0.4.1
- old
+ new
@@ -1,10 +1,12 @@
require 'rubygems'
gem 'httparty'
require 'httparty'
require 'logger'
require 'macaddr'
+require 'net/http'
+require 'json'
class Heartbeat
include HTTParty
def self.is_mac?
@@ -21,26 +23,28 @@
def self.log
@@log
end
- def self.create(config)
+ def self.create(config, version = '0.0')
log.info("#create - Collecting data...")
apikey = config['apikey']
endpoint = config['endpoint']
name = config['name']
apache_status = config['apache_status']
+ mongostat_arguments = config['mongostat_arguments']
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 = {}
+ mongodb = {}
log.debug("Dumping top output...")
if is_linux?
`top -b -n1 > /tmp/top.out`
else
@@ -49,13 +53,19 @@
log.debug("Dumping df output...")
`df -m > /tmp/dfm.out`
if apache_status
+ log.debug("Dumping apache status output...")
`curl #{apache_status} > /tmp/apache.out`
end
+ if mongostat_arguments
+ log.debug("Dumping mongostat output...")
+ `mongostat #{mongostat_arguments} > /tmp/mongodb.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?
@@ -95,13 +105,24 @@
counter += 1
end
end
end
+ if File.exists?('/tmp/mongodb.out')
+ File.open("/tmp/mongodb.out", "r") do |infile|
+ counter = 0
+ while (line = infile.gets)
+ mongodb_status(mongodb, line) if counter == 2
+ counter += 1
+ end
+ end
+ end
+
options = {
:body => {
:heartbeat => {
+ :client_version => version,
:apikey => apikey,
:host => `hostname`.chomp,
:macaddr => Mac.addr,
:name => name,
:timestamp => Time.now.to_i,
@@ -111,11 +132,12 @@
:cpu_usage => cpu_usage,
:processes => processes,
:memory => memory,
:disks => disks,
:swap => swap,
- :apache_status => apache
+ :apache_status => apache,
+ :mongodb_status => mongodb
}
}
}
}
@@ -194,9 +216,29 @@
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.mongodb_status(mongodb, str)
+ if (mo = str.split(' '))
+ mo.each_with_index do |moa, index|
+ mongodb['insert'] = moa.strip.to_i if index == 0
+ mongodb['query'] = moa.strip.to_i if index == 1
+ mongodb['update'] = moa.strip.to_i if index == 2
+ mongodb['delete'] = moa.strip.to_i if index == 3
+ mongodb['getmore'] = moa.strip.to_i if index == 4
+ mongodb['command'] = moa.strip.to_i if index == 5
+ mongodb['flushes'] = moa.strip.to_i if index == 6
+ mongodb['mapped'] = moa.strip if index == 7
+ mongodb['vsize'] = moa.strip if index == 8
+ mongodb['res'] = moa.strip if index == 9
+ mongodb['netIn'] = moa.strip if index == 14
+ mongodb['netOut'] = moa.strip if index == 15
+ mongodb['conn'] = moa.strip if index == 16
+ end
end
end
def self.disk_usage(disks, str)
ds = str.split(' ')