class CrossoverAgent::Base

Attributes

auth_token[R]
ec2_instance_id[R]
instance_id[R]
port[R]
server[R]

Public Class Methods

execute() click to toggle source
# File lib/crossover_agent.rb, line 36
def self.execute
  cli_options = %w(-i -s -p -t -l -d)
  args, options = CliParser.parse([], cli_options)
  agent = CrossoverAgent::Base.new do |cfg|
    cfg.server = options['-s'] if options['-s']
    cfg.port = options['-p'] if options['-p']
    cfg.auth_token = options['-t'] if options['-t']
    cfg.limit = options['-l'].to_i if options['-l']
    cfg.delay = options['-d'].to_i if options['-d']
    cfg.instance_id = options['-i'] if options['-i']
  end
  agent.execute
end
new() { |config| ... } click to toggle source
# File lib/crossover_agent.rb, line 15
def initialize(&block)
  @config = OpenStruct.new(instance_id: 'localhost', server: 'localhost', port: '3000', auth_token: '123456')
  yield @config if block_given?
  @server = @config['server']
  @port = @config['port']
  @auth_token = @config['auth_token']
  @limit = @config['limit'] || 10
  @delay = @config['delay'] || 1
  @ec2_instance_id = `wget -q -O - http://instance-data/latest/meta-data/instance-id`
  @ec2_instance_id = @config['instance_id'] if @ec2_instance_id.empty?
  if OS.linux?
    @cpu = CPU::Load.new
  end
  @disk_stat = Filesystem.stat('/')
  @uw = Usagewatch
end

Public Instance Methods

execute() click to toggle source
# File lib/crossover_agent.rb, line 49
def execute
  loop do
    begin
      push_data
    rescue Exception => e
      puts e.message
    ensure
      sleep @delay
    end
  end
end
remote_url() click to toggle source
# File lib/crossover_agent.rb, line 32
def remote_url
  "http://#{@server}:#{@port}/metrics"
end

Protected Instance Methods

collect_data() click to toggle source
# File lib/crossover_agent.rb, line 65
def collect_data
  cpu_usage = get_cpu_usage
  disk_usage = get_disk_usage
  running_processes = get_processes(@limit)
  data = {
    metric: {
      cpu_usage: cpu_usage,
      disk_usage: disk_usage,
      running_processes: running_processes,
      auth_token: @auth_token,
      ec2_instance_id: @ec2_instance_id
    }
  }
end
get_cpu_usage() click to toggle source
# File lib/crossover_agent.rb, line 79
def get_cpu_usage
  if OS.linux?
    @cpu.last_minute
  else
    @uw.uw_cpuused
  end
end
get_disk_usage() click to toggle source
# File lib/crossover_agent.rb, line 86
def get_disk_usage
  gb_used = @disk_stat.bytes_used / 1024 / 1024 / 1024
  gb_total = @disk_stat.bytes_total / 1024 / 1024 / 1024
  "#{gb_used} Gb / #{gb_total} Gb"
end
get_processes(limit) click to toggle source
# File lib/crossover_agent.rb, line 91
def get_processes(limit)
  ps = `ps aux | sort -rk 3,3 | head -n #{limit}`
  mapping = [:user, :pid, :cpu, :mem, :vsz, :rss, :tt, :stat, :started, :time, :command, :arg]

  res = []
  arr = ps.split("\n")[2..limit]
  arr.each do |item|
    tmp = {}
    x = item.split(" ")
    x.each_with_index do |i, ind|
      tmp[mapping[ind]] = i
    end
    res << tmp
  end
  res
end
push_data() click to toggle source
# File lib/crossover_agent.rb, line 61
def push_data
  RestClient.post remote_url, collect_data.to_json, :content_type => :json, :accept => :json
end