2020-05-04 18:09:10 +00:00
|
|
|
package shim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/agent"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type empty struct{}
|
|
|
|
|
|
|
|
var (
|
2020-05-05 14:14:57 +00:00
|
|
|
stdout io.Writer = os.Stdout
|
|
|
|
stdin io.Reader = os.Stdin
|
|
|
|
forever = 100 * 365 * 24 * time.Hour
|
2020-05-04 18:09:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PollIntervalDisabled is used to indicate that you want to disable polling,
|
|
|
|
// as opposed to duration 0 meaning poll constantly.
|
|
|
|
PollIntervalDisabled = time.Duration(0)
|
|
|
|
)
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
// Shim allows you to wrap your inputs and run them as if they were part of Telegraf,
|
|
|
|
// except built externally.
|
2020-05-04 18:09:10 +00:00
|
|
|
type Shim struct {
|
2020-05-05 14:14:57 +00:00
|
|
|
Inputs []telegraf.Input
|
|
|
|
gatherPromptChans []chan empty
|
|
|
|
metricCh chan telegraf.Metric
|
2020-05-04 18:09:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
// New creates a new shim interface
|
2020-05-04 18:09:10 +00:00
|
|
|
func New() *Shim {
|
|
|
|
return &Shim{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddInput adds the input to the shim. Later calls to Run() will run this input.
|
|
|
|
func (s *Shim) AddInput(input telegraf.Input) error {
|
|
|
|
if p, ok := input.(telegraf.Initializer); ok {
|
|
|
|
err := p.Init()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to init input: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Inputs = append(s.Inputs, input)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddInputs adds multiple inputs to the shim. Later calls to Run() will run these.
|
|
|
|
func (s *Shim) AddInputs(newInputs []telegraf.Input) error {
|
|
|
|
for _, inp := range newInputs {
|
|
|
|
if err := s.AddInput(inp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the input plugins..
|
|
|
|
func (s *Shim) Run(pollInterval time.Duration) error {
|
2020-05-05 14:14:57 +00:00
|
|
|
// context is used only to close the stdin reader. everything else cascades
|
|
|
|
// from that point and closes cleanly when it's done.
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.metricCh = make(chan telegraf.Metric, 1)
|
|
|
|
|
2020-05-04 18:09:10 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
collectMetricsPrompt := make(chan os.Signal, 1)
|
2020-05-05 14:14:57 +00:00
|
|
|
listenForCollectMetricsSignals(ctx, collectMetricsPrompt)
|
2020-05-04 18:09:10 +00:00
|
|
|
|
|
|
|
serializer := influx.NewSerializer()
|
|
|
|
|
|
|
|
for _, input := range s.Inputs {
|
|
|
|
wrappedInput := inputShim{Input: input}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
acc := agent.NewAccumulator(wrappedInput, s.metricCh)
|
2020-05-04 18:09:10 +00:00
|
|
|
acc.SetPrecision(time.Nanosecond)
|
|
|
|
|
|
|
|
if serviceInput, ok := input.(telegraf.ServiceInput); ok {
|
|
|
|
if err := serviceInput.Start(acc); err != nil {
|
|
|
|
return fmt.Errorf("failed to start input: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gatherPromptCh := make(chan empty, 1)
|
2020-05-05 14:14:57 +00:00
|
|
|
s.gatherPromptChans = append(s.gatherPromptChans, gatherPromptCh)
|
2020-05-04 18:09:10 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(input telegraf.Input) {
|
|
|
|
startGathering(ctx, input, acc, gatherPromptCh, pollInterval)
|
|
|
|
if serviceInput, ok := input.(telegraf.ServiceInput); ok {
|
|
|
|
serviceInput.Stop()
|
|
|
|
}
|
2020-05-05 14:14:57 +00:00
|
|
|
close(gatherPromptCh)
|
2020-05-04 18:09:10 +00:00
|
|
|
wg.Done()
|
|
|
|
}(input)
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
go s.stdinCollectMetricsPrompt(ctx, cancel, collectMetricsPrompt)
|
|
|
|
go s.closeMetricChannelWhenInputsFinish(&wg)
|
2020-05-04 18:09:10 +00:00
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
2020-05-05 14:14:57 +00:00
|
|
|
case <-quit: // user-triggered quit
|
2020-05-04 18:09:10 +00:00
|
|
|
// cancel, but keep looping until the metric channel closes.
|
|
|
|
cancel()
|
2020-05-05 14:14:57 +00:00
|
|
|
case _, open := <-collectMetricsPrompt:
|
|
|
|
if !open { // stdin-close-triggered quit
|
|
|
|
cancel()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.collectMetrics(ctx)
|
|
|
|
case m, open := <-s.metricCh:
|
2020-05-04 18:09:10 +00:00
|
|
|
if !open {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
b, err := serializer.Serialize(m)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to serialize metric: %s", err)
|
|
|
|
}
|
|
|
|
// Write this to stdout
|
|
|
|
fmt.Fprint(stdout, string(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasQuit(ctx context.Context) bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
func (s *Shim) stdinCollectMetricsPrompt(ctx context.Context, cancel context.CancelFunc, collectMetricsPrompt chan<- os.Signal) {
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
close(collectMetricsPrompt)
|
|
|
|
}()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdin)
|
2020-05-04 18:09:10 +00:00
|
|
|
// for every line read from stdin, make sure we're not supposed to quit,
|
|
|
|
// then push a message on to the collectMetricsPrompt
|
2020-05-05 14:14:57 +00:00
|
|
|
for scanner.Scan() {
|
2020-05-04 18:09:10 +00:00
|
|
|
// first check if we should quit
|
|
|
|
if hasQuit(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// now push a non-blocking message to trigger metric collection.
|
|
|
|
pushCollectMetricsRequest(collectMetricsPrompt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushCollectMetricsRequest pushes a non-blocking (nil) message to the
|
|
|
|
// collectMetricsPrompt channel to trigger metric collection.
|
2020-05-05 14:14:57 +00:00
|
|
|
// The channel is defined with a buffer of 1, so while it's full, subsequent
|
2020-05-04 18:09:10 +00:00
|
|
|
// requests are discarded.
|
|
|
|
func pushCollectMetricsRequest(collectMetricsPrompt chan<- os.Signal) {
|
|
|
|
select {
|
|
|
|
case collectMetricsPrompt <- nil:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
func (s *Shim) collectMetrics(ctx context.Context) {
|
2020-05-04 18:09:10 +00:00
|
|
|
if hasQuit(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-05 14:14:57 +00:00
|
|
|
for i := 0; i < len(s.gatherPromptChans); i++ {
|
2020-05-04 18:09:10 +00:00
|
|
|
// push a message out to each channel to collect metrics. don't block.
|
|
|
|
select {
|
2020-05-05 14:14:57 +00:00
|
|
|
case s.gatherPromptChans[i] <- empty{}:
|
2020-05-04 18:09:10 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startGathering(ctx context.Context, input telegraf.Input, acc telegraf.Accumulator, gatherPromptCh <-chan empty, pollInterval time.Duration) {
|
|
|
|
if pollInterval == PollIntervalDisabled {
|
|
|
|
return // don't poll
|
|
|
|
}
|
|
|
|
t := time.NewTicker(pollInterval)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
// give priority to stopping.
|
|
|
|
if hasQuit(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// see what's up
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2020-05-05 14:14:57 +00:00
|
|
|
case _, open := <-gatherPromptCh:
|
|
|
|
if !open {
|
|
|
|
// stdin has closed.
|
|
|
|
return
|
|
|
|
}
|
2020-05-04 18:09:10 +00:00
|
|
|
if err := input.Gather(acc); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to gather metrics: %s", err)
|
|
|
|
}
|
|
|
|
case <-t.C:
|
|
|
|
if err := input.Gather(acc); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to gather metrics: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig loads and adds the inputs to the shim
|
|
|
|
func (s *Shim) LoadConfig(filePath *string) error {
|
|
|
|
loadedInputs, err := LoadConfig(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.AddInputs(loadedInputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultImportedPlugins defaults to whatever plugins happen to be loaded and
|
|
|
|
// have registered themselves with the registry. This makes loading plugins
|
|
|
|
// without having to define a config dead easy.
|
|
|
|
func DefaultImportedPlugins() (i []telegraf.Input, e error) {
|
|
|
|
for _, inputCreatorFunc := range inputs.Inputs {
|
|
|
|
i = append(i, inputCreatorFunc())
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig loads the config and returns inputs that later need to be loaded.
|
|
|
|
func LoadConfig(filePath *string) ([]telegraf.Input, error) {
|
2020-05-05 14:14:57 +00:00
|
|
|
if filePath == nil || *filePath == "" {
|
2020-05-04 18:09:10 +00:00
|
|
|
return DefaultImportedPlugins()
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile(*filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := struct {
|
|
|
|
Inputs map[string][]toml.Primitive
|
|
|
|
}{}
|
|
|
|
|
|
|
|
md, err := toml.Decode(string(b), &conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedInputs, err := loadConfigIntoInputs(md, conf.Inputs)
|
|
|
|
|
|
|
|
if len(md.Undecoded()) > 0 {
|
|
|
|
fmt.Fprintf(stdout, "Some plugins were loaded but not used: %q\n", md.Undecoded())
|
|
|
|
}
|
|
|
|
return loadedInputs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfigIntoInputs(md toml.MetaData, inputConfigs map[string][]toml.Primitive) ([]telegraf.Input, error) {
|
|
|
|
renderedInputs := []telegraf.Input{}
|
|
|
|
|
|
|
|
for name, primitives := range inputConfigs {
|
|
|
|
inputCreator, ok := inputs.Inputs[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("unknown input " + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, primitive := range primitives {
|
|
|
|
inp := inputCreator()
|
|
|
|
// Parse specific configuration
|
|
|
|
if err := md.PrimitiveDecode(primitive, inp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
renderedInputs = append(renderedInputs, inp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return renderedInputs, nil
|
|
|
|
}
|
2020-05-05 14:14:57 +00:00
|
|
|
|
|
|
|
func (s *Shim) closeMetricChannelWhenInputsFinish(wg *sync.WaitGroup) {
|
|
|
|
wg.Wait()
|
|
|
|
close(s.metricCh)
|
|
|
|
}
|