如何添加一个插件到Telegraf? [英] How to add a plugin to Telegraf?
问题描述
您好,我想知道是否有人已经准备好了为Influxdb添加一个插件来发送电子邮件。
我有我的代码正在工作。接下来我需要什么以及在哪里放置这些文件?
我发现我需要这样做:
类型ReadFile struct {
// buf [] byte
// MemoryBytes int64
// PID int
}
func(s * ReadFile)描述()字符串{
return这是一个测试插件,用于从文件读取数据并将它们发送到influxdb}
func(s * ReadFile)SampleConfig()string {
returnok = true#表示一切正常
}
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 =警报
[代理]
间隔=10s
#输出
[输出]
[outputs.influxdb]
url =http://127.0.0.1:8086#必填项。
database =summer#需要。
precision =s
#插件
[readFile]
如果您有我需要的清单,如何构建它,我在哪里存储文件或可能是一个示例,可能会非常有用。
谢谢! !
- >我收到了这个,它给了我更好的理解,我认为这可能会有帮助:
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
导入(
time
)
类型ReadFile结构{
计数器int64
}
func(s * TestPlugin)描述()string {
return这是一个测试插件,用于向插件写入数据到influxdb
}
func(s * Test插件)SampleConfig()string {
returnok = true#表示一切正常
}
func收集(acc telegraf.Accumulator)错误{
c:= time.Tick(10 * time.Second)
for now:= range c {
counter:= counter + 1
acc.Add(计数器,标签)
}
}
func init(){
inputs.Add(testPlugin,func()telegraf.Input {return & TestPlugin {}})
}
Hello I would to know if someone have all ready add a plugin to telegraf for Influxdb. I have my go code which is working. What do I need next and where to put theses files?
I've found that I need to do something like this:
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{} })
}
But is this my entire Go plugin or another file in Go to add with my Go program?
And where does the file.conf is store?
[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]
If you have a list of what I need, how to structure it, where I store file or maybe an example could be really helpful.
Thanks!!
-> 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{} })
}
这篇关于如何添加一个插件到Telegraf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!