-
Notifications
You must be signed in to change notification settings - Fork 262
/
main_windows.go
106 lines (93 loc) · 2.63 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
//go:build windows
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"sync/atomic"
"time"
"github.com/chai2010/winsvc"
"flashcat.cloud/categraf/agent"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/pkg/pprof"
)
var (
pprofStart uint32
flagWinSvcName = flag.String("win-service-name", "categraf", "Set windows service name")
flagWinSvcDesc = flag.String("win-service-desc", "Categraf", "Set windows service description")
flagWinSvcInstall = flag.Bool("win-service-install", false, "Install windows service")
flagWinSvcUninstall = flag.Bool("win-service-uninstall", false, "Uninstall windows service")
flagWinSvcStart = flag.Bool("win-service-start", false, "Start windows service")
flagWinSvcStop = flag.Bool("win-service-stop", false, "Stop windows service")
)
func runAgent(ag *agent.Agent) {
if !winsvc.IsAnInteractiveSession() {
if config.Config.Log.FileName == "stdout" || config.Config.Log.FileName == "stderr" ||
config.Config.Log.FileName == "" {
initLog("categraf.log")
} else {
initLog(config.Config.Log.FileName)
}
if err := winsvc.RunAsService(*flagWinSvcName, ag.Start, ag.Stop, false); err != nil {
log.Fatalln("F! failed to run windows service:", err)
}
return
}
ag.Start()
go profile()
handleSignal(ag)
}
func doOSsvc() {
// install service
if *flagWinSvcInstall {
if err := winsvc.InstallService(appPath, *flagWinSvcName, *flagWinSvcDesc); err != nil {
log.Fatalln("F! failed to install service:", *flagWinSvcName, "error:", err)
}
fmt.Println("done")
os.Exit(0)
}
// uninstall service
if *flagWinSvcUninstall {
if err := winsvc.RemoveService(*flagWinSvcName); err != nil {
log.Fatalln("F! failed to uninstall service:", *flagWinSvcName, "error:", err)
}
fmt.Println("done")
os.Exit(0)
}
// start service
if *flagWinSvcStart {
if err := winsvc.StartService(*flagWinSvcName); err != nil {
log.Fatalln("F! failed to start service:", *flagWinSvcName, "error:", err)
}
fmt.Println("done")
os.Exit(0)
}
// stop service
if *flagWinSvcStop && runtime.GOOS == "windows" {
if err := winsvc.StopService(*flagWinSvcName); err != nil {
log.Fatalln("F! failed to stop service:", *flagWinSvcName, "error:", err)
}
fmt.Println("done")
os.Exit(0)
}
}
func profile() {
// TODO: replace with windows event
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
file := filepath.Join(config.Config.ConfigDir, ".pprof")
if _, err := os.Stat(file); err == nil {
if !atomic.CompareAndSwapUint32(&pprofStart, 0, 1) {
return
}
go pprof.Go()
}
}
}
}