dongye1143 2016-05-25 12:25
浏览 184
已采纳

如何将插件添加到Telegraf?

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

  • 写回答

2条回答 默认 最新

  • dongqian7545 2016-05-30 08:59
    关注

    -> 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{} })
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序