class Fluent::TopInputParser

Constants

STATE_HEADER

state

STATE_PROCESS

Public Class Methods

new() click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 9
def initialize()
  reset_state
end

Public Instance Methods

parse(line) click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 13
def parse(line)
  case @state
  when STATE_HEADER then
    return parse_header line
  else
    return parse_process line
  end
end

Private Instance Methods

is_number?(s) click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 53
def is_number?(s)
  begin
    Float(s)
    true
  rescue
    false
  end
end
parse_header(line) click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 27
def parse_header(line)
  #  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  #    1 root      20   0   38004   5992   3916 S  0.0  0.6   0:03.43 systemd

  ss = line.split("\s")
  @state = STATE_PROCESS if ss[0] == "PID"
  return false, nil
end
parse_process(line) click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 36
def parse_process(line)
  ss = line.split("\s")
  if !is_number?(ss[0]) then
    reset_state
    return parse_header(line)
  end
  begin
    pid, user, _pr, _ni, _virt, res, _shr, _s, cpu, mem, _time, cmd = ss
    args = ss.drop(12)
    return true, @@PS_INFO.new(pid.to_i, user, parse_unit(res), cpu.to_f, mem.to_f, cmd, args)
  rescue => e
    $log.warn "parse error #{e.to_s}: " + line
    reset_state
    return false, nil
  end
end
parse_unit(s) click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 62
def parse_unit(s)
  begin 
    case s[-1].downcase
    when "g"  # GiB
      (s[0, s.length-1].to_f * 1024 * 1024).to_i
    when "m"  # MiB
      (s[0, s.length-1].to_f * 1024).to_i
    else
      s.to_i
    end
  rescue
    $log.warn "Memory unit parse error: " + s
    -1
  end
end
reset_state() click to toggle source
# File lib/fluent/plugin/in_top_parser.rb, line 23
def reset_state()
  @state = STATE_HEADER
end