From 995771164311d0682fb51cf1733a5bb5974e61c8 Mon Sep 17 00:00:00 2001 From: ning <710leo@gmail.com> Date: Thu, 1 Jun 2023 20:01:29 +0800 Subject: [PATCH] add n9e-edge --- alert/aconf/conf.go | 5 + cmd/alert/main.go | 2 +- cmd/edge/edge.go | 69 ++++++++++++ cmd/edge/main.go | 68 +++++++++++ cmd/pushgw/main.go | 2 +- etc/alert.toml.example | 60 ---------- ...{pushgw.toml.example => edge.toml.example} | 106 ++++++++++-------- fe.sh | 2 +- 8 files changed, 206 insertions(+), 108 deletions(-) create mode 100644 cmd/edge/edge.go create mode 100644 cmd/edge/main.go delete mode 100644 etc/alert.toml.example rename etc/{pushgw.toml.example => edge.toml.example} (85%) diff --git a/alert/aconf/conf.go b/alert/aconf/conf.go index 59994628..37c9d5ea 100644 --- a/alert/aconf/conf.go +++ b/alert/aconf/conf.go @@ -7,6 +7,7 @@ import ( ) type Alert struct { + Disable bool EngineDelay int64 Heartbeat HeartbeatConfig Alerting Alerting @@ -70,4 +71,8 @@ func (a *Alert) PreCheck() { if a.Heartbeat.EngineName == "" { a.Heartbeat.EngineName = "default" } + + if a.EngineDelay == 0 { + a.EngineDelay = 30 + } } diff --git a/cmd/alert/main.go b/cmd/alert/main.go index 7c1874aa..633cc9e8 100644 --- a/cmd/alert/main.go +++ b/cmd/alert/main.go @@ -17,7 +17,7 @@ import ( var ( showVersion = flag.Bool("version", false, "Show version.") - configDir = flag.String("configs", osx.GetEnv("N9E_CONFIGS", "etc"), "Specify configuration directory.(env:N9E_CONFIGS)") + configDir = flag.String("configs", osx.GetEnv("N9E_ALERT_CONFIGS", "etc"), "Specify configuration directory.(env:N9E_ALERT_CONFIGS)") cryptoKey = flag.String("crypto-key", "", "Specify the secret key for configuration file field encryption.") ) diff --git a/cmd/edge/edge.go b/cmd/edge/edge.go new file mode 100644 index 00000000..a7614cf7 --- /dev/null +++ b/cmd/edge/edge.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "fmt" + + "github.com/ccfos/nightingale/v6/alert" + "github.com/ccfos/nightingale/v6/alert/astats" + "github.com/ccfos/nightingale/v6/alert/process" + "github.com/ccfos/nightingale/v6/conf" + "github.com/ccfos/nightingale/v6/memsto" + "github.com/ccfos/nightingale/v6/pkg/ctx" + "github.com/ccfos/nightingale/v6/pkg/httpx" + "github.com/ccfos/nightingale/v6/pkg/logx" + "github.com/ccfos/nightingale/v6/prom" + "github.com/ccfos/nightingale/v6/pushgw/idents" + "github.com/ccfos/nightingale/v6/pushgw/writer" + + alertrt "github.com/ccfos/nightingale/v6/alert/router" + pushgwrt "github.com/ccfos/nightingale/v6/pushgw/router" +) + +func Initialize(configDir string, cryptoKey string) (func(), error) { + config, err := conf.InitConfig(configDir, cryptoKey) + if err != nil { + return nil, fmt.Errorf("failed to init config: %v", err) + } + + logxClean, err := logx.Init(config.Log) + if err != nil { + return nil, err + } + + ctx := ctx.NewContext(context.Background(), nil, false, config.CenterApi) + + syncStats := memsto.NewSyncStats() + + targetCache := memsto.NewTargetCache(ctx, syncStats, nil) + busiGroupCache := memsto.NewBusiGroupCache(ctx, syncStats) + idents := idents.New(ctx) + writers := writer.NewWriters(config.Pushgw) + pushgwRouter := pushgwrt.New(config.HTTP, config.Pushgw, targetCache, busiGroupCache, idents, writers, ctx) + r := httpx.GinEngine(config.Global.RunMode, config.HTTP) + pushgwRouter.Config(r) + + if !config.Alert.Disable { + alertStats := astats.NewSyncStats() + dsCache := memsto.NewDatasourceCache(ctx, syncStats) + alertMuteCache := memsto.NewAlertMuteCache(ctx, syncStats) + alertRuleCache := memsto.NewAlertRuleCache(ctx, syncStats) + notifyConfigCache := memsto.NewNotifyConfigCache(ctx) + + promClients := prom.NewPromClient(ctx, config.Alert.Heartbeat) + externalProcessors := process.NewExternalProcessors() + + alert.Start(config.Alert, config.Pushgw, syncStats, alertStats, externalProcessors, targetCache, busiGroupCache, alertMuteCache, alertRuleCache, notifyConfigCache, dsCache, ctx, promClients) + + alertrtRouter := alertrt.New(config.HTTP, config.Alert, alertMuteCache, targetCache, busiGroupCache, alertStats, ctx, externalProcessors) + + alertrtRouter.Config(r) + } + + httpClean := httpx.Init(config.HTTP, r) + + return func() { + logxClean() + httpClean() + }, nil +} diff --git a/cmd/edge/main.go b/cmd/edge/main.go new file mode 100644 index 00000000..4fbb5cb2 --- /dev/null +++ b/cmd/edge/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "os/signal" + "syscall" + + "github.com/ccfos/nightingale/v6/pkg/osx" + "github.com/ccfos/nightingale/v6/pkg/version" + + "github.com/toolkits/pkg/runner" +) + +var ( + showVersion = flag.Bool("version", false, "Show version.") + configDir = flag.String("configs", osx.GetEnv("N9E_EDGE_CONFIGS", "etc"), "Specify configuration directory.(env:N9E_EDGE_CONFIGS)") + cryptoKey = flag.String("crypto-key", "", "Specify the secret key for configuration file field encryption.") +) + +func main() { + flag.Parse() + + if *showVersion { + fmt.Println(version.Version) + os.Exit(0) + } + + printEnv() + + cleanFunc, err := Initialize(*configDir, *cryptoKey) + if err != nil { + log.Fatalln("failed to initialize:", err) + } + + code := 1 + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + +EXIT: + for { + sig := <-sc + fmt.Println("received signal:", sig.String()) + switch sig { + case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: + code = 0 + break EXIT + case syscall.SIGHUP: + // reload configuration? + default: + break EXIT + } + } + + cleanFunc() + fmt.Println("process exited") + os.Exit(code) +} + +func printEnv() { + runner.Init() + fmt.Println("runner.cwd:", runner.Cwd) + fmt.Println("runner.hostname:", runner.Hostname) + fmt.Println("runner.fd_limits:", runner.FdLimits()) + fmt.Println("runner.vm_limits:", runner.VMLimits()) +} diff --git a/cmd/pushgw/main.go b/cmd/pushgw/main.go index 55905b1c..caceb0e6 100644 --- a/cmd/pushgw/main.go +++ b/cmd/pushgw/main.go @@ -17,7 +17,7 @@ import ( var ( showVersion = flag.Bool("version", false, "Show version.") - configDir = flag.String("configs", osx.GetEnv("N9E_CONFIGS", "etc"), "Specify configuration directory.(env:N9E_CONFIGS)") + configDir = flag.String("configs", osx.GetEnv("N9E_PUSHGW_CONFIGS", "etc"), "Specify configuration directory.(env:N9E_PUSHGW_CONFIGS)") cryptoKey = flag.String("crypto-key", "", "Specify the secret key for configuration file field encryption.") ) diff --git a/etc/alert.toml.example b/etc/alert.toml.example deleted file mode 100644 index caf34d30..00000000 --- a/etc/alert.toml.example +++ /dev/null @@ -1,60 +0,0 @@ -[Global] -RunMode = "release" - -[CenterApi] -Addrs = ["http://127.0.0.1:17000"] -[CenterApi.BasicAuth] -user001 = "ccc26da7b9aba533cbb263a36c07dcc5" - -[Alert] -[Alert.Heartbeat] -# auto detect if blank -IP = "" -# unit ms -Interval = 1000 -EngineName = "default02" - -[Log] -# log write dir -Dir = "logs" -# log level: DEBUG INFO WARNING ERROR -Level = "DEBUG" -# stdout, stderr, file -Output = "stdout" -# # rotate by time -# KeepHours = 4 -# # rotate by size -# RotateNum = 3 -# # unit: MB -# RotateSize = 256 - -[HTTP] -# http listening address -Host = "0.0.0.0" -# http listening port -Port = 17001 -# https cert file path -CertFile = "" -# https key file path -KeyFile = "" -# whether print access log -PrintAccessLog = false -# whether enable pprof -PProf = false -# expose prometheus /metrics? -ExposeMetrics = true -# http graceful shutdown timeout, unit: s -ShutdownTimeout = 30 -# max content length: 64M -MaxContentLength = 67108864 -# http server read timeout, unit: s -ReadTimeout = 20 -# http server write timeout, unit: s -WriteTimeout = 40 -# http server idle timeout, unit: s -IdleTimeout = 120 - -[HTTP.Alert] -Enable = true -[HTTP.Alert.BasicAuth] -user001 = "ccc26da7b9aba533cbb263a36c07dcc5" \ No newline at end of file diff --git a/etc/pushgw.toml.example b/etc/edge.toml.example similarity index 85% rename from etc/pushgw.toml.example rename to etc/edge.toml.example index 951111af..971310cf 100644 --- a/etc/pushgw.toml.example +++ b/etc/edge.toml.example @@ -6,6 +6,67 @@ Addrs = ["http://127.0.0.1:17000"] [CenterApi.BasicAuth] user001 = "ccc26da7b9aba533cbb263a36c07dcc5" +[Log] +# log write dir +Dir = "logs" +# log level: DEBUG INFO WARNING ERROR +Level = "DEBUG" +# stdout, stderr, file +Output = "stdout" +# # rotate by time +# KeepHours = 4 +# # rotate by size +# RotateNum = 3 +# # unit: MB +# RotateSize = 256 + +[HTTP] +# http listening address +Host = "0.0.0.0" +# http listening port +Port = 17000 +# https cert file path +CertFile = "" +# https key file path +KeyFile = "" +# whether print access log +PrintAccessLog = false +# whether enable pprof +PProf = false +# expose prometheus /metrics? +ExposeMetrics = true +# http graceful shutdown timeout, unit: s +ShutdownTimeout = 30 +# max content length: 64M +MaxContentLength = 67108864 +# http server read timeout, unit: s +ReadTimeout = 20 +# http server write timeout, unit: s +WriteTimeout = 40 +# http server idle timeout, unit: s +IdleTimeout = 120 + +[HTTP.APIForAgent] +Enable = true +# [HTTP.APIForAgent.BasicAuth] +# user001 = "ccc26da7b9aba533cbb263a36c07dcc5" + +[HTTP.APIForService] +Enable = true +[HTTP.APIForService.BasicAuth] +user001 = "ccc26da7b9aba533cbb263a36c07dcc5" + +[Alert] +[Alert.Heartbeat] +# auto detect if blank +IP = "" +# unit ms +Interval = 1000 +EngineName = "edge" + +# [Alert.Alerting] +# NotifyConcurrency = 10 + [Pushgw] # use target labels in database instead of in series LabelRewrite = true @@ -56,48 +117,3 @@ MaxIdleConnsPerHost = 100 # Regex = "([^:]+)(?::\\d+)?" # Replacement = "$1:80" # TargetLabel = "__address__" - -[Log] -# log write dir -Dir = "logs" -# log level: DEBUG INFO WARNING ERROR -Level = "DEBUG" -# stdout, stderr, file -Output = "stdout" -# # rotate by time -# KeepHours = 4 -# # rotate by size -# RotateNum = 3 -# # unit: MB -# RotateSize = 256 - -[HTTP] -# http listening address -Host = "0.0.0.0" -# http listening port -Port = 17000 -# https cert file path -CertFile = "" -# https key file path -KeyFile = "" -# whether print access log -PrintAccessLog = false -# whether enable pprof -PProf = false -# expose prometheus /metrics? -ExposeMetrics = true -# http graceful shutdown timeout, unit: s -ShutdownTimeout = 30 -# max content length: 64M -MaxContentLength = 67108864 -# http server read timeout, unit: s -ReadTimeout = 20 -# http server write timeout, unit: s -WriteTimeout = 40 -# http server idle timeout, unit: s -IdleTimeout = 120 - -[HTTP.Pushgw] -Enable = true -# [HTTP.Pushgw.BasicAuth] -# user001 = "ccc26da7b9aba533cbb263a36c07dcc5" \ No newline at end of file diff --git a/fe.sh b/fe.sh index c64e4f5d..a96fd987 100755 --- a/fe.sh +++ b/fe.sh @@ -3,7 +3,7 @@ TAG=$(curl -sX GET https://api.github.com/repos/n9e/fe/releases/latest | awk '/tag_name/{print $4;exit}' FS='[""]') VERSION=$(echo $TAG) -curl -s -o n9e-fe-${VERSION}.tar.gz -L https://github.com/n9e/fe/releases/download/${TAG}/n9e-fe-${VERSION}.tar.gz +curl -o n9e-fe-${VERSION}.tar.gz -L https://github.com/n9e/fe/releases/download/${TAG}/n9e-fe-${VERSION}.tar.gz tar zxvf n9e-fe-${VERSION}.tar.gz