You've already forked v8-1c-cluster-pde
mirror of
https://github.com/Chipazawra/v8-1c-cluster-pde.git
synced 2025-11-06 08:49:31 +02:00
refactor
This commit is contained in:
2
.env
2
.env
@@ -4,7 +4,7 @@ RAS_HOST=192.168.10.10
|
|||||||
RAS_PORT=2545
|
RAS_PORT=2545
|
||||||
CLS_USER=admin
|
CLS_USER=admin
|
||||||
CLS_PASS=admin
|
CLS_PASS=admin
|
||||||
MODE=pull
|
MODE=push
|
||||||
PUSH_INTERVAL=500
|
PUSH_INTERVAL=500
|
||||||
PUSH_HOST=pushgateway
|
PUSH_HOST=pushgateway
|
||||||
PUSH_PORT=9091
|
PUSH_PORT=9091
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
err := app.Run()
|
err := app.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
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/Chipazawra/v8-1c-cluster-pde/internal/rpHostsCollector"
|
||||||
@@ -33,14 +35,13 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
||||||
if err := env.Parse(&conf); err != nil {
|
if err := env.Parse(&conf); err != nil {
|
||||||
log.Fatalf("app: config...")
|
log.Fatalf("app: config...")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("v8-1c-cluster-pde: config from env:\n %#v", conf)
|
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_HOST, "ras-host", "", "cluster host.")
|
||||||
flag.StringVar(&RAS_PORT, "ras-port", "", "cluster port.")
|
flag.StringVar(&RAS_PORT, "ras-port", "", "cluster port.")
|
||||||
flag.StringVar(&PULL_EXPOSE, "pull-expose", "", "metrics 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 := rascli.NewClient(fmt.Sprintf("%s:%s", conf.RAS_HOST, conf.RAS_PORT))
|
||||||
rcli.AuthenticateAgent(conf.CLS_USER, conf.CLS_PASS)
|
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()
|
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 {
|
switch conf.MODE {
|
||||||
case push:
|
case push:
|
||||||
return RunPusher(rcli)
|
go RunPusher_(ctx, errchan, rcli)
|
||||||
case pull:
|
case pull:
|
||||||
return RunPuller(rcli)
|
go RunPuller_(ctx, errchan, rcli)
|
||||||
default:
|
default:
|
||||||
|
{
|
||||||
|
cancel()
|
||||||
return fmt.Errorf("v8-1c-cluster-pde: %v", "undefined mode")
|
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 {
|
func RunPuller(rasapi rascli.Api) error {
|
||||||
|
|
||||||
log.Printf("v8-1c-cluster-pde: runing in %v mode", conf.MODE)
|
log.Printf("v8-1c-cluster-pde: runing in %v mode", conf.MODE)
|
||||||
promRegistry := prometheus.NewRegistry()
|
promRegistry := prometheus.NewRegistry()
|
||||||
promRegistry.MustRegister(rpHostsCollector.New(rasapi))
|
promRegistry.MustRegister(rpHostsCollector.New(rasapi))
|
||||||
@@ -127,3 +150,42 @@ func RunPusher(rasapi rascli.Api) error {
|
|||||||
pusher.WithInterval(500),
|
pusher.WithInterval(500),
|
||||||
).Run(context.Background())
|
).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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user