diff --git a/lib/riemann/tools.rb b/lib/riemann/tools.rb index fe4f14f3..e2376036 100644 --- a/lib/riemann/tools.rb +++ b/lib/riemann/tools.rb @@ -32,7 +32,8 @@ def options opt :event_host, 'Event hostname', type: String opt :interval, 'Seconds between updates', default: 5 opt :tag, 'Tag to add to events', type: String, multi: true - opt :ttl, 'TTL for events', type: Integer + opt :ttl, 'TTL for events (twice the interval when unspecified)', type: Integer + opt :minimum_ttl, 'Minimum TTL for events', type: Integer, short: :none opt :attribute, 'Attribute to add to the event', type: String, multi: true opt :timeout, 'Timeout (in seconds) when waiting for acknowledgements', default: 30 opt :tcp, 'Use TCP transport instead of UDP (improves reliability, slight overhead.', default: true @@ -50,6 +51,9 @@ def initialize(allow_arguments: false) options @argv = ARGV.dup abort "Error: stray arguments: #{ARGV.map(&:inspect).join(', ')}" if ARGV.any? && !allow_arguments + + options[:ttl] ||= options[:interval] * 2 + options[:ttl] = [options[:minimum_ttl], options[:ttl]].compact.max end # Returns parsed options (cached) from command line. @@ -68,7 +72,7 @@ def attributes def report(event) event[:tags] = event.fetch(:tags, []) + options[:tag] - event[:ttl] ||= options[:ttl] || (options[:interval] * 2) + event[:ttl] ||= options[:ttl] event[:host] = options[:event_host].dup if options[:event_host] diff --git a/spec/riemann/tools_spec.rb b/spec/riemann/tools_spec.rb new file mode 100644 index 00000000..da82ee4a --- /dev/null +++ b/spec/riemann/tools_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'riemann/tools/health' + +class ExampleTool + include Riemann::Tools +end + +RSpec.describe Riemann::Tools do + describe '#options' do + describe ':ttl' do + subject(:tool) { ExampleTool.new } + + { + '' => 10, + '--ttl=60' => 60, + '--interval 10' => 20, + '--minimum-ttl 300' => 300, + '--minimum-ttl 300 --ttl=60' => 300, + '--minimum-ttl 30 --ttl 60' => 60, + }.each do |argv, expected_ttl| + context "with ARGV=\"#{argv}\"" do + before do + ARGV.replace argv.split + end + + it { expect(tool.options[:ttl]).to eq expected_ttl } + end + end + end + end +end