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

130 lines
3.0 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-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() {
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)
2021-12-28 23:06:33 +03:00
log.SetOutput(os.Stdout)
2021-12-29 13:48:16 +03:00
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)
log.Printf("v8-1c-cluster-pde: connected to RAS %v", fmt.Sprintf("%s:%s", conf.RAS_HOST, conf.RAS_PORT))
2021-12-28 14:23:42 +03:00
defer rcli.Close()
2021-12-27 21:15:02 +03:00
2021-12-29 13:48:16 +03:00
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)
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())
}