bin/instrument_server in instrumental_tools-0.2.1 vs bin/instrument_server in instrumental_tools-0.3.0
- old
+ new
@@ -15,10 +15,14 @@
class SystemInspector
TYPES = [:gauges, :incrementors]
attr_accessor *TYPES
+ def self.memory
+ @memory ||= Memory.new
+ end
+
def initialize
@gauges = {}
@incrementors = {}
@platform =
case RUBY_PLATFORM
@@ -30,10 +34,12 @@
raise "unsupported OS"
end
end
def load_all
+ self.class.memory.cycle
+
load @platform.load_cpu
load @platform.load_memory
load @platform.load_disks
load @platform.load_filesystem
end
@@ -144,18 +150,27 @@
output[:gauges].merge!(loadavg)
output
end
def self.cpu
- cpu, user, nice, system, idle, iowait = `cat /proc/stat | grep cpu[^0-9]`.chomp.split(/\s+/)
- total = user.to_i + system.to_i + idle.to_i + iowait.to_i
- {
- 'cpu.user' => (user.to_f / total) * 100,
- 'cpu.system' => (system.to_f / total) * 100,
- 'cpu.idle' => (idle.to_f / total) * 100,
- 'cpu.iowait' => (iowait.to_f / total) * 100,
- }
+ categories = [:user, :nice, :system, :idle, :iowait]
+ values = `cat /proc/stat | grep cpu[^0-9]`.chomp.split(/\s+/).slice(1, 5).map(&:to_f)
+ SystemInspector.memory.store(:cpu_values, values.dup)
+ if previous_values = SystemInspector.memory.retrieve(:cpu_values)
+ values.map!.with_index { |value, index| (previous_values[index] - value).abs }
+ end
+ data = Hash[*categories.zip(values).flatten]
+ total = values.inject { |memo, value| memo + value }
+
+ output = {}
+ if previous_values
+ data.each do |category, value|
+ output["cpu.#{category}"] = value / total * 100
+ end
+ end
+ output["cpu.in_use"] = 100 - data['idle'] / total * 100
+ output
end
def self.loadavg
min_1, min_5, min_15 = `cat /proc/loadavg`.split(/\s+/)
{
@@ -238,10 +253,33 @@
'filesystem.max_open_files' => max,
'filesystem.open_files_pct_max' => (open_files.to_f / max.to_f) * 100,
}
end
end
+
+ class Memory
+ attr_reader :past_values, :current_values
+
+ def initialize
+ @past_values = {}
+ @current_values = {}
+ end
+
+ def store(attribute, value)
+ @current_values[attribute] = value
+ end
+
+ def retrieve(attribute)
+ @past_values[attribute]
+ end
+
+ def cycle
+ @past_values = @current_values
+ @current_values = {}
+ end
+ end
+
end
class ServerController < Pidly::Control
COMMANDS = [:start, :stop, :status, :restart, :clean, :kill]
@@ -309,9 +347,12 @@
:hostname => `hostname`.chomp,
}
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: instrument_server [-k API_KEY] [options] [-d #{ServerController::COMMANDS.join('|')}]"
+ opts.on('-H', '--hostname HOSTNAME', 'Hostname to report as') do |hostname|
+ options[:hostname] = hostname
+ end
opts.on('-k', '--api_key API_KEY', 'API key of your project') do |api_key|
options[:api_key] = api_key
end
opts.on('-c', '--collector COLLECTOR[:PORT]', "Collector (default #{options[:collector]}:#{options[:port]})") do |collector|
address, port = collector.split(':')