如何向 Telegraf 添加插件?

How to add a plugin to Telegraf?

你好,我想知道是否有人已经准备好为 Influxdb 添加一个插件到 telegraf。 我有我的 go 代码,它正在工作。接下来我需要什么以及将这些文件放在哪里?

我发现我需要做这样的事情:

type ReadFile struct {
    //buf []byte
    //MemoryBytes int64
   //PID int
}

func (s *ReadFile) Description() string {
   return "This is a test plugin to read data from a file and send them to influxdb" }

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

func Gather(acc plugins.Accumulator) error {

     readFile(alarmFile)

    acc.Add("alarm", result_of_readFile_here, tags)
    }
  }

    func init() {
    plugins.Add("readFile", func() plugins.Plugin { &ReadFile{} })
 }

但这是我的整个 Go 插件还是 Go 中要添加到我的 Go 程序中的另一个文件?

file.conf 存储在哪里?

[tags]
dc = "alarm"

[agent]
interval = "10s"

# OUTPUTS
[outputs]
[outputs.influxdb]
url = "http://127.0.0.1:8086" # required.
database = "summer" # required.
precision = "s"

# PLUGINS
[readFile]

如果您有我需要的列表、如何构建它、我存储文件的位置或者示例可能真的很有帮助。

谢谢!!

-> 我收到了这个,它让我有了更好的理解,我认为它可能会有所帮助:

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."

-> 而他所说的testPlugin.go

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{} })
}

external plugin support which might be part of Telegraf 1.4.0. If will probably load external *.so files 有一个未解决的问题。

到那时所有的插件都应该合并到 master repository via PRs 中。已经有很多插件在等待审核过程。这种模式显然不是很可扩展。