logstash-input-exec 3.1.5 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4508358dc14aef0cf2440ee8271b24bed40e68cd7eb267fed59b003c16ea2f1
4
- data.tar.gz: b25462749761171d93fafa8245da8d91a227039a6098d11abc752d830bc0422a
3
+ metadata.gz: e9a1739a5a30aaaa767c2c473943a5825bc056d6359ce52f6dac9972ced8b7e6
4
+ data.tar.gz: 686879e94a257b42ef068d3e4fe0f48693b62326acb5e9b37cbf12c7046757a6
5
5
  SHA512:
6
- metadata.gz: d349bbc80698e10aa8561b69a1ddc734c99b557d97e0c4ae5d0ad4a160fe7c80ad325ac68cf66239bdb45f1fd699a40b8ab2e08bd852a34ff436e982a269d5f3
7
- data.tar.gz: 4db1b2a03280489a94f5317e0a3a65175a93c0e7b6fadee012fbdfda0c5357f2fd6507a55d9b5b45ab5cdf81f75c515bd819ee881bdbdadc9f7a7789ad957298
6
+ metadata.gz: a6dc50f96efae3f9eb3e2c777a2ff7afd82d67ebfef2a294887b569127ceac53980291d834ca16576a1a17cbbd7be39346850d15c5d4f93e3e2040df931eeba4
7
+ data.tar.gz: c0cc7f534112ee308e1c4f8f9f174672b955998e6ae3f80d15689adecdb6d10cd31e508dda9a4d8d693e4c6d80aa460d24aebbf6f807bdb31b4245ec65a7097e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 3.2.0
2
+ - Add 'schedule' option to schedule the command to run, using a cron expression
3
+
1
4
  ## 3.1.5
2
5
  - Update gemspec summary
3
6
 
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012–2016 Elasticsearch <http://www.elastic.co>
1
+ Copyright (c) 2012-2018 Elasticsearch <http://www.elastic.co>
2
2
 
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
data/docs/index.asciidoc CHANGED
@@ -28,6 +28,21 @@ Notes:
28
28
  * The `message` field of this event will be the entire stdout of the command.
29
29
 
30
30
 
31
+ Example:
32
+
33
+ [source,ruby]
34
+ ----------------------------------
35
+ input {
36
+ exec {
37
+ command => "ls"
38
+ interval => 30
39
+ }
40
+ }
41
+ ----------------------------------
42
+
43
+ This will execute `ls` command every 30 seconds.
44
+
45
+
31
46
  [id="plugins-{type}s-{plugin}-options"]
32
47
  ==== Exec Input Configuration Options
33
48
 
