-
Notifications
You must be signed in to change notification settings - Fork 261
/
main.go
310 lines (282 loc) · 7.94 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"flag"
"fmt"
"log"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/chai2010/winsvc"
"github.com/kardianos/service"
"github.com/toolkits/pkg/net/tcpx"
"github.com/toolkits/pkg/runner"
"gopkg.in/natefinch/lumberjack.v2"
"flashcat.cloud/categraf/agent"
agentInstall "flashcat.cloud/categraf/agent/install"
agentUpdate "flashcat.cloud/categraf/agent/update"
"flashcat.cloud/categraf/api"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/heartbeat"
"flashcat.cloud/categraf/pkg/osx"
"flashcat.cloud/categraf/writer"
)
var (
appPath string
configDir = flag.String("configs", osx.GetEnv("CATEGRAF_CONFIGS", "conf"), "Specify configuration directory.(env:CATEGRAF_CONFIGS)")
debugMode = flag.Bool("debug", false, "Is debug mode?")
debugLevel = flag.Int("debug-level", 0, "debug level")
testMode = flag.Bool("test", false, "Is test mode? print metrics to stdout")
interval = flag.Int64("interval", 0, "Global interval(unit:Second)")
showVersion = flag.Bool("version", false, "Show version.")
inputFilters = flag.String("inputs", "", "e.g. cpu:mem:system")
install = flag.Bool("install", false, "Install categraf service")
remove = flag.Bool("remove", false, "Remove categraf service")
start = flag.Bool("start", false, "Start categraf service")
stop = flag.Bool("stop", false, "Stop categraf service")
status = flag.Bool("status", false, "Show categraf service status")
update = flag.Bool("update", false, "Update categraf binary")
updateFile = flag.String("update_url", "", "new version for categraf to download")
)
func init() {
// change to current dir
var err error
if appPath, err = winsvc.GetAppPath(); err != nil {
log.Fatal(err)
}
if err := os.Chdir(filepath.Dir(appPath)); err != nil {
log.Fatal(err)
}
}
func initLog(output string) {
switch {
case output == "stdout":
log.SetOutput(os.Stdout)
case output == "stderr":
log.SetOutput(os.Stderr)
case len(output) != 0:
log.SetOutput(&lumberjack.Logger{
Filename: output,
MaxSize: config.Config.Log.MaxSize,
MaxAge: config.Config.Log.MaxAge,
MaxBackups: config.Config.Log.MaxBackups,
LocalTime: config.Config.Log.LocalTime,
Compress: config.Config.Log.Compress,
})
default:
log.SetOutput(os.Stdout)
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Println(config.Version)
os.Exit(0)
}
if *install || *remove || *start || *stop || *status || *update {
err := serviceProcess()
if err != nil {
log.Println("E!", err)
}
return
}
// init configs
if err := config.InitConfig(*configDir, *debugLevel, *debugMode, *testMode, *interval, *inputFilters); err != nil {
log.Fatalln("F! failed to init config:", err)
}
doOSsvc()
printEnv()
initWriters()
go api.Start()
go heartbeat.Work()
tcpx.WaitHosts()
ag, err := agent.NewAgent()
if err != nil {
fmt.Println("F! failed to init agent:", err)
os.Exit(-1)
}
runAgent(ag)
}
func initWriters() {
if err := writer.InitWriters(); err != nil {
log.Fatalln("F! failed to init writer:", err)
}
}
func handleSignal(ag *agent.Agent) {
sc := make(chan os.Signal, 1)
// syscall.SIGUSR2 == 0xc , not available on windows
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE)
EXIT:
for {
sig := <-sc
log.Println("I! received signal:", sig.String())
switch sig {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
break EXIT
case syscall.SIGHUP:
ag.Reload()
case syscall.SIGPIPE:
// https://pkg.go.dev/os/signal#hdr-SIGPIPE
// do nothing
}
}
ag.Stop()
log.Println("I! exited")
}
func printEnv() {
runner.Init()
log.Println("I! runner.binarydir:", runner.Cwd)
log.Println("I! runner.hostname:", runner.Hostname)
log.Println("I! runner.fd_limits:", runner.FdLimits())
log.Println("I! runner.vm_limits:", runner.VMLimits())
}
type program struct{}
func (p *program) Start(s service.Service) error {
return nil
}
func (p *program) Stop(s service.Service) error {
return nil
}
func serviceProcess() error {
svcConfig := agentInstall.ServiceConfig()
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
fmt.Println("generate categraf service error " + err.Error())
return nil
}
if *stop {
if sts, err := s.Status(); err != nil {
log.Println("W! show categraf service status failed:", err)
} else {
switch sts {
case service.StatusRunning:
log.Println("I! categraf service status: running")
case service.StatusStopped:
log.Println("I! categraf service status: stopped")
default:
log.Println("I! categraf service status: unknown")
}
}
if err := s.Stop(); err != nil {
log.Println("E! stop categraf service failed:", err)
} else {
log.Println("I! stop categraf service ok")
}
return nil
}
if *remove {
if sts, err := s.Status(); err != nil {
log.Println("W! show categraf service status failed:", err)
} else {
switch sts {
case service.StatusRunning:
log.Println("I! categraf service status: running")
case service.StatusStopped:
log.Println("I! categraf service status: stopped")
default:
log.Println("I! categraf service status: unknown")
}
}
if err := s.Stop(); err != nil {
log.Println("W! stop categraf service failed:", err)
} else {
log.Println("I! stop categraf service ok")
}
if err := s.Uninstall(); err != nil {
log.Println("E! remove categraf service failed:", err)
} else {
log.Println("I! remove categraf service ok")
}
return nil
}
if *install {
if sts, err := s.Status(); err != nil {
log.Println("W! show categraf service status failed:", err)
} else {
switch sts {
case service.StatusRunning:
log.Println("I! categraf service status: running")
case service.StatusStopped:
log.Println("I! categraf service status: stopped")
default:
log.Println("I! categraf service status: unknown")
}
}
if err := s.Install(); err != nil {
log.Println("E! install categraf service failed:", err)
} else {
log.Println("I! install categraf service ok")
}
return nil
}
if *start {
if sts, err := s.Status(); err != nil {
log.Println("W! show categraf service status failed:", err)
} else {
switch sts {
case service.StatusRunning:
log.Println("I! categraf service status: running")
case service.StatusStopped:
log.Println("I! categraf service status: stopped")
default:
log.Println("I! categraf service status: unknown")
}
}
if err := s.Start(); err != nil {
log.Println("E! start categraf service failed:", err)
} else {
log.Println("I! start categraf service ok")
}
return nil
}
if *status {
if sts, err := s.Status(); err != nil {
log.Println("E! show categraf service status failed:", err)
} else {
switch sts {
case service.StatusRunning:
log.Println("I! show categraf service status: running")
case service.StatusStopped:
log.Println("I! show categraf service status: stopped")
default:
log.Println("I! show categraf service status: unknown")
}
}
return nil
}
if *update {
if *updateFile == "" {
return fmt.Errorf("please input update_url")
}
if sts, err := s.Status(); err != nil {
if strings.Contains(err.Error(), "not installed") {
log.Println("E! update only support mode that running in service mode")
}
return nil
} else {
switch sts {
case service.StatusRunning:
log.Println("I! categraf service status: running, version:", config.Version)
case service.StatusStopped:
log.Println("I! categraf service status: stopped, version:", config.Version)
default:
log.Println("I! categraf service status: unknown, version:", config.Version)
}
}
err := agentUpdate.Update(*updateFile)
if err != nil {
log.Println("E! update categraf failed:", err)
return nil
}
err = s.Restart()
if err != nil {
log.Println("E! restart categraf failed:", err)
return nil
}
log.Println("I! update categraf success")
}
return nil
}