1
0
mirror of https://github.com/Chipazawra/v8-1c-cluster-pde.git synced 2025-03-21 21:17:03 +02:00

192 lines
4.5 KiB
Go
Raw Normal View History

2021-12-27 15:02:41 +03:00
package app
2021-12-27 15:11:48 +03:00
2021-12-27 21:15:02 +03:00
import (
2021-12-29 12:31:50 +03:00
"context"
2021-12-27 21:15:02 +03:00
"flag"
"fmt"
"log"
"net/http"
2021-12-28 23:06:33 +03:00
"os"
2021-12-30 14:07:24 +03:00
"os/signal"
"syscall"
2021-12-27 21:15:02 +03:00
2021-12-29 13:48:16 +03:00
pusher "github.com/Chipazawra/v8-1c-cluster-pde/internal/pusher"
2021-12-28 14:23:42 +03:00
"github.com/Chipazawra/v8-1c-cluster-pde/internal/rpHostsCollector"
2021-12-27 21:15:02 +03:00
"github.com/caarlos0/env"
2021-12-28 14:23:42 +03:00
rascli "github.com/khorevaa/ras-client"
2021-12-27 21:15:02 +03:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
2021-12-29 13:48:16 +03:00
const (
push string = "push"
pull string = "pull"
)
2021-12-27 21:15:02 +03:00
var (
2021-12-29 13:48:16 +03:00
conf Config
RAS_HOST string
RAS_PORT string
PULL_EXPOSE string
MODE string
PUSH_INTERVAL int
PUSH_HOST string
PUSH_PORT string
2021-12-27 21:15:02 +03:00
)
func init() {
2021-12-30 14:07:24 +03:00
2021-12-27 21:15:02 +03:00
if err := env.Parse(&conf); err != nil {
log.Fatalf("app: config...")
}
2021-12-29 13:48:16 +03:00
log.Printf("v8-1c-cluster-pde: config from env:\n %#v", conf)
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")
2021-12-27 21:15:02 +03:00
flag.Parse()
2021-12-29 13:48:16 +03:00
if RAS_HOST != "" {
conf.RAS_HOST = RAS_HOST
}
if RAS_PORT != "" {
conf.RAS_PORT = RAS_PORT
2021-12-27 21:15:02 +03:00
}
2021-12-29 13:48:16 +03:00
if PULL_EXPOSE != "" {
conf.PULL_EXPOSE = PULL_EXPOSE
2021-12-27 21:15:02 +03:00
}
2021-12-28 14:23:42 +03:00
2021-12-29 13:48:16 +03:00
if MODE != "" {
conf.MODE = MODE
2021-12-28 14:23:42 +03:00
}
2021-12-29 13:48:16 +03:00
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)
2021-12-27 21:15:02 +03:00
}
2021-12-27 15:11:48 +03:00
func Run() error {
2021-12-27 21:15:02 +03:00
2021-12-29 13:48:16 +03:00
rcli := rascli.NewClient(fmt.Sprintf("%s:%s", conf.RAS_HOST, conf.RAS_PORT))
rcli.AuthenticateAgent(conf.CLS_USER, conf.CLS_PASS)
2021-12-28 14:23:42 +03:00
defer rcli.Close()
2021-12-27 21:15:02 +03:00
2021-12-30 14:07:24 +03:00
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)
2021-12-29 13:48:16 +03:00
switch conf.MODE {
case push:
2021-12-30 14:07:24 +03:00
go RunPusher_(ctx, errchan, rcli)
2021-12-29 13:48:16 +03:00
case pull:
2021-12-30 14:07:24 +03:00
go RunPuller_(ctx, errchan, rcli)
2021-12-29 13:48:16 +03:00
default:
2021-12-30 14:07:24 +03:00
{
cancel()
return fmt.Errorf("v8-1c-cluster-pde: %v", "undefined mode")
}
2021-12-29 13:48:16 +03:00
}
2021-12-30 14:07:24 +03:00
log.Printf("v8-1c-cluster-pde: received signal %v",
<-sigchan,
)
cancel()
return <-errchan
2021-12-29 13:48:16 +03:00
}
func RunPuller(rasapi rascli.Api) error {
2021-12-30 14:07:24 +03:00
2021-12-29 13:48:16 +03:00
log.Printf("v8-1c-cluster-pde: runing in %v mode", conf.MODE)
2021-12-27 21:15:02 +03:00
promRegistry := prometheus.NewRegistry()
2021-12-29 13:48:16 +03:00
promRegistry.MustRegister(rpHostsCollector.New(rasapi))
2021-12-28 14:23:42 +03:00
http.Handle("/metrics",
promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{}),
2021-12-27 22:48:08 +03:00
)
2021-12-29 13:48:16 +03:00
log.Printf("v8-1c-cluster-pde: listen %v", fmt.Sprintf("%s:%s", "", conf.PULL_EXPOSE))
2021-12-28 23:06:33 +03:00
2021-12-29 13:48:16 +03:00
err := http.ListenAndServe(fmt.Sprintf("%s:%s", "", conf.PULL_EXPOSE), nil)
2021-12-28 14:23:42 +03:00
if err != nil {
2021-12-29 10:46:25 +03:00
return fmt.Errorf("app: %v", err)
2021-12-28 14:23:42 +03:00
}
2021-12-27 21:15:02 +03:00
2021-12-28 14:23:42 +03:00
return nil
}
2021-12-29 12:31:50 +03:00
2021-12-29 13:48:16 +03:00
func RunPusher(rasapi rascli.Api) error {
2021-12-29 14:15:40 +03:00
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))
2021-12-29 12:31:50 +03:00
return pusher.New(
2021-12-29 13:48:16 +03:00
rpHostsCollector.New(rasapi),
fmt.Sprintf("%s:%s", conf.PUSH_HOST, conf.PUSH_PORT),
2021-12-29 12:31:50 +03:00
pusher.WithInterval(500),
).Run(context.Background())
}
2021-12-30 14:07:24 +03:00
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)
}