Skip to content

Commit

Permalink
feat: Add autorestart and restartdelay flags to Windows service (infl…
Browse files Browse the repository at this point in the history
  • Loading branch information
R290 authored Feb 3, 2022
1 parent 4ae8c60 commit 48682ff
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ var fServiceName = flag.String("service-name", "telegraf",
var fServiceDisplayName = flag.String("service-display-name", "Telegraf Data Collector Service",
"service display name (windows only)")

//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows
var fServiceAutoRestart = flag.Bool("service-auto-restart", false,
"auto restart service on failure (windows only)")

//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows
var fServiceRestartDelay = flag.String("service-restart-delay", "5m",
"delay before service auto restart, default is 5m (windows only)")

//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows
var fRunAsConsole = flag.Bool("console", false,
"run as console application (windows only)")
Expand Down
4 changes: 4 additions & 0 deletions cmd/telegraf/telegraf_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func runAsWindowsService(inputFilters, outputFilters []string) {
//set servicename to service cmd line, to have a custom name after relaunch as a service
svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName)

if *fServiceAutoRestart {
svcConfig.Option = service.KeyValue{"OnFailure": "restart", "OnFailureDelayDuration": *fServiceRestartDelay}
}

err := service.Control(s, *fService)
if err != nil {
log.Fatal("E! " + err.Error())
Expand Down
4 changes: 4 additions & 0 deletions docs/WINDOWS_SERVICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ on a single system, you can install the service with the `--service-name` and
> C:\"Program Files"\Telegraf\telegraf.exe --service install --service-name telegraf-2 --service-display-name "Telegraf 2"
```

## Auto restart and restart delay

By default the service will not automatically restart on failure. Providing the `--service-auto-restart` flag during installation will always restart the service with a default delay of 5 minutes. To modify this to for example 3 minutes, provide the additional flag `--service-restart-delay 3m`. The delay can be any valid `time.Duration` string.

## Troubleshooting

When Telegraf runs as a Windows service, Telegraf logs messages to Windows events log before configuration file with logging settings is loaded.
Expand Down
7 changes: 6 additions & 1 deletion internal/usage_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ The commands & flags are:
--service <service> operate on the service (windows only)
--service-name service name (windows only)
--service-display-name service display name (windows only)
--service-auto-restart auto restart service on failure (windows only)
--service-restart-delay delay before service auto restart, default is 5m (windows only)
Examples:
Expand Down Expand Up @@ -73,4 +75,7 @@ Examples:
# install telegraf service with custom name
telegraf --service install --service-name=my-telegraf --service-display-name="My Telegraf"
`
# install telegraf service with auto restart and restart delay of 3 minutes
telegraf --service install --service-auto-restart --service-restart-delay 3m
`

0 comments on commit 48682ff

Please sign in to comment.