-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain_windows.go
158 lines (135 loc) · 4.15 KB
/
main_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package main
import (
"context"
"fmt"
_ "net/http/pprof"
"os"
"path/filepath"
"time"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
"github.com/DataDog/datadog-agent/pkg/util/log"
_ "github.com/DataDog/datadog-agent/pkg/util/containers/providers/windows"
"github.com/DataDog/datadog-agent/pkg/util/winutil"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
)
var (
elog debug.Log
defaultLogFile = "c:\\programdata\\datadog\\logs\\dogstatsd.log"
// DefaultConfPath points to the folder containing datadog.yaml
DefaultConfPath = "c:\\programdata\\datadog"
enabledVals = map[string]bool{"yes": true, "true": true, "1": true,
"no": false, "false": false, "0": false}
subServices = map[string]string{"logs_enabled": "logs_enabled",
"apm_enabled": "apm_config.enabled",
"process_enabled": "process_config.enabled"}
)
func init() {
pd, err := winutil.GetProgramDataDirForProduct("Datadog Dogstatsd")
if err == nil {
DefaultConfPath = pd
defaultLogFile = filepath.Join(pd, "logs", "dogstatsd.log")
} else {
winutil.LogEventViewer(ServiceName, 0x8000000F, defaultLogFile)
}
}
// ServiceName is the name of the service in service control manager
const ServiceName = "dogstatsd"
// EnableLoggingToFile -- set up logging to file
func main() {
// set the Agent flavor
flavor.SetFlavor(flavor.Dogstatsd)
config.Datadog.AddConfigPath(DefaultConfPath)
isIntSess, err := svc.IsAnInteractiveSession()
if err != nil {
fmt.Printf("failed to determine if we are running in an interactive session: %v\n", err)
}
if !isIntSess {
confPath = DefaultConfPath
runService(false)
return
}
defer log.Flush()
if err = dogstatsdCmd.Execute(); err != nil {
log.Error(err)
os.Exit(-1)
}
}
type myservice struct{}
func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
log.Infof("Service control function")
ctx, cancel := context.WithCancel(context.Background())
err := runAgent(ctx)
if err != nil {
log.Errorf("Failed to start agent %v", err)
elog.Error(0xc0000008, err.Error())
errno = 1 // indicates non-successful return from handler.
stopAgent(cancel)
changes <- svc.Status{State: svc.Stopped}
return
}
elog.Info(0x40000003, ServiceName)
loop:
for {
select {
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
// Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
time.Sleep(100 * time.Millisecond)
changes <- c.CurrentStatus
case svc.Stop:
log.Info("Received stop message from service control manager")
elog.Info(0x4000000b, ServiceName)
break loop
case svc.PreShutdown:
log.Info("Received pre-shutdown message from service control manager")
elog.Info(0x4000000d, config.ServiceName)
break loop
case svc.Shutdown:
log.Info("Received shutdown message from service control manager")
elog.Info(0x4000000c, ServiceName)
break loop
default:
log.Warnf("unexpected control request #%d", c)
elog.Warning(0xc0000005, fmt.Sprint(c.Cmd))
}
}
}
elog.Info(0x40000006, ServiceName)
log.Infof("Initiating service shutdown")
changes <- svc.Status{State: svc.StopPending}
stopAgent(cancel)
changes <- svc.Status{State: svc.Stopped}
return
}
func runService(isDebug bool) {
var err error
if isDebug {
elog = debug.New(ServiceName)
} else {
elog, err = eventlog.Open(ServiceName)
if err != nil {
return
}
}
defer elog.Close()
elog.Info(0x40000007, ServiceName)
run := svc.Run
err = run(ServiceName, &myservice{})
if err != nil {
elog.Error(0xc0000008, err.Error())
return
}
elog.Info(0x40000004, ServiceName)
}