How to add a plugin to Telegraf?

huangapple go评论87阅读模式
英文:

How to add a plugin to Telegraf?

问题

你好,我想知道是否有人已经为Telegraf添加了InfluxDB插件。我有一个正常工作的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文件?

文件.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]

如果你有我需要的清单,如何组织它,文件应该存储在哪里,或者有一个示例,都会非常有帮助。

谢谢!

英文:

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!!

答案1

得分: 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中注册插件。

然后,他应该在$GOPATH/src/github.com/influxdata/telegraf中运行make命令。这将在$GOPATH/bin/telegraf中放置新的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{} })
}
英文:

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

答案2

得分: 3

有一个关于外部插件支持的问题已经被提出,可能会成为Telegraf 1.4.0的一部分。它可能会加载外部的*.so文件

在那之前,所有的插件都应该通过[Pull Request](PR)合并到主存储库中。已经有很多插件正在等待审核。显然,这个模型的可扩展性不是很好。

英文:

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.

huangapple
  • 本文由 发表于 2016年5月25日 20:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/37437090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定