Class: Fluent::NostatInput

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

Constant Summary collapse

@@CPU_STAT =
"/proc/stat"
@@MEM_STAT =
"/proc/meminfo"
@@DISK_STAT =
"/proc/diskstats"
@@NET_STAT =
"/proc/net/dev"
@@CPU_USR =
1
@@CPU_SYS =
3
@@CPU_IDL =
4
@@CPU_WAI =
5
@@CPU_HIQ =
6
@@CPU_SIQ =
7
@@LINUX_SECTOR_SIZE_BYTE =
512
@@history =
{}

Instance Method Summary collapse

Constructor Details

#initializeNostatInput

Returns a new instance of NostatInput.



25
26
27
28
# File 'lib/fluent/plugin/in_nostat.rb', line 25

def initialize
  super
  require 'fluent/timezone'
end

Instance Method Details

#configure(conf) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin/in_nostat.rb', line 36

def configure(conf)
  super

  if !@tag
    @tag = @tag_prefix + `hostname`.strip.split('.')[0].strip + ".nostat"
  end
  if !@run_interval
    raise ConfigError, "'run_interval' option is required on nostat input"
  end
end

#emit_graphite_style(time, record) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/fluent/plugin/in_nostat.rb', line 227

def emit_graphite_style (time, record)
  # cpu
  tag = @tag + ".cpu"
  router.emit(tag, time, record["cpu"])
  
  # memory
  tag = @tag + ".mem"
  router.emit(tag, time, record["mem"])

  # disk
  tag_prefix = @tag + ".disk"

  record["disk"].each do |key, value|
    tag = tag_prefix + "." + key
    router.emit(tag, time, value)
  end
  
  # net
  tag_prefix = @tag + ".net"

  record["net"].each do |key, value|
    tag = tag_prefix + "." + key
    router.emit(tag, time, value)
  end
end

#emit_record(time, record) ⇒ Object



253
254
255
256
257
258
259
260
261
# File 'lib/fluent/plugin/in_nostat.rb', line 253

def emit_record (time, record)
  if @output_type == "graphite"
    emit_graphite_style(time, record)
  elsif @output_type == "hash"
    router.emit(@tag, time, record)       
  else
    log.error "invalid output_type. output_type=", @output_type
  end
end

#get_cpu_statObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/in_nostat.rb', line 62

def get_cpu_stat
  res = {}

  first = File.foreach(@@CPU_STAT).first.split

  res["usr"] = first[@@CPU_USR].strip.to_f
  res["sys"] = first[@@CPU_SYS].strip.to_f
  res["idl"] = first[@@CPU_IDL].strip.to_f
  res["wai"] = first[@@CPU_WAI].strip.to_f
  res["siq"] = first[@@CPU_SIQ].strip.to_f
  res["hiq"] = first[@@CPU_HIQ].strip.to_f

  res
end

#get_disk_statObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fluent/plugin/in_nostat.rb', line 105

def get_disk_stat
  res = {}

  File.foreach(@@DISK_STAT) do |line|
    items = line.split
    if ( items[2] =~ /^[hsv]d[a-z]$/ )
      disk = {}

      disk["read"] = items[5].strip.to_i
      disk["write"] = items[9].strip.to_i

      res[items[2]] = disk
    end
  end

  res
end

#get_dstat_cpu(cpu_stat) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fluent/plugin/in_nostat.rb', line 150

def get_dstat_cpu (cpu_stat)
  res = {}
  total = 0
  cpu_stat.each do |key, value|
    res[key] = value - @@history["cpu"][key]
    total += res[key]
  end
    
  res.each do |key,value|
    if ( key == "idl" )
      res[key] = (res[key] / total * 100).floor
    else
      res[key] = (res[key] / total * 100).ceil
    end
  end
  
  res
end

#get_dstat_disk(disk_stat) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fluent/plugin/in_nostat.rb', line 178

