sql_exporter icon indicating copy to clipboard operation
sql_exporter copied to clipboard

Run as a Windows service

Open MaksimGolovkov opened this issue 6 years ago • 17 comments

It would be great to have ability to run sql_exporter as a Windows service. Now I register service with sc.exe, but it crush with error 7000:

The sql_exporter service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion.

MaksimGolovkov avatar Apr 11 '19 12:04 MaksimGolovkov

Apparently you can't just run a random binary as Windows service, it needs to interact with the OS and take commands from it (pause, resume, stop etc.) while publishing its state.

It doesn't appear complicated to implement, you can see a working example in wmi_exporter (just scroll all the way to the bottom). It uses what appears to be Go standard library support for Windows services and I've been running it as a service on a couple of Windows boxes without issues for over a year now.

What I'm wondering though, is if it's possible to have a binary that can run both as a regular process (e.g. started from the command line) and as a Windows service; or whether you would need 2 separate binaries.

But unfortunately I really don't have the time to figure it out, so as they say, contributions are welcome. (o:

free avatar Apr 12 '19 14:04 free

The easiest and least invasive way to me seems to import https://github.com/kardianos/minwinsvc package, which contains platform build annotations, so it would have an effect just for windows build.

It's the way Gogs/Gitea build their apps for Windows at the moment.

burningalchemist avatar Nov 13 '19 17:11 burningalchemist

Using "golang.org/x/sys/windows/svc" I was able to build a release that now runs as a windows service, but in my world, I have to add sql_exporter.yml & mssql_standard.collector.yml into the System32 folder and the service would run successfully. Thanks, John.

johndnlightnit avatar Jan 28 '20 09:01 johndnlightnit

Hi @johndnlightnit ,

I tried to compile your code but getting errors with reference to original fork. can you help me to compile the code or send me the exe you are using

Thanks, Rajasekhar

ghost avatar Aug 27 '20 07:08 ghost

Hi

Leave it with me and ill have a look

Cheers

John

