Class: Oxidized::DebugYAML

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/oxidized/input/debugyaml.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_debug, node, input_name) ⇒ DebugYAML

Returns a new instance of DebugYAML.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/oxidized/input/debugyaml.rb', line 5

def initialize(config_debug, node, input_name)
  return unless config_debug == true ||
                (config_debug.is_a?(String) && config_debug.downcase.include?('yaml'))

  @log = File.open(logfile(node, input_name), 'w')

  @partial_line = false
  @first_line = true
  @commands_started = false

  @log.puts '---'
  @log.puts 'init_prompt: |-'
  @log.flush
end

Instance Method Details

#closeObject



76
77
78
79
80
# File 'lib/oxidized/input/debugyaml.rb', line 76

def close
  return unless @log

  @log.close
end

#logfile(node, input_name) ⇒ Object

Separate method to ease unit tests



21
22
23
24
25
26
# File 'lib/oxidized/input/debugyaml.rb', line 21

def logfile(node, input_name)
  timestamp = Time.now.strftime('%Y%m%d-%H%M%S')
  file = Oxidized::Config::LOG + "/#{node&.ip}-#{input_name}-#{timestamp}.yaml"
  logger.debug "Writing YAML Simulation to #{file}"
  file
end

#receive_data(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/oxidized/input/debugyaml.rb', line 40

def receive_data(data)
  return unless @log
  return if data.empty?

  lines = data.split("\n", -1)

  lines.each_with_index do |line, idx|
    is_last = idx == lines.length - 1
    full_line = is_last ? (data[-1] == "\n") : true
    # Escape line and strip surrounding double quotes
    line = line.dump[1..-2]
    if @first_line
      # Make sure the leading space of the first line (if present)
      # is coded with \0x20 or YAML block scalars won't work
      line.sub!(/^ /, '\x20')
      @first_line = false
    end

    # Make sure trailing white spaces are coded with \0x20
    line.gsub!(/ $/, '\x20')

    output = @partial_line ? line : ('      ' + line)
    @partial_line = false

    if full_line
      @log.puts output
    else
      @log.write output
    end
  end

  @partial_line = data[-1] != "\n"

  @log.flush
end

#send_data(data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/oxidized/input/debugyaml.rb', line 28

def send_data(data)
  return unless @log

  @log.puts
  @log.puts 'commands:' unless @commands_started
  @log.puts "  - #{data.dump}: |-"
  @first_line = true
  @partial_line = false
  @commands_started = true
  @log.flush
end