gpt4 book ai didi

plugins - 如何向 Telegraf 添加插件?

转载 作者:IT王子 更新时间:2023-10-29 00:43:51 28 4
gpt4 key购买 nike

你好,我想知道是否有人已经准备好为 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{} })
}

关于plugins - 如何向 Telegraf 添加插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37437090/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com