Class: Fluent::Plugin::DstatInput

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

Defined Under Namespace

Classes: DstatCSVWatcher

Instance Method Summary collapse

Constructor Details

#initializeDstatInput

Returns a new instance of DstatInput.



10
11
12
13
14
15
16
17
18
19
# File 'lib/fluent/plugin/in_dstat.rb', line 10

def initialize
  super

  require 'csv'
  @line_number = 0
  @first_keys = []
  @second_keys = []
  @data_array = []
  @last_time = Time.now
end

Instance Method Details

#check_dstatObject



59
60
61
62
63
64
65
# File 'lib/fluent/plugin/in_dstat.rb', line 59

def check_dstat
  now = Time.now
  if now - @last_time > @delay * 3
    log.info "Process dstat(#{@pid}) is stopped. Last updated: #{@last_time}"
    restart
  end
end

#configure(conf) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/in_dstat.rb', line 46

def configure(conf)
  super

  @command = "#{@dstat_path} #{@option} --nocolor --output #{@tmp_file} #{@delay} > /dev/null 2>&1"
  @hostname = `#{@hostname_command}`.chomp!

  begin
    `#{@dstat_path} --version`
  rescue Errno::ENOENT
    raise Fluent::ConfigError, "'#{@dstat_path}' command not found. Install dstat before run fluentd"
  end
end

#desc(description) ⇒ Object



24
25
# File 'lib/fluent/plugin/in_dstat.rb', line 24

def desc(description)
end

#receive_lines(lines) ⇒ Object



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
145
146
# File 'lib/fluent/plugin/in_dstat.rb', line 105

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
        else
          @first_keys[index] = @first_keys[index].gsub(/\s/, '_')
        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
      }
      router.emit(@tag, Fluent::Engine.now, record)
    end
    @line_number += 1
    @last_time = Time.now
  end

end

#restartObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fluent/plugin/in_dstat.rb', line 86

def restart
  Process.detach(@pid)
  begin
    Process.kill(:TERM, @pid)
  rescue Errno::ESRCH => e
    log.error "unexpected death of a child process", :error=>e.to_s
    log.error_backtrace
  end
  @dw.detach
  @tw.detach
  @line_number = 0

  @io = IO.popen(@command, "r")
  @pid = @io.pid
  @dw = DstatCSVWatcher.new(@tmp_file, &method(:receive_lines))
  event_loop_attach(@dw)
  @tw = timer_execute(:in_dstat_timer, 1, &method(:check_dstat))
end

#shutdownObject



78
79
80
81
82
83
84
# File 'lib/fluent/plugin/in_dstat.rb', line 78

def shutdown
  Process.kill(:TERM, @pid)
  @dw.detach
  @tw.detach
  File.delete(@tmp_file)
  super
end

#startObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/fluent/plugin/in_dstat.rb', line 67

def start
  super
  system("mkfifo #{@tmp_file}")
  @io = IO.popen(@command, "r")
  @pid = @io.pid

  @dw = DstatCSVWatcher.new(@tmp_file, &method(:receive_lines))
  event_loop_attach(@dw)
  @tw = timer_execute(:in_dstat_timer, 1, &method(:check_dstat))
end