Telegraf as Windows Service
Hi,
Is it possible to use the Telegraf for windows as Windows Service?
I tried to add it as service but did not work.
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe "C:\Program File s\Telegraf\telegraf.exe" Microsoft (R) .NET Framework Installation utility Version 4.0.30319.33440 Copyright (C) Microsoft Corporation. All rights reserved.
Exception occurred while initializing the installation: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Prog ram Files\Telegraf\telegraf.exe' or one of its dependencies. The module was expe cted to contain an assembly manifest..
Tks Fabrizio
agreed, I believe @cwegener has been doing some work on this and might have a PR available soon
Great! I will wait for that.
Tks
Hi
We are running telegraf in Windows boxes using NSSM. I even wrote a blog post about it here: https://www.ricardclau.com/2016/03/running-go-executables-consul-telegraf-as-windows-services/ (the principles apply equally to things like Consul in the Agent mode and effectively any Go binary)
So, essentially it is all about:
- Install chocolatey
- Install NSSM via chocolatey
- And install the service via NSSM
Hope this helps, happy to contribute to the official docs if the maintainers of the project want to!
Thanks for creating telegraf!
cool, thanks @ricardclau, I've seen NSSM before and it seems like a much better way to get a service running in Windows. What command generates the service, would it just be something like this?:
nssm install Telegraf C:\telegraf\telegraf.exe agent -config C:\telegraf\telegraf.conf
also, if you could write up a readme and submit it that would be great, you could put it in docs/WINDOWS_SERVICE.md
@sparrc the command would be
nssm install Telegraf C:\telegraf\telegraf.exe -config C:\telegraf\telegraf.conf
The agent bit is for the Consul deamon I am afraid :) (https://www.consul.io/intro/getting-started/install.html)
Will do a PR with some md this weekend, thanks!
OK, if you put that into the blog post too I can probably get our marketing folks to tweet/promote it ;-)
@sparrc Just amended my blog post with specific examples for Telegraf and mentioning the TICK stack. Hopefully good enough for your marketing folks! :)
Will work a bit now on the docs for Telegraf.
Thanks!
I have used nssm in the past with good success, for example to run the whole ELK stack. There is no need for chocolatey, can just be downloaded to the telegraf directory and run from there.
Yeah, the chocolatey bit is just to make it easier to automate in an idempotent way. Similar to using apt, yum and friends. It is nice package system for Windows but of course you can always download nssm from http://nssm.cc/ and install it yourself
I would really want to run telegraf as a Windows service as well. But since I have little or no control over the environment which telegraf will run I would STRONGLY prefer not to have to deal with nssm, which seems to be non-standard in the way that it registers itself and acts as an intermediary and relies a lot on GUI stuff which is hard to automate and use properly. So all in all NSSM might be nice but it doesn't smell that good from a reliability and compatibility point of view.
Bosun's scollector has a simple -winsvc switch that can install and remove itself as a Windows service without any dependencies ( https://godoc.org/bosun.org/cmd/scollector ).
Just found the other issue where @cwegener said he is working on getting native -service-install in the binary. Any updates? :)
Anyone just tried to use sc.exe to register the service?
@elvarb Oh yes, for an hour or two. Seems telegraf.exe refuses to start when called as a service. I also tried a trick I saw on SO about calling it like "cmd /c telegraf.exe" which in theory should launch it in some kind of command session but to no avail.
Eventually just gave up and now running it manually in the foreground, hoping someone will fix it soon :)
nssm is the only reliable way we have found to run Go binaries as Windows services as it acts as an intermediary between the Windows API and the Go signaling so that you can restart the services and so on
Plus, it seems to be getting popular and even tools like Ansible have modules to interact with it
Of course, having a native installer would be better but at the moment it seems to be the best (or least bad) way, even if it is not a Windows standard
My 2c: scollector (Go) has been running without problems here for weeks as a Windows service. Ofc, there are checks to detect if it stops sending data to detect problems, but that has not happened yet.
Interesting! We had a look at Consul and Telegraf and they did not have that option, this is why we went NSSM. I agree it is far from ideal.
https://github.com/bosun-monitor/bosun/blob/master/cmd/scollector/service_windows.go seems to be where the magic happens. It would be great to be able to remove NSSM!
FWIW there is a guide for setting up the exe as a service using NSSM here: https://github.com/influxdata/telegraf/blob/master/docs/WINDOWS_SERVICE.md
not sure if this is helpfull.. https://godoc.org/golang.org/x/sys/windows/svc/mgr
Yeah this is one of the packages being used in bosun :)