首页 文章

如何在Telegraf中添加插件?

提问于
浏览
2

你好,我想知道是否有人准备 add a plugin to telegraf for 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程序?

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]

如果你有一个我需要的列表,如何构建它,我存储文件的位置或者一个例子可能真的很有帮助 .

谢谢!!

2 回答

  • 1
    • 我收到了这个,它给了我一个更好的理解,我认为它可能会有所帮助:

    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 . 这将放置新的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{} })
    }
    
  • 2

    external plugin support已经打开了一个问题,可能是Telegraf 1.4.0的一部分 . 如果可能会加载external *.so files .

    在此之前,所有插件都应该合并到主repository via PRs中 . 已经有很多插件在审核过程中等待 . 这个模型显然不是很可扩展 .

相关问题