try to support windows
This commit is contained in:
parent
0c770fabbd
commit
2bd08b84ea
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module flashcat.cloud/categraf
|
|||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/chai2010/winsvc v0.0.0-20200705094454-db7ec320025c
|
||||
github.com/gaochao1/sw v1.0.0
|
||||
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
|
|
2
go.sum
2
go.sum
|
@ -50,6 +50,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
|
|||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/chai2010/winsvc v0.0.0-20200705094454-db7ec320025c h1:ZgxF2fGttmsetibm9Tc91TAUWzRZSfjJPstNxU6jWyU=
|
||||
github.com/chai2010/winsvc v0.0.0-20200705094454-db7ec320025c/go.mod h1:b9Xy0A0C/binZARjeVfHEr+gHzQUVztL71bTms7PRIM=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
|
|
71
main.go
71
main.go
|
@ -6,6 +6,8 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
|
@ -13,18 +15,38 @@ import (
|
|||
"flashcat.cloud/categraf/config"
|
||||
"flashcat.cloud/categraf/pkg/osx"
|
||||
"flashcat.cloud/categraf/writer"
|
||||
"github.com/chai2010/winsvc"
|
||||
"github.com/toolkits/pkg/runner"
|
||||
)
|
||||
|
||||
var (
|
||||
appPath string
|
||||
version = "0.1.0"
|
||||
configDir = flag.String("configs", osx.GetEnv("CATEGRAF_CONFIGS", "conf"), "Specify configuration directory.(env:CATEGRAF_CONFIGS)")
|
||||
debugMode = flag.Bool("debug", false, "Is debug mode?")
|
||||
testMode = flag.Bool("test", false, "Is test mode? print metrics to stdout")
|
||||
showVersion = flag.Bool("version", false, "Show version.")
|
||||
inputFilters = flag.String("inputs", "", "e.g. cpu:mem:system")
|
||||
|
||||
flagWinSvcName = flag.String("win-service-name", "categraf", "Set windows service name")
|
||||
flagWinSvcDesc = flag.String("win-service-desc", "Categraf is a monitoring agent", "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 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 main() {
|
||||
flag.Parse()
|
||||
|
||||
|
@ -33,24 +55,21 @@ func main() {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
doWinsvc()
|
||||
printEnv()
|
||||
|
||||
// init configs
|
||||
if err := config.InitConfig(*configDir, *debugMode, *testMode); err != nil {
|
||||
log.Println("F! failed to init config:", err)
|
||||
os.Exit(1)
|
||||
log.Fatalln("F! failed to init config:", err)
|
||||
}
|
||||
|
||||
// init writers
|
||||
if err := writer.Init(config.Config.Writers); err != nil {
|
||||
log.Println("F! failed to init writer:", err)
|
||||
os.Exit(1)
|
||||
log.Fatalln("F! failed to init writer:", err)
|
||||
}
|
||||
|
||||
ag := agent.NewAgent(parseFilter(*inputFilters))
|
||||
ag.Start()
|
||||
|
||||
handleSignal(ag)
|
||||
runAgent(ag)
|
||||
}
|
||||
|
||||
func handleSignal(ag *agent.Agent) {
|
||||
|
@ -96,3 +115,41 @@ func parseFilter(filterStr string) map[string]struct{} {
|
|||
}
|
||||
return filtermap
|
||||
}
|
||||
|
||||
func doWinsvc() {
|
||||
// install service
|
||||
if *flagWinSvcInstall && runtime.GOOS == "windows" {
|
||||
if err := winsvc.InstallService(appPath, *flagWinSvcName, *flagWinSvcDesc); err != nil {
|
||||
log.Fatalln("failed to install service:", *flagWinSvcName, "error:", err)
|
||||
}
|
||||
fmt.Println("done")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// uninstall service
|
||||
if *flagWinSvcUninstall && runtime.GOOS == "windows" {
|
||||
if err := winsvc.RemoveService(*flagWinSvcName); err != nil {
|
||||
log.Fatalln("failed to uninstall service:", *flagWinSvcName, "error:", err)
|
||||
}
|
||||
fmt.Println("done")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// start service
|
||||
if *flagWinSvcStart && runtime.GOOS == "windows" {
|
||||
if err := winsvc.StartService(*flagWinSvcName); err != nil {
|
||||
log.Fatalln("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("failed to stop service:", *flagWinSvcName, "error:", err)
|
||||
}
|
||||
fmt.Println("done")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flashcat.cloud/categraf/agent"
|
||||
)
|
||||
|
||||
func runAgent(ag *agent.Agent) {
|
||||
ag.Start()
|
||||
handleSignal(ag)
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flashcat.cloud/categraf/agent"
|
||||
"github.com/chai2010/winsvc"
|
||||
)
|
||||
|
||||
func runAgent(ag *agent.Agent) {
|
||||
if !winsvc.IsAnInteractiveSession() {
|
||||
if err := winsvc.RunAsService(*flagWinSvcName, ag.Start, ag.Stop, false); err != nil {
|
||||
log.Fatalln("F! failed to run windows service:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ag.Start()
|
||||
handleSignal(ag)
|
||||
}
|
Loading…
Reference in New Issue