categraf/main.go

96 lines
2.2 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-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-04-15 12:35:45 +08:00
"flashcat.cloud/categraf/config"
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-04-13 14:09:33 +08:00
"github.com/toolkits/pkg/runner"
)
var (
2022-04-15 12:35:45 +08:00
version = "0.0.1"
configDir = flag.String("configs", osx.GetEnv("CATEGRAF_CONFIGS", "conf"), "Specify configuration directory.(env:CATEGRAF_CONFIGS)")
debugMode = flag.String("debug", osx.GetEnv("CATEGRAF_DEBUG", "false"), "Is debug mode?.(env:CATEGRAF_DEBUG)")
testMode = flag.Bool("test", false, "Is test mode? print metrics to stdout")
showVersion = flag.Bool("version", false, "Show version.")
inputFilters = flag.String("input-filter", "", "e.g. cpu:mem:system")
2022-04-13 14:09:33 +08:00
)
2022-04-13 12:36:01 +08:00
func main() {
2022-04-13 23:35:43 +08:00
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
2022-04-13 14:09:33 +08:00
printEnv()
2022-04-15 12:35:45 +08:00
// init configs
if err := config.InitConfig(*configDir, *debugMode, *testMode); err != nil {
log.Println("F! failed to init config:", err)
2022-04-13 14:09:33 +08:00
os.Exit(1)
}
2022-04-15 12:35:45 +08:00
// init writers
if err := writer.Init(config.Config.Writers); err != nil {
log.Println("F! failed to init writer:", err)
os.Exit(1)
}
2022-04-13 14:19:02 +08:00
2022-04-15 12:35:45 +08:00
ag := agent.NewAgent(parseFilter(*inputFilters))
2022-04-13 14:19:02 +08:00
ag.Start()
2022-04-15 12:35:45 +08:00
handleSignal(ag)
}
func handleSignal(ag *agent.Agent) {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
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()
default:
break EXIT
}
}
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
}