@@ -37,7 +52,8 @@ This plugin supports the following configuration options plus the <<plugins-{typ
37
52
  |=======================================================================
38
53
  |Setting |Input type|Required
39
54
  | <<plugins-{type}s-{plugin}-command>> |<<string,string>>|Yes
40
- | <<plugins-{type}s-{plugin}-interval>> |<<number,number>>|Yes
55
+ | <<plugins-{type}s-{plugin}-interval>> |<<number,number>>|No
56
+ | <<plugins-{type}s-{plugin}-schedule>> |<<string,string>>|No
41
57
  |=======================================================================
42
58
 
43
59
  Also see <<plugins-{type}s-{plugin}-common-options>> for a list of options supported by all
@@ -57,12 +73,35 @@ Command to run. For example, `uptime`
57
73
  [id="plugins-{type}s-{plugin}-interval"]
58
74
  ===== `interval`
59
75
 
60
- * This is a required setting.
61
- * Value type is <<number,number>>
76
+ * Value type is <<string,string>>
62
77
  * There is no default value for this setting.
63
78
 
64
79
  Interval to run the command. Value is in seconds.
65
80
 
81
+ Either `interval` or `schedule` option must be defined.
82
+
83
+ [id="plugins-{type}s-{plugin}-schedule"]
84
+ ===== `schedule`
85
+
86
+ * Value type is <<number,number>>
87
+ * There is no default value for this setting.
88
+
89
+ Schedule of when to periodically run command.
90
+
91
+ This scheduling syntax is powered by https://github.com/jmettraux/rufus-scheduler[rufus-scheduler].
92
+ The syntax is cron-like with some extensions specific to Rufus (e.g. timezone support).
93
+
94
+ Examples:
95
+
96
+ |==========================================================
97
+ | `* 5 * 1-3 *` | will execute every minute of 5am every day of January through March.
98
+ | `0 * * * *` | will execute on the 0th minute of every hour every day.
99
+ | `0 6 * * * America/Chicago` | will execute at 6:00am (UTC/GMT -5) every day.
100
+ |==========================================================
101
+
102
+ Further documentation describing this syntax can be found https://github.com/jmettraux/rufus-scheduler#parsing-cronlines-and-time-strings[here].
103
+
104
+ Either `interval` or `schedule` option must be defined.
66
105
 
67
106
 
68
107
  [id="plugins-{type}s-{plugin}-common-options"]
@@ -3,6 +3,7 @@ require "logstash/inputs/base"
3
3
  require "logstash/namespace"
4
4
  require "socket" # for Socket.gethostname
5
5
  require "stud/interval"
6
+ require "rufus/scheduler"
6
7
 
7
8
  # Periodically run a shell command and capture the whole output as an event.
8
9
  #
@@ -17,22 +18,41 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
17
18
 
18
19
  default :codec, "plain"
19
20
 
20
- # Command to run. For example, `uptime`
21
+ # Command to run. For example : `uptime`
21
22
  config :command, :validate => :string, :required => true
22
23
 
23
24
  # Interval to run the command. Value is in seconds.
24
- config :interval, :validate => :number, :required => true
25
+ # Either `interval` or `schedule` option must be defined.
26
+ config :interval, :validate => :number
27
+
28
+ # Schedule of when to periodically run command, in Cron format
29
+ # For example: "* * * * *" (execute command every minute, on the minute)
30
+ # Either `interval` or `schedule` option must be defined.
31
+ config :schedule, :validate => :string
25
32
 
26
33
  def register
27
- @logger.info("Registering Exec Input", :type => @type, :command => @command, :interval => @interval)
34
+ @logger.info("Registering Exec Input", :type => @type, :command => @command, :interval => @interval, :schedule => @schedule)
28
35
  @hostname = Socket.gethostname
29
36
  @io = nil
37
+
38
+ if (@interval.nil? && @schedule.nil?) || (@interval && @schedule)
39
+ raise LogStash::ConfigurationError, "jdbc input: either 'interval' or 'schedule' option must be defined."
40
+ end
30
41
  end # def register
31
42
 
32
43
  def run(queue)
33
- while !stop?
34
- inner_run(queue)
35
- end # loop
44
+ if @schedule
45
+ @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
46
+ @scheduler.cron @schedule do
47
+ inner_run(queue)
48
+ end
49
+ @scheduler.join
50
+ else
51
+ while !stop?
52
+ duration = inner_run(queue)
53
+ wait_until_end_of_interval(duration)
54
+ end # loop
55
+ end
36
56
  end # def run
37
57
 
38
58
  def inner_run(queue)
@@ -42,17 +62,23 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
42
62
 
43
63
  @logger.debug? && @logger.debug("Command completed", :command => @command, :duration => duration)
44
64
 
45
- wait_until_end_of_interval(duration)
65
+ return duration
46
66
  end
47
67
 
48
68
  def stop
69
+ close_io()
70
+ @scheduler.shutdown(:wait) if @scheduler
71
+ end
72
+
73
+ private
74
+
75
+ # Close @io
76
+ def close_io
49
77
  return if @io.nil? || @io.closed?
50
78
  @io.close
51
79
  @io = nil
52
80
  end
53
81
 
54
- private
55
-
56
82
  # Wait until the end of the interval
57
83
  # @param [Integer] the duration of the last command executed
58
84
  def wait_until_end_of_interval(duration)
@@ -87,7 +113,7 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
87
113
  @logger.error("Exception while running command",
88
114
  :command => command, :e => e, :backtrace => e.backtrace)
89
115
  ensure
90
- stop
116
+ close_io()
91
117
  end
92
118
  end
93
119
  end # class LogStash::Inputs::Exec
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'logstash-input-exec'
4
- s.version = '3.1.5'
4
+ s.version = '3.2.0'
5
5
  s.licenses = ['Apache License (2.0)']
6
6
  s.summary = "Captures the output of a shell command as an event"
7
7
  s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -23,7 +23,9 @@ Gem::Specification.new do |s|
23
23
  s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
24
24
  s.add_runtime_dependency 'stud', '~> 0.0.22'
25
25
  s.add_runtime_dependency 'logstash-codec-plain'
26
+ s.add_runtime_dependency 'rufus-scheduler'
26
27
 
27
28
  s.add_development_dependency 'logstash-devutils'
29
+ s.add_development_dependency 'timecop'
28
30
  end
29
31
 
@@ -1,13 +1,20 @@
1
1
  # encoding: utf-8
2
+ require "timecop"
3
+ require "time"
2
4
  require_relative "../spec_helper"
3
5
 
4
6
  describe LogStash::Inputs::Exec do
5
7
 
6
- it "should register" do
7
- input = LogStash::Plugin.lookup("input", "exec").new("command" => "ls", "interval" => 0)
8
-
9
- # register will try to load jars and raise if it cannot find jars or if org.apache.log4j.spi.LoggingEvent class is not present
10
- expect {input.register}.to_not raise_error
8
+ context "when register" do
9
+ it "should not raise error if config is valid" do
10
+ input = LogStash::Plugin.lookup("input", "exec").new("command" => "ls", "interval" => 0)
11
+ # register will try to load jars and raise if it cannot find jars or if org.apache.log4j.spi.LoggingEvent class is not present
12
+ expect {input.register}.to_not raise_error
13
+ end
14
+ it "should raise error if config is invalid" do
15
+ input = LogStash::Plugin.lookup("input", "exec").new("command" => "ls")
16
+ expect {input.register}.to raise_error
17
+ end
11
18
  end
12
19
 
13
20
  context "when operating normally" do
@@ -35,6 +42,30 @@ describe LogStash::Inputs::Exec do
35
42
  end
36
43
  end
37
44
 
45
+ context "when scheduling" do
46
+ let(:input) { LogStash::Plugin.lookup("input", "exec").new("command" => "ls", "schedule" => "* * * * * UTC") }
47
+ let(:queue) { [] }
48
+
49
+ before do
50
+ input.register
51
+ end
52
+
53
+ it "should properly schedule" do
54
+ Timecop.travel(Time.new(2000))
55
+ Timecop.scale(60)
56
+ runner = Thread.new do
57
+ input.run(queue)
58
+ end
59
+ sleep 3
60
+ input.stop
61
+ runner.kill
62
+ runner.join
63
+ expect(queue.size).to eq(2)
64
+ Timecop.return
65
+ end
66
+
67
+ end
68
+
38
69
  context "when interrupting the plugin" do
39
70
 
40
71
  it_behaves_like "an interruptible input plugin" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-input-exec
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.5
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-07 00:00:00.000000000 Z
11
+ date: 2018-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ name: rufus-scheduler
68
+ prerelease: false
69
+ type: :runtime
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
61
75
  - !ruby/object:Gem::Dependency
62
76
  requirement: !ruby/object:Gem::Requirement
63
77
  requirements:
@@ -72,6 +86,20 @@ dependencies:
72
86
  - - ">="
73
87
  - !ruby/object:Gem::Version
74
88
  version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ name: timecop
96
+ prerelease: false
97
+ type: :development
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
75
103
  description: This gem is a Logstash plugin required to be installed on top of the
76
104
  Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
77
105
  gem is not a stand-alone program
@@ -113,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
141
  version: '0'
114
142
  requirements: []
115
143
  rubyforge_project:
116
- rubygems_version: 2.6.11
144
+ rubygems_version: 2.6.13
117
145
  signing_key:
118
146
  specification_version: 4
119
147
  summary: Captures the output of a shell command as an event