Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport/fix when file is gone #22

Open
wants to merge 7 commits into
base: core-api-v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 1.1.0
- Add create_if_deleted option to create a destination file in case it
was deleted by another agent in the machine. In case of being false
the system will add the incomming messages to the failure file.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
source 'https://rubygems.org'
gemspec
gemspec
gem "concurrent-ruby", "0.9.1"
39 changes: 33 additions & 6 deletions lib/logstash/outputs/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class LogStash::Outputs::File < LogStash::Outputs::Base

config_name "file"

attr_reader :failure_path

# The path to the file to write. Event fields can be used here,
# like `/var/log/logstash/%{host}/%{application}`
# One may also utilize the path option for date-based log
Expand Down Expand Up @@ -42,6 +44,11 @@ class LogStash::Outputs::File < LogStash::Outputs::Base
# into this file and inside the defined path.
config :filename_failure, :validate => :string, :default => '_filepath_failures'

# If the a file is deleted, but an event is comming with the need to be stored
# in such a file, the plugin will created a gain this file. Default => true
config :create_if_deleted, :validate => :boolean, :default => true


public
def register
require "fileutils" # For mkdir_p
Expand All @@ -56,8 +63,10 @@ def register

if path_with_field_ref?
@file_root = extract_file_root
@failure_path = File.join(@file_root, @filename_failure)
else
@file_root = File.dirname(path)
end
@failure_path = File.join(@file_root, @filename_failure)

now = Time.now
@last_flush_cycle = now
Expand Down Expand Up @@ -94,6 +103,8 @@ def receive(event)
if path_with_field_ref? && !inside_file_root?(file_output_path)
@logger.warn("File: the event tried to write outside the files root, writing the event to the failure file", :event => event, :filename => @failure_path)
file_output_path = @failure_path
elsif !@create_if_deleted && deleted?(file_output_path)
file_output_path = @failure_path
end

output = format_message(event)
Expand Down Expand Up @@ -124,7 +135,6 @@ def inside_file_root?(log_path)
def write_event(log_path, event)
@logger.debug("File, writing event to file.", :filename => log_path)
fd = open(log_path)

# TODO(sissel): Check if we should rotate the file.

fd.write(event)
Expand Down Expand Up @@ -199,23 +209,40 @@ def close_stale_files
end

private
def open(path)
return @files[path] if @files.include?(path) and not @files[path].nil?
def cached?(path)
@files.include?(path) && !@files[path].nil?
end

private
def deleted?(path)
!File.exist?(path)
end

private
def open(path)
if !deleted?(path) && cached?(path)
return @files[path]
elsif deleted?(path)
if @create_if_deleted
@logger.debug("Required path was deleted, creating the file again", :path => path)
@files.delete(path)
else
return @files[path] if cached?(path)
end
end
@logger.info("Opening file", :path => path)

dir = File.dirname(path)
if !Dir.exists?(dir)
@logger.info("Creating directory", :directory => dir)
FileUtils.mkdir_p(dir)
end

# work around a bug opening fifos (bug JRUBY-6280)
stat = File.stat(path) rescue nil
if stat && stat.ftype == "fifo" && LogStash::Environment.jruby?
fd = java.io.FileWriter.new(java.io.File.new(path))
else
fd = File.new(path, "a")
fd = File.new(path, "a+")
end
if gzip
fd = Zlib::GzipWriter.new(fd)
Expand Down
4 changes: 2 additions & 2 deletions logstash-output-file.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|

s.name = 'logstash-output-file'
s.version = '1.0.0'
s.version = '1.1.0'
s.licenses = ['Apache License (2.0)']
s.summary = "This output will write events to files on disk"
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
Expand All @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }

# Gem dependencies
s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 1.999.0'
s.add_runtime_dependency 'logstash-input-generator'

s.add_development_dependency 'logstash-devutils'
Expand Down
53 changes: 53 additions & 0 deletions spec/outputs/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "stud/temporary"
require "tempfile"
require "uri"
require "fileutils"

describe LogStash::Outputs::File do
describe "ship lots of events to a file" do
Expand Down Expand Up @@ -106,6 +107,58 @@
end

describe "receiving events" do

context "when the output file is deleted" do

let(:temp_file) { Tempfile.new('logstash-spec-output-file_deleted') }

let(:config) do
{ "path" => temp_file.path, "flush_interval" => 0 }
end

it "should recreate the required file if deleted" do
output = LogStash::Outputs::File.new(config)
output.register

10.times do |i|
event = LogStash::Event.new("event_id" => i)
output.receive(event)
end
FileUtils.rm(temp_file)
10.times do |i|
event = LogStash::Event.new("event_id" => i+10)
output.receive(event)
end
expect(FileTest.size(temp_file.path)).to be > 0
end

context "when appending to the error log" do

let(:config) do
{ "path" => temp_file.path, "flush_interval" => 0, "create_if_deleted" => false }
end

it "should append the events to the filename_failure location" do
output = LogStash::Outputs::File.new(config)
output.register

10.times do |i|
event = LogStash::Event.new("event_id" => i)
output.receive(event)
end
FileUtils.rm(temp_file)
10.times do |i|
event = LogStash::Event.new("event_id" => i+10)
output.receive(event)
end
expect(FileTest.exist?(temp_file.path)).to be_falsey
expect(FileTest.size(output.failure_path)).to be > 0
end

end

end

context "when using an interpolated path" do
context "when trying to write outside the files root directory" do
let(:bad_event) do
Expand Down