categraf/main.go

169 lines
4.3 KiB
Go
Raw Normal View History

2022-04-13 12:36:01 +08:00
package main
2022-04-13 14:09:33 +08:00
import (
"flag"
2022-04-13 23:35:43 +08:00
"fmt"
2022-04-13 14:09:33 +08:00
"log"
"os"
2022-04-13 14:19:02 +08:00
"os/signal"
2022-05-29 17:03:03 +08:00
"path/filepath"
"runtime"
2022-04-15 12:35:45 +08:00
"strings"
2022-04-13 14:19:02 +08:00
"syscall"
2022-04-13 14:09:33 +08:00
"flashcat.cloud/categraf/agent"
2022-08-03 10:41:44 +08:00
"flashcat.cloud/categraf/api"
2022-04-15 12:35:45 +08:00
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/house"
2022-04-13 14:09:33 +08:00
"flashcat.cloud/categraf/pkg/osx"
2022-04-15 12:35:45 +08:00
"flashcat.cloud/categraf/writer"
2022-05-29 17:03:03 +08:00
"github.com/chai2010/winsvc"
2022-04-13 14:09:33 +08:00
"github.com/toolkits/pkg/runner"
)
var (
2022-05-29 17:03:03 +08:00
appPath string
2022-04-15 12:35:45 +08:00
configDir = flag.String("configs", osx.GetEnv("CATEGRAF_CONFIGS", "conf"), "Specify configuration directory.(env:CATEGRAF_CONFIGS)")
2022-04-18 23:35:47 +08:00
debugMode = flag.Bool("debug", false, "Is debug mode?")
2022-04-15 12:35:45 +08:00
testMode = flag.Bool("test", false, "Is test mode? print metrics to stdout")
2022-07-11 20:40:30 +08:00
interval = flag.Int64("interval", 0, "Global interval(unit:Second)")
2022-04-15 12:35:45 +08:00
showVersion = flag.Bool("version", false, "Show version.")
2022-04-19 19:08:56 +08:00
inputFilters = flag.String("inputs", "", "e.g. cpu:mem:system")
2022-05-29 17:03:03 +08:00
flagWinSvcName = flag.String("win-service-name", "categraf", "Set windows service name")
flagWinSvcDesc = flag.String("win-service-desc", "Categraf", "Set windows service description")
2022-05-29 17:03:03 +08:00
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")
2022-04-13 14:09:33 +08:00
)
2022-04-13 12:36:01 +08:00
2022-05-29 17:03:03 +08:00
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)
}
}
2022-04-13 12:36:01 +08:00
func main() {
2022-04-13 23:35:43 +08:00
flag.Parse()
if *showVersion {
2022-05-29 17:37:57 +08:00
fmt.Println(config.Version)
2022-04-13 23:35:43 +08:00
os.Exit(0)
}
2022-04-13 14:09:33 +08:00
2022-05-29 17:03:03 +08:00
doWinsvc()
2022-04-13 14:09:33 +08:00
printEnv()
2022-04-15 12:35:45 +08:00
// init configs
2022-07-11 20:40:30 +08:00
if err := config.InitConfig(*configDir, *debugMode, *testMode, *interval); err != nil {
2022-05-29 17:03:03 +08:00
log.Fatalln("F! failed to init config:", err)
2022-04-13 14:09:33 +08:00
}
initWriters()
2022-04-13 14:19:02 +08:00
2022-08-03 10:41:44 +08:00
go api.Start()
go agent.Report()
2022-08-03 10:41:44 +08:00
2022-04-15 12:35:45 +08:00
ag := agent.NewAgent(parseFilter(*inputFilters))
2022-05-29 17:03:03 +08:00
runAgent(ag)
2022-04-15 12:35:45 +08:00
}
func initWriters() {
if err := writer.InitWriters(); err != nil {
log.Fatalln("F! failed to init writer:", err)
}
if err := house.InitMetricsHouse(); err != nil {
log.Fatalln("F! failed to init metricshouse:", err)
}
}
2022-04-15 12:35:45 +08:00
func handleSignal(ag *agent.Agent) {
sc := make(chan os.Signal, 1)
2022-05-12 10:56:59 +08:00
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE)
2022-04-15 12:35:45 +08:00
2022-04-13 14:19:02 +08:00
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()
2022-05-12 10:56:59 +08:00
case syscall.SIGPIPE:
// https://pkg.go.dev/os/signal#hdr-SIGPIPE
// do nothing
2022-04-13 14:19:02 +08:00
default:
break EXIT
}
}
ag.Stop()
2022-04-13 14:19:02 +08:00
log.Println("I! exited")
2022-04-13 14:09:33 +08:00
}
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())
2022-04-13 12:36:01 +08:00
}
2022-04-15 12:35:45 +08:00
func parseFilter(filterStr string) map[string]struct{} {
filters := strings.Split(filterStr, ":")
filtermap := make(map[string]struct{})
for i := 0; i < len(filters); i++ {
if strings.TrimSpace(filters[i]) == "" {
continue
}
filtermap[filters[i]] = struct{}{}
}
return filtermap
}
2022-05-29 17:03:03 +08:00
func doWinsvc() {
// install service
if *flagWinSvcInstall && runtime.GOOS == "windows" {
if err := winsvc.InstallService(appPath, *flagWinSvcName, *flagWinSvcDesc); err != nil {
2022-05-29 17:21:06 +08:00
log.Fatalln("F! failed to install service:", *flagWinSvcName, "error:", err)
2022-05-29 17:03:03 +08:00
}
fmt.Println("done")
os.Exit(0)
}
// uninstall service
if *flagWinSvcUninstall && runtime.GOOS == "windows" {
if err := winsvc.RemoveService(*flagWinSvcName); err != nil {
2022-05-29 17:21:06 +08:00
log.Fatalln("F! failed to uninstall service:", *flagWinSvcName, "error:", err)
2022-05-29 17:03:03 +08:00
}
fmt.Println("done")
os.Exit(0)
}
// start service
if *flagWinSvcStart && runtime.GOOS == "windows" {
if err := winsvc.StartService(*flagWinSvcName); err != nil {
2022-05-29 17:21:06 +08:00
log.Fatalln("F! failed to start service:", *flagWinSvcName, "error:", err)
2022-05-29 17:03:03 +08:00
}
fmt.Println("done")
os.Exit(0)
}
// stop service
if *flagWinSvcStop && runtime.GOOS == "windows" {
if err := winsvc.StopService(*flagWinSvcName); err != nil {
2022-05-29 17:21:06 +08:00
log.Fatalln("F! failed to stop service:", *flagWinSvcName, "error:", err)
2022-05-29 17:03:03 +08:00
}
fmt.Println("done")
os.Exit(0)
}
}