2020-06-05 07:09:22 +08:00
package main
import (
"flag"
"fmt"
"os"
"time"
// TODO: import your plugins
// _ "github.com/my_github_user/my_plugin_repo/plugins/inputs/mypluginname"
"github.com/influxdata/telegraf/plugins/common/shim"
)
var pollInterval = flag . Duration ( "poll_interval" , 1 * time . Second , "how often to send metrics" )
2021-04-20 23:24:24 +08:00
var pollIntervalDisabled = flag . Bool ( "poll_interval_disabled" , false , "set to true to disable polling. You want to use this when you are sending metrics on your own schedule" )
2020-06-05 07:09:22 +08:00
var configFile = flag . String ( "config" , "" , "path to the config file for this plugin" )
var err error
2022-10-12 00:31:44 +08:00
// This is designed to be simple; Just change the import above, and you're good.
2020-06-05 07:09:22 +08:00
//
// However, if you want to do all your config in code, you can like so:
//
2022-02-22 23:11:30 +08:00
// // initialize your plugin with any settings you want
2022-09-09 02:49:36 +08:00
//
// myInput := &mypluginname.MyPlugin{
// DefaultSettingHere: 3,
// }
2020-06-05 07:09:22 +08:00
//
// shim := shim.New()
//
// shim.AddInput(myInput)
//
2021-06-03 21:53:15 +08:00
// // now the shim.Run() call as below. Note the shim is only intended to run a single plugin.
2020-06-05 07:09:22 +08:00
func main ( ) {
// parse command line options
flag . Parse ( )
if * pollIntervalDisabled {
* pollInterval = shim . PollIntervalDisabled
}
// create the shim. This is what will run your plugins.
2022-02-22 23:11:30 +08:00
shimLayer := shim . New ( )
2020-06-05 07:09:22 +08:00
// If no config is specified, all imported plugins are loaded.
2022-02-22 23:11:30 +08:00
// otherwise, follow what the config asks for.
2020-06-05 07:09:22 +08:00
// Check for settings from a config toml file,
// (or just use whatever plugins were imported above)
2022-10-12 00:31:44 +08:00
if err = shimLayer . LoadConfig ( configFile ) ; err != nil {
2020-06-05 07:09:22 +08:00
fmt . Fprintf ( os . Stderr , "Err loading input: %s\n" , err )
os . Exit ( 1 )
}
2022-10-12 00:31:44 +08:00
// run a single plugin until stdin closes, or we receive a termination signal
if err = shimLayer . Run ( * pollInterval ) ; err != nil {
2020-06-05 07:09:22 +08:00
fmt . Fprintf ( os . Stderr , "Err: %s\n" , err )
os . Exit ( 1 )
}
}