如何向 Telegraf 添加插件?

Phi*_*Dpt 6 plugins go influxdb

您好,我想知道是否有人准备好为 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{} })
 }
Run Code Online (Sandbox Code Playgroud)

但这是我的整个 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]
Run Code Online (Sandbox Code Playgroud)

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

谢谢!!

Phi*_*Dpt 4

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

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

“他的插件代码看起来不错。他需要将该文件放在 $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/testPlugin/testPlugin.go 中

他应该为插件编写一个测试并将其放置在 $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/testPlugin/testPlugin_test.go

完成后,他需要在 $GOPATH/src/github.com/influxdata/telegraf/plugin/inputs/all/all.go 注册插件

然后他应该make从 $GOPATH/src/github.com/influxdata/telegraf 运行。这会将新的 telegraf 二进制文件放置在 $GOPATH/bin/telegraf 中。

使用以下标志运行二进制文件以生成有效配置:

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

从那里,您可以通过传递示例配置来使用 -test 标志运行二进制文件:

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

这将输出要插入数据库的线路协议。”

-> 还有他谈到的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{} })
}
Run Code Online (Sandbox Code Playgroud)