I also wrote the processors.execd
plugin in Go just for fun:
package main
import (
"fmt"
"os"
"time"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/plugins/serializers"
)
func main() {
parser := influx.NewStreamParser(os.Stdin)
serializer, _ := serializers.NewInfluxSerializer()
for {
metric, err := parser.Next()
if err != nil {
if err == influx.EOF {
return // stream ended
}
if parseErr, isParseError := err.(*influx.ParseError); isParseError {
fmt.Fprintf(os.Stderr, "parse ERR %v\n", parseErr)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "ERR %v\n", err)
os.Exit(1)
}
now := time.Now() // now timestamp
original := metric.Time() // get original timestamp
metric.AddField("original_ts", original.UnixNano())
metric.AddField("inserted_ts", now.UnixNano())
metric.SetTime(now) // replace timestamp
b, err := serializer.Serialize(metric)
if err != nil {
fmt.Fprintf(os.Stderr, "ERR %v\n", err)
os.Exit(1)
}
fmt.Fprint(os.Stdout, string(b))
}
}
Just build the Go file, copy the executable to the Telegraf folder and add the config.
This is the config snippet for the execd plugin for the telegraf.conf
file:
[[processors.execd]]
command = ["processor.exe"]