Class: CrossoverAgent::Base

Inherits:
Object
  • Object
show all
Includes:
Sys
Defined in:
lib/crossover_agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|@config| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (@config)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/crossover_agent.rb', line 13

def initialize(&block)
  @config = OpenStruct.new(server: 'localhost', port: '3000', auth_token: '123456')
  yield @config
  @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 = "localhost" if @ec2_instance_id.empty?
  @cpu = CPU::Load.new
  @disk_stat = Filesystem.stat('/')
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



12
13
14
# File 'lib/crossover_agent.rb', line 12

def auth_token
  @auth_token
end

#ec2_instance_idObject (readonly)

Returns the value of attribute ec2_instance_id.



12
13
14
# File 'lib/crossover_agent.rb', line 12

def ec2_instance_id
  @ec2_instance_id
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/crossover_agent.rb', line 12

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



12
13
14
# File 'lib/crossover_agent.rb', line 12

def server
  @server
end

Class Method Details

.executeObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/crossover_agent.rb', line 31

def self.execute
  cli_options = %w(-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']
  end
  agent.execute
end

Instance Method Details

#executeObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/crossover_agent.rb', line 43

def execute

  loop do
    begin
      push_data
    rescue Exception => e
      puts e.message
    ensure
      sleep @delay
    end
  end
end

#remote_urlObject



27
28
29
# File 'lib/crossover_agent.rb', line 27

def remote_url
  "http://#{@server}:#{@port}/metrics"
end