Class: Fluent::Plugin::MacOsLogInput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMacOsLogInput

Returns a new instance of MacOsLogInput.



12
13
14
15
16
17
# File 'lib/fluent/plugin/in_macoslog.rb', line 12

def initialize
  super
  @pf_file = nil
  @log_start_regex = nil
  @compiled_command = nil
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



51
52
53
# File 'lib/fluent/plugin/in_macoslog.rb', line 51

def parser
  @parser
end

Instance Method Details

#compile_commandObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/in_macoslog.rb', line 76

def compile_command
  result = @command

  if @style
    result += " --style #{@style}"
  elsif not result =~ /"--style"/
    result += " --style default"
  end

  if @predicate
    result += " --predicate '#{@predicate}'"
  end

  if @levels.length > 0
    compiled_level = @levels.map { |level| "--#{level}" }.join(" ")
    result += " #{compiled_level}"
  end

  result
end

#configure(conf) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fluent/plugin/in_macoslog.rb', line 53

def configure(conf)
  compat_parameters_convert(conf, :parser)

  super

  unless @tag
    raise Fluent::ConfigError, "'tag' option is required on macoslog input"
  end

  @compiled_command = compile_command

  $log.info "MacOs log command '#{@compiled_command}'"

  @parser = parser_create

  unless @pos_file
    $log.warn "'pos_file PATH' parameter is not set to a 'macoslog' source."
    $log.warn "this parameter is highly recommended to save the position to resume from."
  end

  @log_start_regex = Regexp.compile("\\A#{@log_line_start}")
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/fluent/plugin/in_macoslog.rb', line 97

def multi_workers_ready?
  true
end

#on_record(time, record) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/fluent/plugin/in_macoslog.rb', line 190

def on_record(time, record)
  tag = extract_tag_from_record(record)
  tag ||= @tag
  time ||= extract_time_from_record(record) || Fluent::EventTime.now
  router.emit(tag, time, record)
rescue => e
  log.error "macoslog failed to emit", tag: tag, record: Yajl.dump(record), error: e
  router.emit_error_event(tag, time, record, e) if tag && time && record
end

#parse_line_json(io) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/fluent/plugin/in_macoslog.rb', line 157

def parse_line_json(io)
  logs = Queue.new
  io.each_line.with_index do |line,index|
    logs.push(line.chomp("\n"))
    if index >= @log_header_lines
      @parser.parse(logs.pop, &method(:on_record))
    end
  end
end

#parse_timestamp_base(io) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fluent/plugin/in_macoslog.rb', line 167

def parse_timestamp_base(io)
  log = ""
  io.each_line.with_index do |line,index|
    # Skips log header
    if index >= @log_header_lines
      if line =~ @log_start_regex
        if log.empty?
          log = line
        else
          @parser.parse(log.chomp("\n"), &method(:on_record))
          log = line
        end
      else
        log += line
      end
    end
  end

  unless log.empty?
    @parser.parse(log.chomp("\n"), &method(:on_record))
  end
end

#run(io) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/fluent/plugin/in_macoslog.rb', line 147

def run(io)
  unless io.eof
    if @style == :ndjson
      parse_line_json(io)
    else
      parse_timestamp_base(io)
    end
  end
end

#shutdownObject



141
142
143
144
145
# File 'lib/fluent/plugin/in_macoslog.rb', line 141

def shutdown
  @pf_file.close if @pf_file

  super
end

#startObject



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
# File 'lib/fluent/plugin/in_macoslog.rb', line 101

def start
  super

  if @pos_file
    pos_file_dir = File.dirname(@pos_file)
    FileUtils.mkdir_p(pos_file_dir, mode: @dir_perm) unless Dir.exist?(pos_file_dir)
    @pf_file = File.open(@pos_file, File::RDWR|File::CREAT|File::BINARY, @file_perm)
    @pf_file.sync = true

    start = @pf_file.read.to_i
    if start == 0
      start = Fluent::EventTime.now.to_int
      @pf_file.write(start)
    end
  else
    start = Fluent::EventTime.now.to_int
    @pf_file.write(start) if @pf_file
  end

  oldest = Fluent::EventTime.now.to_int - @max_age.to_i
  if start < oldest
    log.info "Start timestamp over max_age ", start: start, oldest: oldest
    start = oldest
  end

  time_callback = -> (timestamp) {
    if @pf_file
      @pf_file.rewind
      @pf_file.write(timestamp)
    end
  }

  timer_process_execute(:exec_input,
                        @compiled_command,
                        start, @run_interval,
                        time_callback,
                        delay_seconds: 1,
                        immediate: true, mode: [@connect_mode], &method(:run))
end