'How to write simple telegraf input plugin

Hello I'm beginner to telegraf, Can anyone tell me how to write simple telegraf input plugin. ("Hello World")...



Solution 1:[1]

Get telegraf code and make

  1. To go your go-project dir, go/src/
  2. go get -d github.com/influxdata/telegraf
  3. cd github.com/influxdata/telegraf
  4. make

For Creating a simple plugin

  1. cd telegraf
  2. cd plugins/inputs/
  3. create a package (folder) -> mkdir sample
  4. cd sample
  5. touch sample.go and

write below code in it

package sample

import (
    "github.com/influxdata/telegraf"
    "github.com/influxdata/telegraf/plugins/inputs"
)

type Sample struct {
    Message string `toml:"message"`
}

func (s *Sample) Description() string {
    return "Intended to test"
}

var SampleConfig = `
##You can also change the message if you want to print different than Hello world  by uncommenting below line and change message.
# message = "Hello"
`

func (s *Sample) SampleConfig() string {
    return SampleConfig
}

func (s *Sample) Init() error {
    return nil
}

func (s *Sample) Gather(acc telegraf.Accumulator) error {
    greet := "Your Message is " + s.Message

    fields := make(map[string]interface{})
    fields["Message"] = greet
    //tags := make(map[string]string)

    acc.AddFields("sample", fields, nil)

    return nil
}

func init() {
    inputs.Add("sample", func() telegraf.Input { return &Sample{"Hello-World"} })
}

Save the code

You need to add/register path of your newly created package(plugin) in all.go present in

go/src/ github.com/influxdata/telegraf/plugins/inputs/all/all.go

vi /go/src/github.com/influxdata/telegraf/plugins/inputs/all/all.go

As we have created sample input plugin we need to add its path at the end in import, add the below line at end of import

_ "github.com/influxdata/telegraf/plugins/inputs/sample"

Then all.go file will look like

import(
.
.
.
.
  _ "github.com/influxdata/telegraf/plugins/inputs/sample"
)

Now, come back to telegraf directory

cd /go/src/github.com/influxdata/telegraf

make

this will make new binary of telegraf

Note: For Output Plugin we are using a file plugin where we put output in File

Setting Input and Output plugin for telegraf.conf file

./telegraf --input-filter=sample --output-filter=file config > telegraf.conf

vi telegraf.conf

change in [[outputs.file]]

1. files = ["stdout", "/tmp/Your-Output-Filename.json"]

2. data_format = "json"

save it & run using

./telegraf --config telegraf.conf

You can see the output like, enter image description here

Now go to the output file directory open the file and see the content of in it by,

vi /tmp/Your-Output-Filename.json

Its like, enter image description here

NOTE: You can also change the message in SampleConfig() by uncommenting it ( SampleConfig() has highest priority), while running telegraf binary it reads data from here, if its not found here(if its commented) then it use the values which is passed in init()

Each time you changes the code you have to again make the code

Thank You !!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Somesh Mahajan