Add SIGHUP support to reload telegraf config

closes #539
This commit is contained in:
Thibault Cohen 2016-01-17 03:08:02 -05:00 committed by Cameron Sparr
parent bc71e956a5
commit 551db20657
2 changed files with 127 additions and 112 deletions

View File

@ -9,6 +9,7 @@
- [#534](https://github.com/influxdata/telegraf/pull/534): NSQ input plugin. Thanks @allingeek! - [#534](https://github.com/influxdata/telegraf/pull/534): NSQ input plugin. Thanks @allingeek!
- [#494](https://github.com/influxdata/telegraf/pull/494): Graphite output plugin. Thanks @titilambert! - [#494](https://github.com/influxdata/telegraf/pull/494): Graphite output plugin. Thanks @titilambert!
- AMQP SSL support. Thanks @ekini! - AMQP SSL support. Thanks @ekini!
- [#539](https://github.com/influxdata/telegraf/pull/539): Reload config on SIGHUP. Thanks @titilambert!
### Bugfixes ### Bugfixes
- [#506](https://github.com/influxdb/telegraf/pull/506): Ping input doesn't return response time metric when timeout. Thanks @titilambert! - [#506](https://github.com/influxdb/telegraf/pull/506): Ping input doesn't return response time metric when timeout. Thanks @titilambert!

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
"syscall"
"github.com/influxdb/telegraf" "github.com/influxdb/telegraf"
"github.com/influxdb/telegraf/internal/config" "github.com/influxdb/telegraf/internal/config"
@ -82,143 +83,156 @@ Examples:
` `
func main() { func main() {
flag.Usage = usageExit reload := make(chan bool, 1)
flag.Parse() reload <- true
for <-reload {
reload <- false
flag.Usage = usageExit
flag.Parse()
if flag.NFlag() == 0 { if flag.NFlag() == 0 {
usageExit() usageExit()
} }
var inputFilters []string var inputFilters []string
if *fInputFiltersLegacy != "" { if *fInputFiltersLegacy != "" {
inputFilter := strings.TrimSpace(*fInputFiltersLegacy) inputFilter := strings.TrimSpace(*fInputFiltersLegacy)
inputFilters = strings.Split(":"+inputFilter+":", ":") inputFilters = strings.Split(":"+inputFilter+":", ":")
} }
if *fInputFilters != "" { if *fInputFilters != "" {
inputFilter := strings.TrimSpace(*fInputFilters) inputFilter := strings.TrimSpace(*fInputFilters)
inputFilters = strings.Split(":"+inputFilter+":", ":") inputFilters = strings.Split(":"+inputFilter+":", ":")
} }
var outputFilters []string var outputFilters []string
if *fOutputFiltersLegacy != "" { if *fOutputFiltersLegacy != "" {
outputFilter := strings.TrimSpace(*fOutputFiltersLegacy) outputFilter := strings.TrimSpace(*fOutputFiltersLegacy)
outputFilters = strings.Split(":"+outputFilter+":", ":") outputFilters = strings.Split(":"+outputFilter+":", ":")
} }
if *fOutputFilters != "" { if *fOutputFilters != "" {
outputFilter := strings.TrimSpace(*fOutputFilters) outputFilter := strings.TrimSpace(*fOutputFilters)
outputFilters = strings.Split(":"+outputFilter+":", ":") outputFilters = strings.Split(":"+outputFilter+":", ":")
} }
if *fVersion { if *fVersion {
v := fmt.Sprintf("Telegraf - Version %s", Version) v := fmt.Sprintf("Telegraf - Version %s", Version)
fmt.Println(v) fmt.Println(v)
return return
} }
if *fSampleConfig { if *fSampleConfig {
config.PrintSampleConfig(inputFilters, outputFilters) config.PrintSampleConfig(inputFilters, outputFilters)
return return
} }
if *fUsage != "" { if *fUsage != "" {
if err := config.PrintInputConfig(*fUsage); err != nil { if err := config.PrintInputConfig(*fUsage); err != nil {
if err2 := config.PrintOutputConfig(*fUsage); err2 != nil { if err2 := config.PrintOutputConfig(*fUsage); err2 != nil {
log.Fatalf("%s and %s", err, err2) log.Fatalf("%s and %s", err, err2)
}
}
return
}
var (
c *config.Config
err error
)
if *fConfig != "" {
c = config.NewConfig()
c.OutputFilters = outputFilters
c.InputFilters = inputFilters
err = c.LoadConfig(*fConfig)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println("Usage: Telegraf")
flag.PrintDefaults()
return
}
if *fConfigDirectoryLegacy != "" {
err = c.LoadDirectory(*fConfigDirectoryLegacy)
if err != nil {
log.Fatal(err)
} }
} }
return
}
var ( if *fConfigDirectory != "" {
c *config.Config err = c.LoadDirectory(*fConfigDirectory)
err error if err != nil {
) log.Fatal(err)
}
}
if len(c.Outputs) == 0 {
log.Fatalf("Error: no outputs found, did you provide a valid config file?")
}
if len(c.Inputs) == 0 {
log.Fatalf("Error: no plugins found, did you provide a valid config file?")
}
if *fConfig != "" { ag, err := telegraf.NewAgent(c)
c = config.NewConfig()
c.OutputFilters = outputFilters
c.InputFilters = inputFilters
err = c.LoadConfig(*fConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else {
fmt.Println("Usage: Telegraf")
flag.PrintDefaults()
return
}
if *fConfigDirectoryLegacy != "" { if *fDebug {
err = c.LoadDirectory(*fConfigDirectoryLegacy) ag.Config.Agent.Debug = true
}
if *fQuiet {
ag.Config.Agent.Quiet = true
}
if *fTest {
err = ag.Test()
if err != nil {
log.Fatal(err)
}
return
}
err = ag.Connect()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
if *fConfigDirectory != "" { shutdown := make(chan struct{})
err = c.LoadDirectory(*fConfigDirectory) signals := make(chan os.Signal)
if err != nil { signal.Notify(signals, os.Interrupt, syscall.SIGHUP)
log.Fatal(err) go func() {
} sig := <-signals
} if sig == os.Interrupt {
if len(c.Outputs) == 0 { close(shutdown)
log.Fatalf("Error: no outputs found, did you provide a valid config file?") }
} if sig == syscall.SIGHUP {
if len(c.Inputs) == 0 { log.Printf("Reloading Telegraf config\n")
log.Fatalf("Error: no plugins found, did you provide a valid config file?") <-reload
} reload <- true
close(shutdown)
}
}()
ag, err := telegraf.NewAgent(c) log.Printf("Starting Telegraf (version %s)\n", Version)
if err != nil { log.Printf("Loaded outputs: %s", strings.Join(c.OutputNames(), " "))
log.Fatal(err) log.Printf("Loaded plugins: %s", strings.Join(c.InputNames(), " "))
} log.Printf("Tags enabled: %s", c.ListTags())
if *fDebug { if *fPidfile != "" {
ag.Config.Agent.Debug = true f, err := os.Create(*fPidfile)
} if err != nil {
log.Fatalf("Unable to create pidfile: %s", err)
}
if *fQuiet { fmt.Fprintf(f, "%d\n", os.Getpid())
ag.Config.Agent.Quiet = true
}
if *fTest { f.Close()
err = ag.Test()
if err != nil {
log.Fatal(err)
}
return
}
err = ag.Connect()
if err != nil {
log.Fatal(err)
}
shutdown := make(chan struct{})
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt)
go func() {
<-signals
close(shutdown)
}()
log.Printf("Starting Telegraf (version %s)\n", Version)
log.Printf("Loaded outputs: %s", strings.Join(c.OutputNames(), " "))
log.Printf("Loaded plugins: %s", strings.Join(c.InputNames(), " "))
log.Printf("Tags enabled: %s", c.ListTags())
if *fPidfile != "" {
f, err := os.Create(*fPidfile)
if err != nil {
log.Fatalf("Unable to create pidfile: %s", err)
} }
fmt.Fprintf(f, "%d\n", os.Getpid()) ag.Run(shutdown)
f.Close()
} }
ag.Run(shutdown)
} }
func usageExit() { func usageExit() {