2023-11-04 19:51:35 +00:00
|
|
|
// Package initiate allows us to initiate Time Sensitive components (Like registering the windows service) as early as possible in the startup process
|
2022-08-23 13:57:16 +00:00
|
|
|
package initiate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-22 10:17:51 +00:00
|
|
|
"os"
|
2022-08-23 13:57:16 +00:00
|
|
|
|
2024-09-28 17:53:12 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2022-08-23 13:57:16 +00:00
|
|
|
"golang.org/x/sys/windows/svc"
|
2023-04-22 10:17:51 +00:00
|
|
|
"golang.org/x/sys/windows/svc/eventlog"
|
2022-08-23 13:57:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
serviceName = "windows_exporter"
|
|
|
|
)
|
|
|
|
|
2024-01-07 12:24:51 +00:00
|
|
|
type windowsExporterService struct{}
|
2022-08-23 13:57:16 +00:00
|
|
|
|
2024-09-28 17:53:12 +00:00
|
|
|
func (s *windowsExporterService) Execute(_ []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
|
2022-08-23 13:57:16 +00:00
|
|
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
|
|
|
changes <- svc.Status{State: svc.StartPending}
|
|
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
2024-08-10 20:05:33 +00:00
|
|
|
|
2024-09-28 17:53:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case exitCodeCh := <-ExitCodeCh:
|
2024-08-10 20:05:33 +00:00
|
|
|
changes <- svc.Status{State: svc.StopPending}
|
|
|
|
|
2024-09-28 17:53:12 +00:00
|
|
|
return true, uint32(exitCodeCh)
|
|
|
|
case c := <-r:
|
|
|
|
switch c.Cmd {
|
|
|
|
case svc.Interrogate:
|
|
|
|
changes <- c.CurrentStatus
|
|
|
|
case svc.Stop, svc.Shutdown:
|
|
|
|
_ = logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "service stop received")
|
|
|
|
|
|
|
|
changes <- svc.Status{State: svc.StopPending}
|
|
|
|
|
|
|
|
return false, 0
|
|
|
|
default:
|
|
|
|
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("unexpected control request #%d", c))
|
|
|
|
}
|
2022-08-23 13:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-28 17:53:12 +00:00
|
|
|
var (
|
|
|
|
IsService bool
|
|
|
|
ExitCodeCh = make(chan int)
|
|
|
|
StopCh = make(chan struct{})
|
|
|
|
)
|
2022-08-23 13:57:16 +00:00
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
//nolint:gochecknoinits
|
2022-08-23 13:57:16 +00:00
|
|
|
func init() {
|
2024-09-28 17:53:12 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
IsService, err = svc.IsWindowsService()
|
2022-08-23 13:57:16 +00:00
|
|
|
if err != nil {
|
2024-09-28 17:53:12 +00:00
|
|
|
err = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("Failed to detect service: %v", err))
|
2023-04-22 10:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
os.Exit(1)
|
2022-08-23 13:57:16 +00:00
|
|
|
}
|
2023-04-22 10:17:51 +00:00
|
|
|
|
2024-09-28 17:53:12 +00:00
|
|
|
if IsService {
|
|
|
|
err = logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "Attempting to start exporter service")
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2022-08-23 13:57:16 +00:00
|
|
|
go func() {
|
2024-01-07 12:24:51 +00:00
|
|
|
err = svc.Run(serviceName, &windowsExporterService{})
|
2022-08-23 13:57:16 +00:00
|
|
|
if err != nil {
|
2024-09-28 17:53:12 +00:00
|
|
|
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("Failed to start service: %v", err))
|
2022-08-23 13:57:16 +00:00
|
|
|
}
|
2024-09-28 17:53:12 +00:00
|
|
|
|
|
|
|
StopCh <- struct{}{}
|
2022-08-23 13:57:16 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 17:53:12 +00:00
|
|
|
|
|
|
|
func logToEventToLog(eType uint16, msg string) error {
|
|
|
|
eventLog, err := eventlog.Open("windows_exporter")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open event log: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := windows.UTF16PtrFromString(msg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error convert string to UTF-16: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := []*uint16{p, nil, nil, nil, nil, nil, nil, nil, nil}
|
|
|
|
|
|
|
|
return windows.ReportEvent(eventLog.Handle, eType, 0, 3299, 0, 9, 0, &ss[0], nil)
|
|
|
|
}
|