Skip to content

Commit 2a72295

Browse files
authored
Detect changes to config and reload telegraf (copy of pr #8529) (#9485)
1 parent 4591c62 commit 2a72295

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

cmd/telegraf/telegraf.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"syscall"
1616
"time"
1717

18+
"github.com/influxdata/tail/watch"
1819
"github.com/influxdata/telegraf"
1920
"github.com/influxdata/telegraf/agent"
2021
"github.com/influxdata/telegraf/config"
@@ -27,6 +28,7 @@ import (
2728
"github.com/influxdata/telegraf/plugins/outputs"
2829
_ "github.com/influxdata/telegraf/plugins/outputs/all"
2930
_ "github.com/influxdata/telegraf/plugins/processors/all"
31+
"gopkg.in/tomb.v1"
3032
)
3133

3234
type sliceFlags []string
@@ -53,7 +55,7 @@ var fTestWait = flag.Int("test-wait", 0, "wait up to this many seconds for servi
5355

5456
var fConfigs sliceFlags
5557
var fConfigDirs sliceFlags
56-
58+
var fWatchConfig = flag.String("watch-config", "", "Monitoring config changes [notify, poll]")
5759
var fVersion = flag.Bool("version", false, "display the version and exit")
5860
var fSampleConfig = flag.Bool("sample-config", false,
5961
"print out full sample configuration")
@@ -115,6 +117,15 @@ func reloadLoop(
115117
signals := make(chan os.Signal, 1)
116118
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
117119
syscall.SIGTERM, syscall.SIGINT)
120+
if *fWatchConfig != "" {
121+
for _, fConfig := range fConfigs {
122+
if _, err := os.Stat(fConfig); err == nil {
123+
go watchLocalConfig(signals, fConfig)
124+
} else {
125+
log.Printf("W! Cannot watch config %s: %s", fConfig, err)
126+
}
127+
}
128+
}
118129
go func() {
119130
select {
120131
case sig := <-signals:
@@ -136,6 +147,46 @@ func reloadLoop(
136147
}
137148
}
138149

150+
func watchLocalConfig(signals chan os.Signal, fConfig string) {
151+
var mytomb tomb.Tomb
152+
var watcher watch.FileWatcher
153+
if *fWatchConfig == "poll" {
154+
watcher = watch.NewPollingFileWatcher(fConfig)
155+
} else {
156+
watcher = watch.NewInotifyFileWatcher(fConfig)
157+
}
158+
changes, err := watcher.ChangeEvents(&mytomb, 0)
159+
if err != nil {
160+
log.Printf("E! Error watching config: %s\n", err)
161+
return
162+
}
163+
log.Println("I! Config watcher started")
164+
select {
165+
case <-changes.Modified:
166+
log.Println("I! Config file modified")
167+
case <-changes.Deleted:
168+
// deleted can mean moved. wait a bit a check existence
169+
<-time.After(time.Second)
170+
if _, err := os.Stat(fConfig); err == nil {
171+
log.Println("I! Config file overwritten")
172+
} else {
173+
log.Println("W! Config file deleted")
174+
if err := watcher.BlockUntilExists(&mytomb); err != nil {
175+
log.Printf("E! Cannot watch for config: %s\n", err.Error())
176+
return
177+
}
178+
log.Println("I! Config file appeared")
179+
}
180+
case <-changes.Truncated:
181+
log.Println("I! Config file truncated")
182+
case <-mytomb.Dying():
183+
log.Println("I! Config watcher ended")
184+
return
185+
}
186+
mytomb.Done()
187+
signals <- syscall.SIGHUP
188+
}
189+
139190
func runAgent(ctx context.Context,
140191
inputFilters []string,
141192
outputFilters []string,

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ require (
8080
github.com/influxdata/influxdb-observability/common v0.0.0-20210429174543-86ae73cafd31
8181
github.com/influxdata/influxdb-observability/otel2influx v0.0.0-20210429174543-86ae73cafd31
8282
github.com/influxdata/influxdb-observability/otlp v0.0.0-20210429174543-86ae73cafd31
83-
github.com/influxdata/tail v1.0.1-0.20200707181643-03a791b270e4
83+
github.com/influxdata/tail v1.0.1-0.20210707231403-b283181d1fa7
8484
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65
8585
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8
8686
github.com/jackc/pgx/v4 v4.6.0
@@ -153,6 +153,7 @@ require (
153153
gopkg.in/ldap.v3 v3.1.0
154154
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
155155
gopkg.in/olivere/elastic.v5 v5.0.70
156+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7
156157
gopkg.in/yaml.v2 v2.4.0
157158
gotest.tools v2.2.0+incompatible
158159
k8s.io/api v0.20.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,8 @@ github.com/influxdata/influxql v1.1.0/go.mod h1:KpVI7okXjK6PRi3Z5B+mtKZli+R1DnZg
886886
github.com/influxdata/line-protocol v0.0.0-20180522152040-32c6aa80de5e/go.mod h1:4kt73NQhadE3daL3WhR5EJ/J2ocX0PZzwxQ0gXJ7oFE=
887887
github.com/influxdata/promql/v2 v2.12.0/go.mod h1:fxOPu+DY0bqCTCECchSRtWfc+0X19ybifQhZoQNF5D8=
888888
github.com/influxdata/roaring v0.4.13-0.20180809181101-fc520f41fab6/go.mod h1:bSgUQ7q5ZLSO+bKBGqJiCBGAl+9DxyW63zLTujjUlOE=
889-
github.com/influxdata/tail v1.0.1-0.20200707181643-03a791b270e4 h1:K3A5vHPs/p8OjI4SL3l1+hs/98mhxTVDcV1Ap0c265E=
890-
github.com/influxdata/tail v1.0.1-0.20200707181643-03a791b270e4/go.mod h1:VeiWgI3qaGdJWust2fP27a6J+koITo/1c/UhxeOxgaM=
889+
github.com/influxdata/tail v1.0.1-0.20210707231403-b283181d1fa7 h1:0rQOs1VHLVFpAAOIR0mJEvVOIaMYFgYdreeVbgI9sII=
890+
github.com/influxdata/tail v1.0.1-0.20210707231403-b283181d1fa7/go.mod h1:VeiWgI3qaGdJWust2fP27a6J+koITo/1c/UhxeOxgaM=
891891
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0=
892892
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65 h1:vvyMtD5LTJc1W9sQKjDkAWdcg0478CszSdzlHtiAXCY=
893893
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65/go.mod h1:zApaNFpP/bTpQItGZNNUMISDMDAnTXu9UqJ4yT3ocz8=

internal/usage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ The commands & flags are:
1616
--aggregator-filter <filter> filter the aggregators to enable, separator is :
1717
--config <file> configuration file to load
1818
--config-directory <directory> directory containing additional *.conf files
19+
--watch-config Telegraf will restart on local config changes. Monitor changes
20+
using either fs notifications or polling. Valid values: 'inotify' or 'poll'.
21+
Monitoring is off by default.
1922
--plugin-directory directory containing *.so files, this directory will be
2023
searched recursively. Any Plugin found will be loaded
2124
and namespaced.

internal/usage_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ The commands & flags are:
1616
--aggregator-filter <filter> filter the aggregators to enable, separator is :
1717
--config <file> configuration file to load
1818
--config-directory <directory> directory containing additional *.conf files
19+
--watch-config Telegraf will restart on local config changes. Monitor changes
20+
using either fs notifications or polling. Valid values: 'inotify' or 'poll'.
21+
Monitoring is off by default.
1922
--debug turn on debug logging
2023
--input-filter <filter> filter the inputs to enable, separator is :
2124
--input-list print available input plugins.

0 commit comments

Comments
 (0)