From a34180459af91c97b1b0e885a69dec0e69e659db Mon Sep 17 00:00:00 2001 From: Steven Soroka Date: Fri, 28 Feb 2020 13:58:56 -0500 Subject: [PATCH] Close stdin on exit in execd input --- CHANGELOG.md | 1 + plugins/inputs/execd/README.md | 5 +++-- plugins/inputs/execd/execd.go | 10 ++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40bf18e9b..5b03ca072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [infiniband](/plugins/inputs/infiniband/README.md) - Contributed by @willfurnell - [modbus](/plugins/inputs/modbus/README.md) - Contributed by @garciaolais - [monit](/plugins/inputs/monit/README.md) - Contributed by @SirishaGopigiri +- [execd](/plugins/inputs/execd/README.md) - Contributed by @jgraichen #### New Processors diff --git a/plugins/inputs/execd/README.md b/plugins/inputs/execd/README.md index 1205fdd56..022311924 100644 --- a/plugins/inputs/execd/README.md +++ b/plugins/inputs/execd/README.md @@ -1,6 +1,7 @@ # Execd Input Plugin -The `execd` plugin runs an external program as a daemon. The programs must output metrics in any one of the accepted [Input Data Formats](https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md) on its standard output. +The `execd` plugin runs an external program as a daemon. The programs must output metrics in any one of the accepted +[Input Data Formats](https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md) on its standard output. The `signal` can be configured to send a signal the running daemon on each collection interval. @@ -9,11 +10,11 @@ Program output on standard error is mirrored to the telegraf log. ### Configuration: ```toml +[[inputs.execd]] ## Program to run as daemon command = ["telegraf-smartctl", "-d", "/dev/sda"] ## Define how the process is signaled on each collection interval. - ## Valid values are: ## "none" : Do not signal anything. ## The process must output metrics by itself. diff --git a/plugins/inputs/execd/execd.go b/plugins/inputs/execd/execd.go index 0d1fc7cc5..e852d045e 100644 --- a/plugins/inputs/execd/execd.go +++ b/plugins/inputs/execd/execd.go @@ -21,12 +21,13 @@ const sampleConfig = ` command = ["telegraf-smartctl", "-d", "/dev/sda"] ## Define how the process is signaled on each collection interval. - ## Valid values are: ## "none" : Do not signal anything. ## The process must output metrics by itself. - ## "STDIN" : Send a newline on STDIN. - ## "SIGHUP" : Send a HUP signal. Not available on Windows. + ## "STDIN" : Send a newline on STDIN. + ## "SIGHUP" : Send a HUP signal. Not available on Windows. + ## "SIGUSR1" : Send a USR1 signal. Not available on Windows. + ## "SIGUSR2" : Send a USR2 signal. Not available on Windows. signal = "none" ## Delay before the process is restarted after an unexpected termination @@ -100,9 +101,10 @@ func (e *Execd) cmdLoop(ctx context.Context) { select { case <-ctx.Done(): + e.stdin.Close() // Immediately exit process but with a graceful shutdown // period before killing - internal.WaitTimeout(e.cmd, 0) + internal.WaitTimeout(e.cmd, 200*time.Millisecond) return case err := <-done: log.Printf("E! [inputs.execd] Process %s terminated: %s", e.Command, err)