2020-02-29 02:46:03 +08:00
|
|
|
package execd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2020-05-05 02:09:10 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2020-06-05 07:09:22 +08:00
|
|
|
"github.com/influxdata/telegraf/internal/process"
|
2020-02-29 02:46:03 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2020-05-06 05:43:45 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/influx"
|
2020-02-29 02:46:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const sampleConfig = `
|
|
|
|
|
## 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.
|
2020-02-29 02:58:56 +08:00
|
|
|
## "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.
|
2020-02-29 02:46:03 +08:00
|
|
|
signal = "none"
|
|
|
|
|
|
|
|
|
|
## Delay before the process is restarted after an unexpected termination
|
|
|
|
|
restart_delay = "10s"
|
|
|
|
|
|
|
|
|
|
## Data format to consume.
|
|
|
|
|
## Each data format has its own unique set of configuration options, read
|
|
|
|
|
## more about them here:
|
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
|
|
|
|
data_format = "influx"
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type Execd struct {
|
|
|
|
|
Command []string
|
|
|
|
|
Signal string
|
2020-05-05 02:09:10 +08:00
|
|
|
RestartDelay config.Duration
|
2020-02-29 02:46:03 +08:00
|
|
|
|
2020-06-05 07:09:22 +08:00
|
|
|
process *process.Process
|
|
|
|
|
acc telegraf.Accumulator
|
|
|
|
|
parser parsers.Parser
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Execd) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Execd) Description() string {
|
|
|
|
|
return "Run executable as long-running input plugin"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Execd) SetParser(parser parsers.Parser) {
|
|
|
|
|
e.parser = parser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Execd) Start(acc telegraf.Accumulator) error {
|
|
|
|
|
e.acc = acc
|
|
|
|
|
if len(e.Command) == 0 {
|
2020-05-05 02:09:10 +08:00
|
|
|
return fmt.Errorf("FATAL no command specified")
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 07:09:22 +08:00
|
|
|
var err error
|
|
|
|
|
e.process, err = process.New(e.Command)
|
2020-02-29 02:46:03 +08:00
|
|
|
if err != nil {
|
2020-06-05 07:09:22 +08:00
|
|
|
return fmt.Errorf("Error creating new process: %w", err)
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 07:09:22 +08:00
|
|
|
e.process.RestartDelay = time.Duration(e.RestartDelay)
|
|
|
|
|
e.process.ReadStdoutFn = e.cmdReadOut
|
|
|
|
|
e.process.ReadStderrFn = e.cmdReadErr
|
2020-02-29 02:46:03 +08:00
|
|
|
|
2020-06-05 07:09:22 +08:00
|
|
|
if err = e.process.Start(); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to start process %s: %w", e.Command, err)
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
2020-05-05 02:09:10 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 07:09:22 +08:00
|
|
|
func (e *Execd) Stop() {
|
|
|
|
|
e.process.Stop()
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Execd) cmdReadOut(out io.Reader) {
|
2020-05-06 05:43:45 +08:00
|
|
|
if _, isInfluxParser := e.parser.(*influx.Parser); isInfluxParser {
|
|
|
|
|
// work around the lack of built-in streaming parser. :(
|
|
|
|
|
e.cmdReadOutStream(out)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 02:46:03 +08:00
|
|
|
scanner := bufio.NewScanner(out)
|
|
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
metrics, err := e.parser.Parse(scanner.Bytes())
|
|
|
|
|
if err != nil {
|
2020-05-05 02:09:10 +08:00
|
|
|
e.acc.AddError(fmt.Errorf("Parse error: %s", err))
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, metric := range metrics {
|
|
|
|
|
e.acc.AddMetric(metric)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2020-05-05 02:09:10 +08:00
|
|
|
e.acc.AddError(fmt.Errorf("Error reading stdout: %s", err))
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 05:43:45 +08:00
|
|
|
func (e *Execd) cmdReadOutStream(out io.Reader) {
|
|
|
|
|
parser := influx.NewStreamParser(out)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
metric, err := parser.Next()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == influx.EOF {
|
|
|
|
|
break // stream ended
|
|
|
|
|
}
|
|
|
|
|
if parseErr, isParseError := err.(*influx.ParseError); isParseError {
|
|
|
|
|
// parse error.
|
|
|
|
|
e.acc.AddError(parseErr)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// some non-recoverable error?
|
|
|
|
|
e.acc.AddError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e.acc.AddMetric(metric)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 02:46:03 +08:00
|
|
|
func (e *Execd) cmdReadErr(out io.Reader) {
|
|
|
|
|
scanner := bufio.NewScanner(out)
|
|
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2020-06-05 07:09:22 +08:00
|
|
|
log.Printf("[inputs.execd] stderr: %q", scanner.Text())
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2020-05-05 02:09:10 +08:00
|
|
|
e.acc.AddError(fmt.Errorf("Error reading stderr: %s", err))
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
inputs.Add("execd", func() telegraf.Input {
|
|
|
|
|
return &Execd{
|
|
|
|
|
Signal: "none",
|
2020-05-05 02:09:10 +08:00
|
|
|
RestartDelay: config.Duration(10 * time.Second),
|
2020-02-29 02:46:03 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|