From: rajacoast [email protected] Sent: 27 August 2020 08:11 To: free/sql_exporter [email protected] Cc: John Denny [email protected]; Mention [email protected] Subject: Re: [free/sql_exporter] Run as a Windows service (#30)

Hi @johndnlightnithttps://github.com/johndnlightnit ,

I tried to compile your code but getting errors with reference to original fork. can you help me to compile the code or send me the exe you are using

Thanks, Rajasekhar

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/free/sql_exporter/issues/30#issuecomment-681661978, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK7DKZEPAFJ6ZB5K2VFJMX3SCYBJLANCNFSM4HFFYPGA.

LEGAL DISCLAIMER: This email and any files transmitted with it are confidential and intended solely for use by the individual or entity to whom they are addressed. If you have received this transmission in error please contact the sender or your system manager. Any views or opinions presented by the sender of this message are not necessarily those of nlightn-IT Ltd. The recipient should check this email and any attachments for the presence of viruses. nlightn-IT Ltd accepts no liability for any damage caused by any virus transmitted by this email. nlightn-IT Ltd, Registered in England No: 04317633 Registered Office: Elmton Lodge Farm, Spring Lane, Elmton, Worksop, S80 4LX

johndnlightnit avatar Aug 27 '20 07:08 johndnlightnit

Hi

I uploaed a build version.

Cheers

John

https://github.com/johndnlightnit/sql_exporter/blob/master/sql_exporter.zip

From: rajacoast [email protected] Sent: 27 August 2020 08:11 To: free/sql_exporter [email protected] Cc: John Denny [email protected]; Mention [email protected] Subject: Re: [free/sql_exporter] Run as a Windows service (#30)

Hi @johndnlightnithttps://github.com/johndnlightnit ,

I tried to compile your code but getting errors with reference to original fork. can you help me to compile the code or send me the exe you are using

Thanks, Rajasekhar

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/free/sql_exporter/issues/30#issuecomment-681661978, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK7DKZEPAFJ6ZB5K2VFJMX3SCYBJLANCNFSM4HFFYPGA.

LEGAL DISCLAIMER: This email and any files transmitted with it are confidential and intended solely for use by the individual or entity to whom they are addressed. If you have received this transmission in error please contact the sender or your system manager. Any views or opinions presented by the sender of this message are not necessarily those of nlightn-IT Ltd. The recipient should check this email and any attachments for the presence of viruses. nlightn-IT Ltd accepts no liability for any damage caused by any virus transmitted by this email. nlightn-IT Ltd, Registered in England No: 04317633 Registered Office: Elmton Lodge Farm, Spring Lane, Elmton, Worksop, S80 4LX

johndnlightnit avatar Aug 27 '20 12:08 johndnlightnit

Thanks John,

But I have some problem with the service. MSSql_Exporter is getting created and started but the http link is working. Also I am not able to stop the service. I am testing this Windows server 2012

Thanks,

ghost avatar Aug 30 '20 08:08 ghost

@rajacoast @johndnlightnit Hi everyone! :) I was able to register and run Sql Exporter as a Windows service with a bit less code and no invasion to /Windows/System32/ (as I described in my comment above). I'll release the update with proper instructions soon here: https://github.com/burningalchemist/sql_exporter. The change is tiny and can be backported to the original project too.

UPD: https://github.com/burningalchemist/sql_exporter/releases/tag/0.7.1 Please take a look at Windows service section in README.md.

burningalchemist avatar Aug 30 '20 09:08 burningalchemist

Good work. As I have previously mentioned, I am not a developer, just cobbled it together to make it work, as there wasn't a version that worked as a service that I could see before, look forward to seeing you update ;0)


From: Sergei Zyubin [email protected] Sent: 30 August 2020 10:33 To: free/sql_exporter [email protected] Cc: John Denny [email protected]; Mention [email protected] Subject: Re: [free/sql_exporter] Run as a Windows service (#30)

@rajacoasthttps://github.com/rajacoast @johndnlightnithttps://github.com/johndnlightnit Hi everyone! :) I was able to register and run Sql Exporter as Windows service with a bit less code and invasion to /Windows/System32/ (as I described in the comment above). I'll release the update with instructions tomorrow here: https://github.com/burningalchemist/sql_exporter. The change is tiny and can be backported to the original project too.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/free/sql_exporter/issues/30#issuecomment-683398275, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK7DKZC5FOAFMR2OBRSL5VTSDIMFNANCNFSM4HFFYPGA.

LEGAL DISCLAIMER: This email and any files transmitted with it are confidential and intended solely for use by the individual or entity to whom they are addressed. If you have received this transmission in error please contact the sender or your system manager. Any views or opinions presented by the sender of this message are not necessarily those of nlightn-IT Ltd. The recipient should check this email and any attachments for the presence of viruses. nlightn-IT Ltd accepts no liability for any damage caused by any virus transmitted by this email. nlightn-IT Ltd, Registered in England No: 04317633 Registered Office: Elmton Lodge Farm, Spring Lane, Elmton, Worksop, S80 4LX

johndnlightnit avatar Aug 30 '20 19:08 johndnlightnit

@johndnlightnit Thank you for looking into this, it's valuable! 🙂👍 The update is released.

burningalchemist avatar Aug 30 '20 20:08 burningalchemist

No worries and brill will have a look as I sometimes had issues with service getting stuck on reboot.


From: Sergei Zyubin [email protected] Sent: 30 August 2020 21:05 To: free/sql_exporter [email protected] Cc: John Denny [email protected]; Mention [email protected] Subject: Re: [free/sql_exporter] Run as a Windows service (#30)

@johndnlightnithttps://github.com/johndnlightnit Thank you for looking into this, it's valuable! 🙂👍 The update is releasedhttps://github.com/burningalchemist/sql_exporter/releases/tag/0.7.1.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/free/sql_exporter/issues/30#issuecomment-683464268, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK7DKZBQPW7MBL2VKVLEICTSDKWKFANCNFSM4HFFYPGA.

LEGAL DISCLAIMER: This email and any files transmitted with it are confidential and intended solely for use by the individual or entity to whom they are addressed. If you have received this transmission in error please contact the sender or your system manager. Any views or opinions presented by the sender of this message are not necessarily those of nlightn-IT Ltd. The recipient should check this email and any attachments for the presence of viruses. nlightn-IT Ltd accepts no liability for any damage caused by any virus transmitted by this email. nlightn-IT Ltd, Registered in England No: 04317633 Registered Office: Elmton Lodge Farm, Spring Lane, Elmton, Worksop, S80 4LX

johndnlightnit avatar Aug 31 '20 11:08 johndnlightnit

@johndnlightnit Cool, let me know! I haven't tested it extensively, but will be happy to look into edge cases 👍

burningalchemist avatar Aug 31 '20 11:08 burningalchemist

Thanks @burningalchemist & @johndnlightnit. The exporter is working for me. I have to test the server reboot senario

ghost avatar Aug 31 '20 17:08 ghost

@burningalchemist - I built your new release, ran as a service, but I am still unable to start the service unless the yaml files are in the /Windows/System32/ Folder, I'm not sure what I have missed. cheers J. It does now stop and start a treat though cheers.

johndnlightnit avatar Sep 01 '20 13:09 johndnlightnit

Hi @johndnlightnit ! Could you provide the way you register the service? :) I think I can help with this.

burningalchemist avatar Sep 01 '20 13:09 burningalchemist

All good, just needed to reference the sqlexporter config from the service startup, sorry was being a bit slow. All good now many thanks.

johndnlightnit avatar Sep 01 '20 13:09 johndnlightnit

Yeah, great! 👍

burningalchemist avatar Sep 01 '20 14:09 burningalchemist