def get_dstat_disk (disk_stat)
  res = {}

  disk_stat.each do |key, value|
    disk = {}
    disk["read"] = (((value["read"] - @@history["disk"][key]["read"]) * @@LINUX_SECTOR_SIZE_BYTE) / @run_interval).ceil
    disk["write"] = (((value["write"] - @@history["disk"][key]["write"]) * @@LINUX_SECTOR_SIZE_BYTE) / @run_interval).ceil
    res[key] = disk
  end

  res
end

#get_dstat_mem(mem_stat) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/fluent/plugin/in_nostat.rb', line 169

def get_dstat_mem (mem_stat)
  res = {}
  mem_stat.each do |key, value|
    res[key] = value * 1024
  end

  res
end

#get_dstat_net(net_stat) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fluent/plugin/in_nostat.rb', line 191

def get_dstat_net (net_stat)
  res = {}

  net_stat.each do |key, value|
    net = {}
    net["recv"] = (((value["recv"] - @@history["net"][key]["recv"])) / @run_interval).ceil
    net["send"] = (((value["send"] - @@history["net"][key]["send"])) / @run_interval).ceil
    res[key] = net
  end

  res
end

#get_dstat_record(stat) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fluent/plugin/in_nostat.rb', line 204

def get_dstat_record (stat)
  record = {}
  record["cpu"] = get_dstat_cpu (stat["cpu"])
  record["mem"] = get_dstat_mem (stat["mem"])
  record["disk"] = get_dstat_disk (stat["disk"])
  record["net"] = get_dstat_net (stat["net"])

  @@history = stat

  record
end

#get_mem_statObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fluent/plugin/in_nostat.rb', line 77

def get_mem_stat
  res = {}
  used = 0
  total = 0

  File.foreach(@@MEM_STAT) do |line|
    items = line.split
    name = items[0].split(':').first


    case name
      when "MemTotal"
#          res["total"] = items[1].strip.to_i
      total = items[1].strip.to_i
      when "MemFree"
      res["free"] = items[1].strip.to_i
      when "Buffers"
      res["buff"] = items[1].strip.to_i
      when "Cached"
      res["cach"] = items[1].strip.to_i
      else
    end
  end

  res["used"] = total - res["free"] - res["buff"] - res["cach"]
  res
end

#get_net_statObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fluent/plugin/in_nostat.rb', line 123

def get_net_stat
  res = {}

  File.foreach(@@NET_STAT).with_index do |line, index|
    if ( index < 2 )
      next
    end

    items = line.split
    name = items[0].split(':').first

    if ( name =~ /^lo$/ )
      next
    end

    net = {}

    net["recv"] = items[1].strip.to_i
    net["send"] = items[9].strip.to_i

    res[name] = net

  end

  res
end

#get_statsObject



216
217
218
219
220
221
222
223
224
225
# File 'lib/fluent/plugin/in_nostat.rb', line 216

def get_stats
  stat = {}

  stat["cpu"] = get_cpu_stat
  stat["disk"] = get_disk_stat
  stat["net"] = get_net_stat
  stat["mem"] = get_mem_stat
  
  stat
end

#run_periodicObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/fluent/plugin/in_nostat.rb', line 263

def run_periodic
  until @finished
    begin
      sleep @run_interval

      stat = get_stats

      if (mode == "dstat")
        record = get_dstat_record (stat)
      else
        record = stat
      end

      time = Engine.now
      
      emit_record(time,record)
    rescue => e
      log.error "nostat failed to emit", :error => e.to_s, :error_class => e.class.to_s, :tag => tag
      log.error "nostat to run or shutdown child process", :error => $!.to_s, :error_class => $!.class.to_s
      log.warn_backtrace $!.backtrace

    end
  end
end

#shutdownObject



57
58
59
60
# File 'lib/fluent/plugin/in_nostat.rb', line 57

def shutdown
  @finished = true
  @thread.join
end

#startObject



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

def start
  if ( @mode == "dstat" )
    @@history = get_stats
    sleep @run_interval
  end

  @finished = false
  @thread = Thread.new(&method(:run_periodic))
end