How to add a plugin to Telegraf?

巧了我就是萌 提交于 2019-12-05 14:03:02

-> I receive this, it gave me a better understanding, I think it could be helpful:

https://github.com/influxdata/telegraf/blob/master/CONTRIBUTING.md

"His plugin code looks good to go. He needs to place that file in $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/testPlugin/testPlugin.go

He should write a test for the plugin and place it at $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/testPlugin/testPlugin_test.go

After this is complete he needs to register the plugin at $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/all/all.go

Then he should run make from $GOPATH/src/github.com/influxdata/telegraf. This will place the new telegraf binary in $GOPATH/bin/telegraf.

Run the binary with the following flags to generate the valid configuration:

$GOPATH/bin/telegraf -sample-config -input-filter testPlugin -output-filter influxdb > testPlugin_config.conf

From there you can run the binary with the -test flag by passing it the sample config:

$GOPATH/bin/telegraf -config testPlugin_config.conf -test

This will output the line protocol that is to be inserted into the database."

-> And the testPlugin.go that he talks about:

package testPlugin

import (
    "time"
)

type ReadFile struct {
 counter int64
}

func (s *TestPlugin) Description() string {
  return "This is a test plugin to write data to influxdb with a plugin"
}

func (s *TestPlugin) SampleConfig() string {
  return "ok = true # indicate if everything is fine"
}

func Gather(acc telegraf.Accumulator) error {

c := time.Tick(10 * time.Second)
for now := range c {

    counter := counter + 1
    acc.Add("counter",counter, tags)
 }
} 

func init() {
  inputs.Add("testPlugin", func() telegraf.Input { return &TestPlugin{} })
}

There's an opened issue for external plugin support which might be part of Telegraf 1.4.0. If will probably load external *.so files.

Until then all plugins are supposed to be merged into master repository via PRs. There are already many plugins waiting in review process. This model is obviously not very scalable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!