diff --git a/.env b/.env index d8962cf..0fabcda 100644 --- a/.env +++ b/.env @@ -3,4 +3,8 @@ ADMIN_PASSWORD=admin RAS_HOST=192.168.10.10 RAS_PORT=2545 CLS_USER=admin -CLS_PASS=admin \ No newline at end of file +CLS_PASS=admin +MODE=push +PUSH_INTERVAL=500 +PUSH_HOST=pushgateway +PUSH_PORT=9091 \ No newline at end of file diff --git a/cmd/app/main.go b/cmd/app/main.go index 44da2d6..c716e07 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -8,7 +8,7 @@ import ( func main() { - err := app.RunPusher() + err := app.Run() if err != nil { log.Fatal(err) diff --git a/internal/app/app.go b/internal/app/app.go index f955fba..5abaffd 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -8,7 +8,7 @@ import ( "net/http" "os" - pusher "github.com/Chipazawra/v8-1c-cluster-pde/internal/Pusher" + pusher "github.com/Chipazawra/v8-1c-cluster-pde/internal/pusher" "github.com/Chipazawra/v8-1c-cluster-pde/internal/rpHostsCollector" "github.com/caarlos0/env" rascli "github.com/khorevaa/ras-client" @@ -16,11 +16,20 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) +const ( + push string = "push" + pull string = "pull" +) + var ( - conf Config - hostFlag string - portFlag string - exposeFlag string + conf Config + RAS_HOST string + RAS_PORT string + PULL_EXPOSE string + MODE string + PUSH_INTERVAL int + PUSH_HOST string + PUSH_PORT string ) func init() { @@ -28,43 +37,80 @@ func init() { log.Fatalf("app: config...") } + log.Printf("v8-1c-cluster-pde: config from env:\n %#v", conf) + log.SetOutput(os.Stdout) - flag.StringVar(&hostFlag, "host", "", "cluster host.") - flag.StringVar(&portFlag, "port", "", "cluster port.") - flag.StringVar(&exposeFlag, "expose", "", "metrics port.") + flag.StringVar(&RAS_HOST, "ras-host", "", "cluster host.") + flag.StringVar(&RAS_PORT, "ras-port", "", "cluster port.") + flag.StringVar(&PULL_EXPOSE, "pull-expose", "", "metrics port.") + flag.StringVar(&MODE, "mode", "", "mode push or pull") + flag.IntVar(&PUSH_INTERVAL, "push-interval", 0, "mode push or pull") + flag.StringVar(&PUSH_HOST, "push-host", "", "pushgateway host") + flag.StringVar(&PUSH_PORT, "push-port", "", "pushgateway port") flag.Parse() - if hostFlag != "" { - conf.Host = hostFlag + if RAS_HOST != "" { + conf.RAS_HOST = RAS_HOST } - if portFlag != "" { - conf.Port = portFlag + if RAS_PORT != "" { + conf.RAS_PORT = RAS_PORT } - if exposeFlag != "" { - conf.Expose = exposeFlag + if PULL_EXPOSE != "" { + conf.PULL_EXPOSE = PULL_EXPOSE } + + if MODE != "" { + conf.MODE = MODE + } + + if PUSH_INTERVAL != 0 { + conf.PUSH_INTERVAL = PUSH_INTERVAL + } + + if PUSH_HOST != "" { + conf.PUSH_HOST = PUSH_HOST + } + + if PUSH_PORT != "" { + conf.PUSH_PORT = PUSH_PORT + } + + log.Printf("v8-1c-cluster-pde: overrided config from stdin:\n%#v", conf) } func Run() error { - rcli := rascli.NewClient(fmt.Sprintf("%s:%s", conf.Host, conf.Port)) - rcli.AuthenticateAgent(conf.User, conf.Pass) - log.Printf("cluster-pde connected to RAS: %v", fmt.Sprintf("%s:%s", conf.Host, conf.Port)) + rcli := rascli.NewClient(fmt.Sprintf("%s:%s", conf.RAS_HOST, conf.RAS_PORT)) + rcli.AuthenticateAgent(conf.CLS_USER, conf.CLS_PASS) + log.Printf("v8-1c-cluster-pde: connected to RAS %v", fmt.Sprintf("%s:%s", conf.RAS_HOST, conf.RAS_PORT)) defer rcli.Close() + switch conf.MODE { + case push: + return RunPusher(rcli) + case pull: + return RunPuller(rcli) + default: + return fmt.Errorf("v8-1c-cluster-pde: %v", "undefined mode") + } + +} + +func RunPuller(rasapi rascli.Api) error { + log.Printf("v8-1c-cluster-pde: runing in %v mode", conf.MODE) promRegistry := prometheus.NewRegistry() - promRegistry.MustRegister(rpHostsCollector.New(rcli)) + promRegistry.MustRegister(rpHostsCollector.New(rasapi)) http.Handle("/metrics", promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{}), ) - log.Printf("cluster-pde is running on: %v", fmt.Sprintf("%s:%s", "", conf.Expose)) + log.Printf("v8-1c-cluster-pde: listen %v", fmt.Sprintf("%s:%s", "", conf.PULL_EXPOSE)) - err := http.ListenAndServe(fmt.Sprintf("%s:%s", "", conf.Expose), nil) + err := http.ListenAndServe(fmt.Sprintf("%s:%s", "", conf.PULL_EXPOSE), nil) if err != nil { return fmt.Errorf("app: %v", err) } @@ -72,16 +118,11 @@ func Run() error { return nil } -func RunPusher() error { - - rcli := rascli.NewClient(fmt.Sprintf("%s:%s", conf.Host, conf.Port)) - rcli.AuthenticateAgent(conf.User, conf.Pass) - log.Printf("cluster-pde connected to RAS: %v", fmt.Sprintf("%s:%s", conf.Host, conf.Port)) - defer rcli.Close() - +func RunPusher(rasapi rascli.Api) error { + log.Printf("v8-1c-cluster-pde: runing in %v mode", conf.MODE) return pusher.New( - rpHostsCollector.New(rcli), - "pushgateway:9091", + rpHostsCollector.New(rasapi), + fmt.Sprintf("%s:%s", conf.PUSH_HOST, conf.PUSH_PORT), pusher.WithInterval(500), ).Run(context.Background()) } diff --git a/internal/app/config.go b/internal/app/config.go index a5a234e..e835b00 100644 --- a/internal/app/config.go +++ b/internal/app/config.go @@ -1,9 +1,13 @@ package app type Config struct { - Host string `env:"RAS_HOST" envDefault:"localhost"` - Port string `env:"RAS_PORT" envDefault:"1545"` - User string `env:"CLS_USER"` - Pass string `env:"CLS_PASS"` - Expose string `env:"EXPOSE" envDefault:"9096"` + RAS_HOST string `env:"RAS_HOST" envDefault:"localhost"` + RAS_PORT string `env:"RAS_PORT" envDefault:"1545"` + CLS_USER string `env:"CLS_USER"` + CLS_PASS string `env:"CLS_PASS"` + PULL_EXPOSE string `env:"PULL_EXPOSE" envDefault:"9096"` + MODE string `env:"MODE" envDefault:"pull"` + PUSH_INTERVAL int `env:"PUSH_INTERVAL" envDefault:"500"` + PUSH_HOST string `env:"PUSH_HOST" envDefault:"localhost"` + PUSH_PORT string `env:"PUSH_PORT" envDefault:"9091"` }