class ServerMetrics::Collector
Attributes
Public Class Methods
see to_hash. The hash should contain :options and :memory keys
# File lib/server_metrics/collector.rb, line 131 def self.from_hash(hash) c=new(hash[:options]) c.instance_variable_set('@memory', hash[:memory]) c.instance_variable_set('@data', hash[:data]) c end
# File lib/server_metrics/collector.rb, line 19 def initialize(options={}) @options = options @data={} @memory={} @collector_id = self.class.name+'-'+@options.to_a.sort_by { |a| a.first.to_s }.flatten.join('-') @error=nil end
Public Instance Methods
Convert strings containing 'T,G,M,or K' to MB. The result is a float only – units are NOT returned
# File lib/server_metrics/collector.rb, line 101 def convert_to_mb(value) value = if value =~ /G/i value.to_f*1024.0 elsif value =~ /M/i value.to_f elsif value =~ /K/i (value.to_f/1024.0) elsif value =~ /T/i (value.to_f*1024.0*1024.0) else value.to_f end ("%.1f" % [value]).to_f end
counter(:rkbps, stats / 2, :per => :second) counter(:rpm, request_counter, :per => :minute) counter(:swap_ins, vmstat, :per => :second, :round => true)
# File lib/server_metrics/collector.rb, line 64 def counter(name, value, options = {}, &block) current_time = Time.now if data = memory("_counter_#{name}") last_time, last_value = data[:time], data[:value] elapsed_seconds = current_time - last_time # We won't log it if the value has wrapped or enough time hasn't # elapsed if value >= last_value && elapsed_seconds >= 1 if block result = block.call(last_value, value) else result = value - last_value end case options[:per] when :second, 'second' result = result / elapsed_seconds.to_f when :minute, 'minute' result = result / elapsed_seconds.to_f * 60.0 else raise "Unknown option for ':per': #{options[:per].inspect}" end if options[:round] result = (result * (10 ** options[:round])).round / (10 ** options[:round]).to_f end report(name => result) end end remember("_counter_#{name}" => {:time => current_time, :value => value}) end
# File lib/server_metrics/collector.rb, line 138 def linux? ruby_config['target_os'].start_with?('linux') end
memory(:no_track) memory.delete(:no_track) memory.clear
# File lib/server_metrics/collector.rb, line 45 def memory(name = nil) if name.nil? @memory else @memory[name] || @memory[name.is_a?(String) ? name.to_sym : String(name)] end end
# File lib/server_metrics/collector.rb, line 117 def normalize_key(key) (key.is_a?(String) ? key : key.to_s).downcase.gsub(" ", "_").gsub("%", "percent").to_sym end
# File lib/server_metrics/collector.rb, line 27 def option(name) @options[name] || @options[name.is_a?(String) ? name.to_sym : String(name)] end
# File lib/server_metrics/collector.rb, line 142 def osx? ruby_config['target_os'].start_with?('darwin') end
remember(name1: value1, name2: value2)
# File lib/server_metrics/collector.rb, line 56 def remember(hash) @memory.merge!(hash) end
# File lib/server_metrics/collector.rb, line 37 def report(hash) @data.merge!(hash) end
# File lib/server_metrics/collector.rb, line 31 def run @data={} build_report @data end
returns a hash you can serialize and store on disk, or just hold onto and re-instantiate the collector later. Why you'd need to do this: to persist the memory (including counters) of a plugin instance.
Collector.from_hash(h)
is the flipside of this: Collector.from_hash(plugin.to_hash)
gets you essentially the same instance
# File lib/server_metrics/collector.rb, line 126 def to_hash {:options => @options, :memory => @memory, :data => @data, :plugin_id => @plugin_id} end
Private Instance Methods
Returns hash of Ruby config parameters.
# File lib/server_metrics/collector.rb, line 149 def ruby_config RbConfig::CONFIG end