bin/instrument_server in instrumental_tools-0.5.1 vs bin/instrument_server in instrumental_tools-0.5.2

- old
+ new

@@ -33,10 +33,18 @@ else raise "unsupported OS" end end + def self.command_missing(command, section) + puts "Command #{command} not found. Metrics for #{section} will not be collected." + end + + def self.command_present?(command, section) + `which #{command}`.length > 0 || command_missing(command, section) + end + def load_all self.class.memory.cycle load @platform.load_cpu load @platform.load_memory @@ -48,11 +56,15 @@ @gauges.merge!(stats[:gauges] || {}) end module OSX def self.load_cpu - { :gauges => top } + output = { :gauges => {} } + if SystemInspector.command_present?('top', 'CPU') + output[:gauges].merge!(top) + end + output end def self.top lines = [] processes = date = load = cpu = nil @@ -82,11 +94,15 @@ } end def self.load_memory # TODO: swap - { :gauges => vm_stat } + output = { :gauges => {} } + if SystemInspector.command_present?('vm_stat', 'memory') + output[:gauges].merge!(vm_stat) + end + output end def self.vm_stat header, *rows = `vm_stat`.split("\n") page_size = header.match(/page size of (\d+) bytes/)[1].to_i @@ -106,11 +122,15 @@ output["memory.free_percent"] = output["memory.free_mb"] / total * 100 # TODO: verify output end def self.load_disks - { :gauges => df } + output = { :gauges => {} } + if SystemInspector.command_present?('df', 'disk') + output[:gauges].merge!(df) + end + output end def self.df output = {} `df -k`.split("\n").grep(%r{^/dev/}).each do |line| @@ -181,12 +201,16 @@ } end def self.load_memory output = { :gauges => {} } - output[:gauges].merge!(memory) - output[:gauges].merge!(swap) + if SystemInspector.command_present?('free', 'memory') + output[:gauges].merge!(memory) + end + if SystemInspector.command_present?('free', 'swap') + output[:gauges].merge!(swap) + end output end def self.memory _, total, used, free, shared, buffers, cached = `free -k -o | grep Mem`.chomp.split(/\s+/) @@ -207,11 +231,18 @@ 'swap.free_percent' => (free.to_f / total.to_f) * 100, } end def self.load_disks - { :gauges => disk_storage.merge(disk_io) } + output = { :gauges => {} } + if SystemInspector.command_present?('df', 'disk storage') + output[:gauges].merge!(disk_storage) + end + if SystemInspector.command_present?('mount', 'disk IO') + output[:gauges].merge!(disk_io) + end + output end def self.disk_storage output = {} `df -Pk`.split("\n").grep(%r{^/dev/}).each do |line| @@ -250,10 +281,14 @@ end output end def self.load_filesystem - { :gauges => filesystem } + output = { :gauges => {} } + if SystemInspector.command_present?('sysctl', 'filesystem') + output[:gauges].merge!(filesystem) + end + output end def self.filesystem allocated, unused, max = `sysctl fs.file-nr`.split[-3..-1].map { |v| v.to_i } open_files = allocated - unused