lib/heartbeat-client.rb in heartbeat-client-0.4.2 vs lib/heartbeat-client.rb in heartbeat-client-0.4.3
- old
+ new
@@ -23,132 +23,151 @@
def self.log
@@log
end
- def self.create(config, version = '0.0')
+ def self.create(config, version = '0.0', gather_metrics = false)
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
- `top -l 1 > /tmp/top.out`
- end
-
- log.debug("Dumping df output...")
- `df -m > /tmp/dfm.out`
+ if gather_metrics
+ 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 = {}
- if apache_status
- log.debug("Dumping apache status output...")
- `curl #{apache_status} > /tmp/apache.out`
- end
+ log.debug("Dumping top output...")
+ if is_linux?
+ `top -b -n1 > /tmp/top.out`
+ else
+ `top -l 1 > /tmp/top.out`
+ end
- if mongostat_arguments
- log.debug("Dumping mongostat output...")
- `mongostat #{mongostat_arguments} > /tmp/mongodb.out`
- end
+ log.debug("Dumping df output...")
+ `df -m > /tmp/dfm.out`
- 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?
- 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')
- memory_usage(memory, line) if line.include?('Mem')
- swap_usage(swap, line) if line.include?('Swap')
- 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')
- memory_usage(memory, line) if line.include?('PhysMem')
- 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
+ if apache_status
+ log.debug("Dumping apache status output...")
+ `curl #{apache_status} > /tmp/apache.out`
end
-
- if File.exists?('/tmp/dfm.out')
- File.open("/tmp/dfm.out", "r") do |infile|
- counter = 0
+
+ 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)
- disk_usage(disks, line) if counter > 0
+ 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')
+ memory_usage(memory, line) if line.include?('Mem')
+ swap_usage(swap, line) if line.include?('Swap')
+ 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')
+ memory_usage(memory, line) if line.include?('PhysMem')
+ 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
- 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
+
+ if File.exists?('/tmp/dfm.out')
+ File.open("/tmp/dfm.out", "r") do |infile|
+ counter = 0
+ while (line = infile.gets)
+ disk_usage(disks, line) if counter > 0
+ counter += 1
+ end
end
end
- end
- if File.exists?('/tmp/mongodb.out')
- File.open("/tmp/mongodb.out", "r") do |infile|
- counter = 0; lines = []
- while (line = infile.gets)
- lines << line
+ 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
- mongodb_status(mongodb, lines)
end
- end
+ if File.exists?('/tmp/mongodb.out')
+ File.open("/tmp/mongodb.out", "r") do |infile|
+ counter = 0; lines = []
+ while (line = infile.gets)
+ lines << line
+ end
+ mongodb_status(mongodb, lines)
+ end
+ end
+
+ options = {
+ :body => {
+ :heartbeat => {
+ :client_version => version,
+ :apikey => apikey,
+ :host => `hostname`.chomp,
+ :macaddr => Mac.addr,
+ :name => name,
+ :timestamp => Time.now.to_i,
+ :values => {
+ :process_stats => procs,
+ :load_avg => load_avg,
+ :cpu_usage => cpu_usage,
+ :processes => processes,
+ :memory => memory,
+ :disks => disks,
+ :swap => swap,
+ :apache_status => apache,
+ :mongodb_status => mongodb
+ }
+ }
+ }
+ }
+
+ log.info("#create - Sending data to endpoint (with metrics)...")
+ res = Heartbeat.post(endpoint + '/heartbeat', options)
+ log.debug("Response: #{res.response.inspect}") if res
+ else
+ log.error "No top output found."
+ end
+ else
options = {
:body => {
:heartbeat => {
:client_version => version,
:apikey => apikey,
:host => `hostname`.chomp,
:macaddr => Mac.addr,
:name => name,
- :timestamp => Time.now.to_i,
- :values => {
- :process_stats => procs,
- :load_avg => load_avg,
- :cpu_usage => cpu_usage,
- :processes => processes,
- :memory => memory,
- :disks => disks,
- :swap => swap,
- :apache_status => apache,
- :mongodb_status => mongodb
- }
+ :timestamp => Time.now.to_i
}
}
}
- log.info("#create - Sending data to endpoint...")
+ log.info("#create - Sending data to endpoint (no metrics)...")
res = Heartbeat.post(endpoint + '/heartbeat', options)
log.debug("Response: #{res.response.inspect}") if res
- log.info("Finished iteration.")
- else
- log.error "No top output found."
end
+ log.info("Finished iteration.")
end
def self.processes(procs, str)
proc = str.split(':')
if proc and proc[0]