mirror of
https://github.com/Chipazawra/v8-1c-cluster-pde.git
synced 2024-11-21 16:46:33 +02:00
refactor
This commit is contained in:
parent
6bc2512dcc
commit
95ce08a734
2
.env
2
.env
@ -4,7 +4,7 @@ RAS_HOST=192.168.10.10
|
||||
RAS_PORT=2545
|
||||
CLS_USER=admin
|
||||
CLS_PASS=admin
|
||||
MODE=pull
|
||||
MODE=push
|
||||
PUSH_INTERVAL=500
|
||||
PUSH_HOST=pushgateway
|
||||
PUSH_PORT=9091
|
@ -7,7 +7,6 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
err := app.Run()
|
||||
|
||||
if err != nil {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
pusher "github.com/Chipazawra/v8-1c-cluster-pde/internal/pusher"
|
||||
"github.com/Chipazawra/v8-1c-cluster-pde/internal/rpHostsCollector"
|
||||
@ -33,14 +35,13 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
if err := env.Parse(&conf); err != nil {
|
||||
log.Fatalf("app: config...")
|
||||
}
|
||||
|
||||
log.Printf("v8-1c-cluster-pde: config from env:\n %#v", conf)
|
||||
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
flag.StringVar(&RAS_HOST, "ras-host", "", "cluster host.")
|
||||
flag.StringVar(&RAS_PORT, "ras-port", "", "cluster port.")
|
||||
flag.StringVar(&PULL_EXPOSE, "pull-expose", "", "metrics port.")
|
||||
@ -85,21 +86,43 @@ func Run() error {
|
||||
|
||||
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()
|
||||
|
||||
log.Printf("v8-1c-cluster-pde: connected to RAS %v",
|
||||
fmt.Sprintf("%s:%s", conf.RAS_HOST, conf.RAS_PORT),
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
sigchan := make(chan os.Signal, 1)
|
||||
defer close(sigchan)
|
||||
errchan := make(chan error, 1)
|
||||
defer close(errchan)
|
||||
|
||||
signal.Notify(sigchan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
switch conf.MODE {
|
||||
case push:
|
||||
return RunPusher(rcli)
|
||||
go RunPusher_(ctx, errchan, rcli)
|
||||
case pull:
|
||||
return RunPuller(rcli)
|
||||
go RunPuller_(ctx, errchan, rcli)
|
||||
default:
|
||||
return fmt.Errorf("v8-1c-cluster-pde: %v", "undefined mode")
|
||||
{
|
||||
cancel()
|
||||
return fmt.Errorf("v8-1c-cluster-pde: %v", "undefined mode")
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("v8-1c-cluster-pde: received signal %v",
|
||||
<-sigchan,
|
||||
)
|
||||
cancel()
|
||||
|
||||
return <-errchan
|
||||
}
|
||||
|
||||
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(rasapi))
|
||||
@ -127,3 +150,42 @@ func RunPusher(rasapi rascli.Api) error {
|
||||
pusher.WithInterval(500),
|
||||
).Run(context.Background())
|
||||
}
|
||||
|
||||
func RunPuller_(ctx context.Context, errchan chan<- error, rasapi rascli.Api) {
|
||||
|
||||
log.Printf("v8-1c-cluster-pde: runing in %v mode", conf.MODE)
|
||||
promRegistry := prometheus.NewRegistry()
|
||||
promRegistry.MustRegister(rpHostsCollector.New(rasapi))
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/metrics",
|
||||
promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{}),
|
||||
)
|
||||
srv := http.Server{
|
||||
Addr: fmt.Sprintf("%s:%s", "", conf.PULL_EXPOSE),
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
go func() {
|
||||
errchan <- srv.ListenAndServe()
|
||||
}()
|
||||
log.Printf("v8-1c-cluster-pde: listen %v", fmt.Sprintf("%s:%s", "", conf.PULL_EXPOSE))
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
if err := srv.Shutdown(context.Background()); err != nil {
|
||||
errchan <- fmt.Errorf("v8-1c-cluster-pde: server shutdown with err: %v", err)
|
||||
}
|
||||
log.Printf("v8-1c-cluster-pde: server shutdown")
|
||||
}
|
||||
|
||||
func RunPusher_(ctx context.Context, errchan chan<- error, rasapi rascli.Api) {
|
||||
log.Printf("v8-1c-cluster-pde: runing in %v mode pushgateway %v\n",
|
||||
conf.MODE, fmt.Sprintf("%s:%s", conf.PUSH_HOST, conf.PUSH_PORT))
|
||||
|
||||
errchan <- pusher.New(
|
||||
rpHostsCollector.New(rasapi),
|
||||
fmt.Sprintf("%s:%s", conf.PUSH_HOST, conf.PUSH_PORT),
|
||||
pusher.WithInterval(500),
|
||||
).Run(ctx)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user