Class: Panoptimon::Collector

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/panoptimon/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, logger

Constructor Details

#initialize(args) ⇒ Collector

Returns a new instance of Collector.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/panoptimon/collector.rb', line 12

def initialize (args)
  cmd = args.delete(:command) or raise "must have 'command' argument"
  @cmd = cmd.class == Array ? cmd : Shellwords.shellsplit(cmd)
  ->(exe) {
    raise "no such file '#{exe}'" unless File.exist?(exe)
    raise "command '#{exe}' is not executable" unless File.executable?(exe)
  }.call(@cmd[0]) # TODO or maybe args[:interpreter]
  @bus = args.delete(:bus) or raise "must have 'bus' argument"
  args.each { |k,v| instance_variable_set("@#{k}", v) }

  @name ||= 'unnamed'
  @config ||= {}

  @interval = config[:interval] ||= 60
  @last_run_time = Time.at(-@interval)
end

Instance Attribute Details

#busObject (readonly)

Returns the value of attribute bus.



11
12
13
# File 'lib/panoptimon/collector.rb', line 11

def bus
  @bus
end

#cmdObject (readonly)

Returns the value of attribute cmd.



11
12
13
# File 'lib/panoptimon/collector.rb', line 11

def cmd
  @cmd
end

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/panoptimon/collector.rb', line 11

def config
  @config
end

#intervalObject (readonly)

Returns the value of attribute interval.



11
12
13
# File 'lib/panoptimon/collector.rb', line 11

def interval
  @interval
end

#last_run_timeObject (readonly)

Returns the value of attribute last_run_time.



11
12
13
# File 'lib/panoptimon/collector.rb', line 11

def last_run_time
  @last_run_time
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/panoptimon/collector.rb', line 11

def name
  @name
end

Instance Method Details

#noise(mess) ⇒ Object



48
49
50
# File 'lib/panoptimon/collector.rb', line 48

def noise(mess)
  logger.warn "collector/#{name} noise: #{mess.chomp}"
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/panoptimon/collector.rb', line 29

def run
  cmdc = @cmd + [JSON.generate(config)]

  @last_run_time = Time.now # TODO .to_i ?

  logger.info {"run command: #{cmdc}"}
  @child = EM.popen3b(cmdc, CollectorSink, self)
  @child.on_unbind { |status, errmess|
    logger.error {"collector #{name} failed: #{status}" +
      (errmess.nil? ? '' :
        "\n  #{errmess.chomp.split(/\n/).join("\n  ")}")
    } if(not(status.nil?) and status != 0)
    @child = nil
  }
  logger.debug "timeout is: #{config[:timeout]}"
  # XXX afaict, eventmachine just did not implement this:
  # @child.set_comm_inactivity_timeout(config[:timeout])
end

#running?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/panoptimon/collector.rb', line 52

def running?
  @child.nil? ? false : true
end