Class: Fluent::TDMonitorAgentInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_td_monitor_agent.rb

Defined Under Namespace

Modules: Mac Classes: BandwidthStat, CpuStat, DiskStat, MemoryStat, TimerWatcher

Constant Summary collapse

VERSION =
"0.2.1"
EVENT_ENDPOINT_PATH =
'/v1/monitoring/start'

Instance Method Summary collapse

Constructor Details

#initializeTDMonitorAgentInput

Returns a new instance of TDMonitorAgentInput.



26
27
28
29
30
31
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 26

def initialize
  super
  require 'json'
  require 'ohai'
  require 'httpclient'
end

Instance Method Details

#configure(conf) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 55

def configure(conf)
  super

  @agent_id = get_agent_id
  @mac_address = Mac.address
  @ca_file = find_ca_file
  $log.warn "crt file not found. Use VERIFY_NONE in SSL context" if @ca_file.nil?
end

#on_timerObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 109

def on_timer
  retrying = false
  @retry_limit.times { |i|
    if send_to_tdms(EVENT_ENDPOINT_PATH, collect_info)
      if retrying
        log.warn "retry succeeded after #{i} retry"
      end
      return
    else
      retrying = true
    end
    sleep 2
  }
  log.error "Send instance metrics failed. Try next #{@emit_interval} seconds"
end

#runObject



100
101
102
103
104
105
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 100

def run
  @loop.run
rescue => e
  log.error "unexpected error", :error=> e.to_s
  log.error_backtrace
end

#shutdownObject



92
93
94
95
96
97
98
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 92

def shutdown
  log.info "shutdown td_monitor_agent plugin"

  @loop.watchers.each {|w| w.detach }
  @loop.stop
  @thread.join
end

#startObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 64

def start
  Engine.set_tag_path

  @started_at = Time.now.to_i
  @monitor_agent = ExMonitorAgentInput.new
  begin
    unless @disable_node_info
      @cpu_stat = CpuStat.new
      @disk_stat = DiskStat.new(FileBuffer.class_variable_get(:@@buffer_paths).keys)
      @memory_stat = MemoryStat.new
      @bandwidth_stat = BandwidthStat.new(@emit_interval)
    end
  rescue => e
    @disable_node_info = true
    log.warn "Failed to get system metrics. Set 'disable_node_info' to true: #{e}"
  end
  @counters = collect_counters

  unless register_instance_info
    log.warn "Can't register instance information at start"
  end

  @loop = Coolio::Loop.new
  @timer = TimerWatcher.new(@emit_interval, true, log, &method(:on_timer))
  @loop.attach(@timer)
  @thread = Thread.new(&method(:run))
end