Class: Fluent::DstatInput
- Inherits:
-
Input
- Object
- Input
- Fluent::DstatInput
- Includes:
- Mixin::RewriteTagName
- Defined in:
- lib/fluent/plugin/in_dstat.rb
Defined Under Namespace
Classes: DstatCSVWatcher, TimerWatcher
Instance Method Summary collapse
- #check_dstat ⇒ Object
- #configure(conf) ⇒ Object
-
#initialize ⇒ DstatInput
constructor
A new instance of DstatInput.
- #receive_lines(lines) ⇒ Object
- #restart ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #touch_or_truncate(file) ⇒ Object
Constructor Details
#initialize ⇒ DstatInput
Returns a new instance of DstatInput.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/fluent/plugin/in_dstat.rb', line 8 def initialize super require 'csv' @line_number = 0 @first_keys = [] @second_keys = [] @data_array = [] @max_lines = 100 @last_time = Time.now end |
Instance Method Details
#check_dstat ⇒ Object
36 37 38 |
# File 'lib/fluent/plugin/in_dstat.rb', line 36 def check_dstat restart if (Time.now - @last_time) > @delay*3 end |
#configure(conf) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/fluent/plugin/in_dstat.rb', line 29 def configure(conf) super @command = "#{@dstat_path} #{@option} --output #{@tmp_file} #{@delay}" @hostname = `#{@hostname_command}`.chomp! end |
#receive_lines(lines) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/fluent/plugin/in_dstat.rb', line 95 def receive_lines(lines) lines.each do |line| next if line == "" case @line_number when 0..1 when 2 line.delete!("\"") @first_keys = CSV.parse_line(line) pre_key = "" @first_keys.each_with_index do |key, index| if key.nil? || key == "" @first_keys[index] = pre_key end pre_key = @first_keys[index] end when 3 line.delete!("\"") @second_keys = line.split(',') @first_keys.each_with_index do |key, index| @data_array[index] = {} @data_array[index][:first] = key @data_array[index][:second] = @second_keys[index] end else values = line.split(',') data = Hash.new { |hash,key| hash[key] = Hash.new {} } values.each_with_index do |v, index| data[@first_keys[index]][@second_keys[index]] = v end record = { 'hostname' => @hostname, 'dstat' => data } emit_tag = @tag.dup filter_record(emit_tag, Engine.now, record) Engine.emit(emit_tag, Engine.now, record) end if (@line_number % @max_lines) == (@max_lines - 1) @dw.detach File.truncate(@tmp_file, 0) @dw = DstatCSVWatcher.new(@tmp_file, &method(:receive_lines)) @dw.attach(@loop) end @line_number += 1 @last_time = Time.now end end |
#restart ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/fluent/plugin/in_dstat.rb', line 71 def restart Process.detach(@pid) Process.kill(:TERM, @pid) @dw.detach @tw.detach @line_number = 0 touch_or_truncate(@tmp_file) @io = IO.popen(@command, "r") @pid = @io.pid @dw = DstatCSVWatcher.new(@tmp_file, &method(:receive_lines)) @dw.attach(@loop) @tw = TimerWatcher.new(1, true, &method(:check_dstat)) @tw.attach(@loop) end |
#run ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/fluent/plugin/in_dstat.rb', line 62 def run begin @loop.run rescue $log.error "unexpected error", :error=>$!.to_s $log.error_backtrace end end |
#shutdown ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/fluent/plugin/in_dstat.rb', line 53 def shutdown Process.kill(:TERM, @pid) @dw.detach @tw.detach @loop.stop @thread.join File.delete(@tmp_file) end |
#start ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fluent/plugin/in_dstat.rb', line 40 def start touch_or_truncate(@tmp_file) @io = IO.popen(@command, "r") @pid = @io.pid @loop = Coolio::Loop.new @dw = DstatCSVWatcher.new(@tmp_file, &method(:receive_lines)) @dw.attach(@loop) @tw = TimerWatcher.new(1, true, &method(:check_dstat)) @tw.attach(@loop) @thread = Thread.new(&method(:run)) end |
#touch_or_truncate(file) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/fluent/plugin/in_dstat.rb', line 87 def touch_or_truncate(file) if File.exist?(file) File.truncate(file, 0) else `touch #{file}` end end |