logstash-input-exec 3.3.2 → 3.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 454a3fd142d85a04a0e93b3054d6f300d9dbbe7f02013485e2e58d12c5aba3d3
4
- data.tar.gz: c2a32db4b81afc9429beee70d704b7b2cb737b08b924f813a5e91e5de4fd709d
3
+ metadata.gz: 01d8e2cf6057c8c259d53f056dab08b583d222b367adda70a80a045eac7fa08d
4
+ data.tar.gz: 30536c1078190608e0894f9e018dc70cb7fffe0b72ce80ed08a36294d804def8
5
5
  SHA512:
6
- metadata.gz: 983904e90f7ba90ecae393cdda1fa39abb093bc923ca82f63c70dc818b48edc36211926a6848a7c48a7547f17e0d96b7bb7a21d0f353768c064f5d2f17eab7b5
7
- data.tar.gz: 3e7e26cde26f98fcbe1d3e0a752dec41729135c85d49720197d2a1d018c97898188bbe27fcd73290c0169d68d7263b8b6d270f6c95a8012d22a35dda47cf10b4
6
+ metadata.gz: cf1aa23f2e652c11dbdf22cfb78a1831ab0965befa58b69fbef3d83e71f85e65e98f48d71363df258aa087d9d01f0fdc43b784ec8ee4a38631af5d0615aee467
7
+ data.tar.gz: d517483d728e1c59f99bbce6318aeb685f0933e8300d1fbc144362c4828a40c67015edcaefe735b44c12b08f9a6ffff01e208cb3f6779581afc8cc7d37a2c407
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## 3.6.0
2
+ - Refactor: start using scheduler mixin [#33](https://github.com/logstash-plugins/logstash-input-exec/pull/33)
3
+ - Fix: Test failures due to insufficient time given for scheduler to start [#32](https://github.com/logstash-plugins/logstash-input-exec/pull/32)
4
+
5
+ ## 3.5.0
6
+ - Fix: behavior incompatiblity between (standalone) LS and LS in Docker [#30](https://github.com/logstash-plugins/logstash-input-exec/pull/30)
7
+
8
+ ## 3.4.0
9
+ - Feat: adjust fields for ECS compatibility [#28](https://github.com/logstash-plugins/logstash-input-exec/pull/28)
10
+ - Plugin will no longer override fields if they exist in the decoded payload.
11
+ (It no longer sets the `host` field if decoded from the command's output.)
12
+
13
+ ## 3.3.3
14
+ - Docs: improved doc on memory usage [#27](https://github.com/logstash-plugins/logstash-input-exec/pull/27)
15
+
1
16
  ## 3.3.2
2
17
  - Fixed issue where certain log entries were incorrectly writing 'jdbc input' instead of 'exec input' [#21](https://github.com/logstash-plugins/logstash-input-exec/pull/21)
3
18
 
data/CONTRIBUTORS CHANGED
@@ -3,6 +3,7 @@ reports, or in general have helped logstash along its way.
3
3
 
4
4
  Contributors:
5
5
  * Christoph Mertz (cmertz)
6
+ * Colin Surprenant (colinsurprenant)
6
7
  * John E. Vincent (lusis)
7
8
  * Jordan Sissel (jordansissel)
8
9
  * Nick Ethier (nickethier)
data/LICENSE CHANGED
@@ -1,13 +1,202 @@
1
- Copyright (c) 2012-2018 Elasticsearch <http://www.elastic.co>
2
1
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
6
5
 
7
- http://www.apache.org/licenses/LICENSE-2.0
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
7
 
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2020 Elastic and contributors
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Logstash Plugin
2
2
 
3
- [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-input-exec.svg)](https://travis-ci.org/logstash-plugins/logstash-input-exec)
3
+ [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-input-exec.svg)](https://travis-ci.com/logstash-plugins/logstash-input-exec)
4
4
 
5
5
  This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
6
 
data/docs/index.asciidoc CHANGED
@@ -23,10 +23,15 @@ include::{include_path}/plugin_header.asciidoc[]
23
23
 
24
24
  Periodically run a shell command and capture the whole output as an event.
25
25
 
26
- Notes:
27
-
26
+ [NOTE]
27
+ ========
28
28
  * The `command` field of this event will be the command run.
29
29
  * The `message` field of this event will be the entire stdout of the command.
30
+ ========
31
+
32
+ IMPORTANT: The exec input ultimately uses `fork` to spawn a child process.
33
+ Using fork duplicates the parent process address space (in our case, **logstash and the JVM**); this is mitigated with OS copy-on-write but ultimately you can end up allocating lots of memory just for a "simple" executable.
34
+ If the exec input fails with errors like `ENOMEM: Cannot allocate memory` it is an indication that there is not enough non-JVM-heap physical memory to perform the fork.
30
35
 
31
36
 
32
37
  Example:
@@ -35,17 +40,36 @@ Example:
35
40
  ----------------------------------
36
41
  input {
37
42
  exec {
38
- command => "ls"
43
+ command => "echo 'hi!'"
39
44
  interval => 30
40
45
  }
41
46
  }
42
47
  ----------------------------------
43
48
 
44
- This will execute `ls` command every 30 seconds.
49
+ This will execute `echo` command every 30 seconds.
50
+
51
+ [id="plugins-{type}s-{plugin}-ecs"]
52
+ ==== Compatibility with the Elastic Common Schema (ECS)
53
+
54
+ This plugin adds metadata about the event's source, and can be configured to do so
55
+ in an {ecs-ref}[ECS-compatible] way with <<plugins-{type}s-{plugin}-ecs_compatibility>>.
56
+ This metadata is added after the event has been decoded by the appropriate codec,
57
+ and will not overwrite existing values.
58
+
59
+ |========
60
+ | ECS Disabled | ECS v1 , v8 | Description
61
+
62
+ | `host` | `[host][name]` | The name of the {ls} host that processed the event
63
+ | `command` | `[process][command_line]` | The command run by the plugin
64
+ | `[@metadata][exit_status]` | `[process][exit_code]` | The exit code of the process
65
+ | -- | `[@metadata][input][exec][process][elapsed_time]`
66
+ | The elapsed time the command took to run in nanoseconds
67
+ | `[@metadata][duration]` | -- | Command duration in seconds as a floating point number (deprecated)
68
+ |========
45
69
 
46
70
 
47
71
  [id="plugins-{type}s-{plugin}-options"]
48
- ==== Exec Input Configuration Options
72
+ ==== Exec Input configuration options
49
73
 
50
74
  This plugin supports the following configuration options plus the <<plugins-{type}s-{plugin}-common-options>> described later.
51
75
 
@@ -53,6 +77,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
53
77
  |=======================================================================
54
78
  |Setting |Input type|Required
55
79
  | <<plugins-{type}s-{plugin}-command>> |<<string,string>>|Yes
80
+ | <<plugins-{type}s-{plugin}-ecs_compatibility>> |<<string,string>>|No
56
81
  | <<plugins-{type}s-{plugin}-interval>> |<<number,number>>|No
57
82
  | <<plugins-{type}s-{plugin}-schedule>> |<<string,string>>|No
58
83
  |=======================================================================
@@ -71,6 +96,58 @@ input plugins.
71
96
 
72
97
  Command to run. For example, `uptime`
73
98
 
99
+ [id="plugins-{type}s-{plugin}-ecs_compatibility"]
100
+ ===== `ecs_compatibility`
101
+
102
+ * Value type is <<string,string>>
103
+ * Supported values are:
104
+ ** `disabled`: uses backwards compatible field names, such as `[host]`
105
+ ** `v1`, `v8`: uses fields that are compatible with ECS, such as `[host][name]`
106
+
107
+ Controls this plugin's compatibility with the {ecs-ref}[Elastic Common Schema (ECS)].
108
+ See <<plugins-{type}s-{plugin}-ecs>> for detailed information.
109
+
110
+
111
+ **Sample output: ECS enabled**
112
+ [source,ruby]
113
+ -----
114
+ {
115
+ "message" => "hi!\n",
116
+ "process" => {
117
+ "command_line" => "echo 'hi!'",
118
+ "exit_code" => 0
119
+ },
120
+ "host" => {
121
+ "name" => "deus-ex-machina"
122
+ },
123
+
124
+ "@metadata" => {
125
+ "input" => {
126
+ "exec" => {
127
+ "process" => {
128
+ "elapsed_time"=>3042
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+ -----
135
+
136
+ **Sample output: ECS disabled**
137
+ [source,ruby]
138
+ -----
139
+ {
140
+ "message" => "hi!\n",
141
+ "command" => "echo 'hi!'",
142
+ "host" => "deus-ex-machina",
143
+
144
+ "@metadata" => {
145
+ "exit_status" => 0,
146
+ "duration" => 0.004388
147
+ }
148
+ }
149
+ -----
150
+
74
151
  [id="plugins-{type}s-{plugin}-interval"]
75
152
  ===== `interval`
76
153
 
@@ -1,9 +1,12 @@
1
1
  # encoding: utf-8
2
2
  require "logstash/inputs/base"
3
3
  require "logstash/namespace"
4
+ require "open3"
4
5
  require "socket" # for Socket.gethostname
5
6
  require "stud/interval"
6
- require "rufus/scheduler"
7
+
8
+ require 'logstash/plugin_mixins/ecs_compatibility_support'
9
+ require "logstash/plugin_mixins/scheduler"
7
10
 
8
11
  # Periodically run a shell command and capture the whole output as an event.
9
12
  #
@@ -14,6 +17,9 @@ require "rufus/scheduler"
14
17
  #
15
18
  class LogStash::Inputs::Exec < LogStash::Inputs::Base
16
19
 
20
+ include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1)
21
+ include LogStash::PluginMixins::Scheduler
22
+
17
23
  config_name "exec"
18
24
 
19
25
  default :codec, "plain"
@@ -31,22 +37,25 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
31
37
  config :schedule, :validate => :string
32
38
 
33
39
  def register
34
- @logger.info("Registering Exec Input", :type => @type, :command => @command, :interval => @interval, :schedule => @schedule)
35
- @hostname = Socket.gethostname
36
- @io = nil
37
-
40
+ @hostname = Socket.gethostname.freeze
41
+
38
42
  if (@interval.nil? && @schedule.nil?) || (@interval && @schedule)
39
43
  raise LogStash::ConfigurationError, "exec input: either 'interval' or 'schedule' option must be defined."
40
44
  end
45
+
46
+ @host_name_field = ecs_select[disabled: 'host', v1: '[host][name]']
47
+ @process_command_line_field = ecs_select[disabled: 'command', v1: '[process][command_line]']
48
+ @process_exit_code_field = ecs_select[disabled: '[@metadata][exit_status]', v1: '[process][exit_code]']
49
+
50
+ # migrate elapsed time tracking to whole nanos, from legacy floating-point fractional seconds
51
+ @process_elapsed_time_field = ecs_select[disabled: nil, v1: '[@metadata][input][exec][process][elapsed_time]'] # in nanos
52
+ @legacy_duration_field = ecs_select[disabled: '[@metadata][duration]', v1: nil] # in seconds
41
53
  end # def register
42
54
 
43
55
  def run(queue)
44
56
  if @schedule
45
- @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
46
- @scheduler.cron @schedule do
47
- execute(queue)
48
- end
49
- @scheduler.join
57
+ scheduler.cron(@schedule) { execute(queue) }
58
+ scheduler.join
50
59
  else
51
60
  while !stop?
52
61
  duration = execute(queue)
@@ -56,13 +65,11 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
56
65
  end # def run
57
66
 
58
67
  def stop
59
- close_io()
60
- @scheduler.shutdown(:wait) if @scheduler
68
+ close_out_and_in
61
69
  end
62
70
 
63
71
  # Execute a given command
64
- # @param [String] A command string
65
- # @param [Array or Queue] A queue to append events to
72
+ # @param queue the LS queue to append events to
66
73
  def execute(queue)
67
74
  start = Time.now
68
75
  output = exit_status = nil
@@ -71,20 +78,21 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
71
78
  output, exit_status = run_command()
72
79
  rescue StandardError => e
73
80
  @logger.error("Error while running command",
74
- :command => @command, :e => e, :backtrace => e.backtrace)
81
+ :command => @command, :exception => e, :backtrace => e.backtrace)
75
82
  rescue Exception => e
76
83
  @logger.error("Exception while running command",
77
- :command => @command, :e => e, :backtrace => e.backtrace)
84
+ :command => @command, :exception => e, :backtrace => e.backtrace)
78
85
  end
79
- duration = Time.now - start
80
- @logger.debug? && @logger.debug("Command completed", :command => @command, :duration => duration)
86
+ duration = Time.now.to_r - start.to_r
87
+ @logger.debug? && @logger.debug("Command completed", :command => @command, :duration => duration.to_f)
81
88
  if output
82
89
  @codec.decode(output) do |event|
83
90
  decorate(event)
84
- event.set("host", @hostname)
85
- event.set("command", @command)
86
- event.set("[@metadata][duration]", duration)
87
- event.set("[@metadata][exit_status]", exit_status)
91
+ event.set(@host_name_field, @hostname) unless event.include?(@host_name_field)
92
+ event.set(@process_command_line_field, @command) unless event.include?(@process_command_line_field)
93
+ event.set(@process_exit_code_field, exit_status) unless event.include?(@process_exit_code_field)
94
+ event.set(@process_elapsed_time_field, to_nanos(duration)) if @process_elapsed_time_field
95
+ event.set(@legacy_duration_field, duration.to_f) if @legacy_duration_field
88
96
  queue << event
89
97
  end
90
98
  end
@@ -94,24 +102,30 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
94
102
  private
95
103
 
96
104
  def run_command
97
- @io = IO.popen(@command)
98
- output = @io.read
99
- @io.close # required in order to read $?
100
- exit_status = $?.exitstatus # should be threadsafe as per rb_thread_save_context
105
+ @p_in, @p_out, waiter = Open3.popen2(@command)
106
+ output = @p_out.read
107
+ exit_status = waiter.value.exitstatus
101
108
  [output, exit_status]
102
109
  ensure
103
- close_io()
110
+ close_out_and_in
104
111
  end
105
112
 
106
- # Close @io
107
- def close_io
108
- return if @io.nil? || @io.closed?
109
- @io.close
110
- @io = nil
113
+ def close_out_and_in
114
+ close_io(@p_out)
115
+ @p_out = nil
116
+ close_io(@p_in)
117
+ @p_in = nil
118
+ end
119
+
120
+ def close_io(io)
121
+ return if io.nil? || io.closed?
122
+ io.close
123
+ rescue => e
124
+ @logger.debug("ignoring exception raised while closing io", :io => io, :exception => e.class, :message => e.message)
111
125
  end
112
126
 
113
127
  # Wait until the end of the interval
114
- # @param [Integer] the duration of the last command executed
128
+ # @param duration [Integer] the duration of the last command executed
115
129
  def wait_until_end_of_interval(duration)
116
130
  # Sleep for the remainder of the interval, or 0 if the duration ran
117
131
  # longer than the interval.
@@ -124,5 +138,10 @@ class LogStash::Inputs::Exec < LogStash::Inputs::Base
124
138
  end
125
139
  end
126
140
 
141
+ # convert seconds to nanoseconds
142
+ # @param time_diff [Numeric] the (rational value) difference to convert
143
+ def to_nanos(time_diff)
144
+ (time_diff * 1_000_000).to_i
145
+ end
127
146
 
128
147
  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.3.2'
4
+ s.version = '3.6.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"
@@ -21,9 +21,11 @@ Gem::Specification.new do |s|
21
21
 
22
22
  # Gem dependencies
23
23
  s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
24
+ s.add_runtime_dependency 'logstash-mixin-ecs_compatibility_support', '~> 1.3'
25
+ s.add_runtime_dependency "logstash-mixin-scheduler", '~> 1.0'
26
+
24
27
  s.add_runtime_dependency 'stud', '~> 0.0.22'
25
28
  s.add_runtime_dependency 'logstash-codec-plain'
26
- s.add_runtime_dependency 'rufus-scheduler'
27
29
 
28
30
  s.add_development_dependency 'logstash-devutils'
29
31
  s.add_development_dependency 'timecop'
@@ -2,93 +2,142 @@
2
2
  require "timecop"
3
3
  require "time"
4
4
  require_relative "../spec_helper"
5
+ require "logstash/devutils/rspec/shared_examples"
6
+ require 'logstash/plugin_mixins/ecs_compatibility_support/spec_helper'
5
7
 
6
- describe LogStash::Inputs::Exec do
8
+ describe LogStash::Inputs::Exec, :ecs_compatibility_support do
7
9
 
8
- context "when register" do
9
- let(:input) { described_class.new("command" => "ls", "interval" => 0) }
10
- it "should not raise error if config is valid" do
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
10
+ ecs_compatibility_matrix(:disabled, :v1, :v8) do |ecs_select|
11
+
12
+ before(:each) do
13
+ allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(ecs_compatibility)
13
14
  end
14
- context "with an invalid config" do
15
- let(:input) { described_class.new("command" => "ls") }
16
- it "should raise error" do
17
- expect {input.register}.to raise_error(LogStash::ConfigurationError)
15
+
16
+ context "when register" do
17
+ let(:input) { described_class.new("command" => "ls", "interval" => 0) }
18
+
19
+ it "should not raise error if config is valid" do
20
+ # register will try to load jars and raise if it cannot find jars or if org.apache.log4j.spi.LoggingEvent class is not present
21
+ expect { input.register }.to_not raise_error
18
22
  end
19
- end
20
- end
21
23
 
22
- context "when operating normally" do
23
- let(:input) { described_class.new("command" => "ls", "interval" => 0) }
24
- let(:queue) { [] }
25
- let(:loggr) { double('loggr') }
26
-
27
- before :each do
28
- expect(described_class).to receive(:logger).and_return(loggr).exactly(7).times
29
- allow(loggr).to receive(:info)
30
- allow(loggr).to receive(:info?)
31
- allow(loggr).to receive(:warn)
32
- allow(loggr).to receive(:warn?)
33
- allow(loggr).to receive(:debug)
34
- allow(loggr).to receive(:debug?)
24
+ context "with an invalid config" do
25
+ let(:input) { described_class.new("command" => "ls") }
26
+ it "should raise error" do
27
+ expect { input.register }.to raise_error(LogStash::ConfigurationError)
28
+ end
29
+ end
35
30
  end
36
31
 
37
- it "enqueues some events" do
38
- input.register
39
- expect(loggr).not_to receive(:error)
32
+ context "when operating normally" do
33
+ let(:input) { described_class.new("command" => "echo 'hi!'", "interval" => 0) }
34
+ let(:queue) { [] }
40
35
 
41
- input.execute(queue)
36
+ before :each do
37
+ input.register
38
+ end
42
39
 
43
- expect(queue.size).not_to be_zero
44
- end
45
- end
40
+ it "enqueues some events" do
41
+ expect(input.logger).not_to receive(:error)
46
42
 
47
- context "when a command runs normally" do
48
- let(:input) { described_class.new("command" => "/bin/sh -c 'sleep 1; /bin/echo -n two; exit 3'", "interval" => 0) }
49
- let(:queue) { [] }
43
+ input.execute(queue)
50
44
 
51
- before do
52
- input.register
53
- input.execute(queue)
45
+ expect(queue.size).not_to be_zero
46
+ end
54
47
  end
55
48
 
56
- after do
57
- input.stop
58
- end
49
+ context "when command fails" do
50
+ let(:input) { described_class.new("command" => "invalid_command 1 2 3", "interval" => 0) }
51
+ let(:queue) { [] }
59
52
 
60
- it "has duration tracked" do
61
- expect(queue.pop.get('[@metadata][duration]')).to be > 1
62
- end
63
- it "has output as expected" do
64
- expect(queue.pop.get('message')).to eq "two"
53
+ before :each do
54
+ input.register
55
+ end
56
+
57
+ it "does not enqueue an event (in a non-Docker env)" do
58
+ expect(input.logger).to receive(:error).and_call_original
59
+
60
+ input.execute(queue)
61
+
62
+ expect(queue.map(&:to_hash)).to be_empty
63
+ end
65
64
  end
66
- it "has exit_status tracked" do
67
- expect(queue.pop.get('[@metadata][exit_status]')).to eq 3
65
+
66
+ context "when a command runs normally" do
67
+ let(:command) { "/bin/sh -c 'sleep 1; /bin/echo -n two; exit 3'" }
68
+ let(:input) { described_class.new("command" => command, "interval" => 0) }
69
+ let(:queue) { [] }
70
+
71
+ before do
72
+ input.register
73
+ input.execute(queue)
74
+ end
75
+
76
+ after do
77
+ input.stop
78
+ end
79
+
80
+ it "has duration (in seconds)" do
81
+ duration = queue.pop.get('[@metadata][duration]')
82
+ expect(duration).to be_a Float
83
+ expect(duration).to be > 1
84
+ expect(duration).to be < 3
85
+ end if ecs_select.active_mode == :disabled
86
+
87
+ it "reports process elapsed time (in nanos)" do
88
+ elapsed_time = queue.pop.get('[@metadata][input][exec][process][elapsed_time]')
89
+ expect(elapsed_time).to be_a Integer
90
+ expect(elapsed_time).to be > 1 * 1_000_000
91
+ expect(elapsed_time).to be < 3 * 1_000_000
92
+ end if ecs_select.active_mode != :disabled
93
+
94
+ it "has output as expected" do
95
+ expect(queue.pop.get('message')).to eq "two"
96
+ end
97
+
98
+ it "reports process command_line " do
99
+ if ecs_select.active_mode == :disabled
100
+ expect(queue.pop.get('command')).to eql command
101
+ else
102
+ expect(queue.pop.get('[process][command_line]')).to eql command
103
+ end
104
+ end
105
+
106
+ it "reports process exit_code" do
107
+ if ecs_select.active_mode == :disabled
108
+ expect(queue.pop.get('[@metadata][exit_status]')).to eq 3
109
+ else
110
+ expect(queue.pop.get('[process][exit_code]')).to eq 3
111
+ end
112
+ end
113
+
68
114
  end
69
115
 
70
116
  end
71
117
 
72
118
  context "when scheduling" do
73
- let(:input) { described_class.new("command" => "ls", "schedule" => "* * * * * UTC") }
119
+ let(:input) { described_class.new("command" => "ls --help", "schedule" => "5-6 * * * * UTC") }
74
120
  let(:queue) { [] }
75
121
 
76
122
  before do
123
+ Timecop.travel(Time.new(2000))
124
+ Timecop.scale(60)
77
125
  input.register
78
126
  end
79
127
 
128
+ after do
129
+ Timecop.return
130
+ end
131
+
80
132
  it "should properly schedule" do
81
- Timecop.travel(Time.new(2000))
82
- Timecop.scale(60)
83
133
  runner = Thread.new do
84
134
  input.run(queue)
85
135
  end
86
- sleep 3
136
+ sleep 10
87
137
  input.stop
88
138
  runner.kill
89
139
  runner.join
90
140
  expect(queue.size).to eq(2)
91
- Timecop.return
92
141
  end
93
142
  end
94
143
 
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.3.2
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-20 00:00:00.000000000 Z
11
+ date: 2022-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -35,36 +35,50 @@ dependencies:
35
35
  requirements:
36
36
  - - "~>"
37
37
  - !ruby/object:Gem::Version
38
- version: 0.0.22
39
- name: stud
38
+ version: '1.3'
39
+ name: logstash-mixin-ecs_compatibility_support
40
40
  prerelease: false
41
41
  type: :runtime
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.0.22
46
+ version: '1.3'
47
47
  - !ruby/object:Gem::Dependency
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  requirements:
50
- - - ">="
50
+ - - "~>"
51
51
  - !ruby/object:Gem::Version
52
- version: '0'
53
- name: logstash-codec-plain
52
+ version: '1.0'
53
+ name: logstash-mixin-scheduler
54
54
  prerelease: false
55
55
  type: :runtime
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ">="
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0'
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: 0.0.22
67
+ name: stud
68
+ prerelease: false
69
+ type: :runtime
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.0.22
61
75
  - !ruby/object:Gem::Dependency
62
76
  requirement: !ruby/object:Gem::Requirement
63
77
  requirements:
64
78
  - - ">="
65
79
  - !ruby/object:Gem::Version
66
80
  version: '0'
67
- name: rufus-scheduler
81
+ name: logstash-codec-plain
68
82
  prerelease: false
69
83
  type: :runtime
70
84
  version_requirements: !ruby/object:Gem::Requirement
@@ -140,8 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
154
  - !ruby/object:Gem::Version
141
155
  version: '0'
142
156
  requirements: []
143
- rubyforge_project:
144
- rubygems_version: 2.6.13
157
+ rubygems_version: 3.1.6
145
158
  signing_key:
146
159
  specification_version: 4
147
160
  summary: Captures the output of a shell command as an event