diff --git a/ziti-probe/README.md b/ziti-probe/README.md index d702a6552..9a2907d9d 100644 --- a/ziti-probe/README.md +++ b/ziti-probe/README.md @@ -5,7 +5,8 @@ Ziti Probe ## Setup -create config type for metric collection service with the following schema (this is optional) +create `ziti-probe-config.v1` config type for metric collection service with the following schema + (this is optional) ```json { "$id": "http://ziti-edge.netfoundry.io/schemas/ziti-probe-config.v1.json", @@ -19,6 +20,12 @@ create config type for metric collection service with the following schema (this "dbName": { "type": "string" }, + "dbUser": { + "type": "string" + }, + "dbPassword": { + "type": "string" + }, "interval": { "type": "integer", "minimum": 1, @@ -33,7 +40,9 @@ it wil be used to configure the service in the next step), This is optional { "dbType": "influxdb", // only influxdb is currently supported "dbName": "ziti", // name of database in Influxdb, ziti is the default - "interval": 60 // how often metrics are uploaded (in seconds), 60 is the default + "dbUser": "superuser", // optional + "dbPassword": "supersecret", // optional + "interval": 60 // how often metrics are uploaded (in seconds), 300 (every 5 minutes) is the default } ``` create service with a router termination @@ -54,7 +63,8 @@ $ ziti-probe run ``` #### What's happening? 1. `ziti-probe` authenticates to Ziti controller with provided identity - 1. it checks that `probe-service` is available and get configuration. If configuration is not assigned default values are used. + 1. it checks that `probe-service` is available and get configuration. + If configuration is not assigned default values are used. 1. it opens connections to all edge routers configured for `probe-service` (refer to ziti policies) 1. each connection performs a round-trip latency probe to each of edge routers every 30 seconds 1. `ziti-probe` then submits collected metrics to `probe-service`. diff --git a/ziti-probe/subcmd/run.go b/ziti-probe/subcmd/run.go index 3e60c73c1..4817239b7 100644 --- a/ziti-probe/subcmd/run.go +++ b/ziti-probe/subcmd/run.go @@ -8,6 +8,7 @@ import ( "github.com/netfoundry/ziti-foundation/metrics/metrics_pb" "github.com/netfoundry/ziti-sdk-golang/ziti" "github.com/netfoundry/ziti-sdk-golang/ziti/config" + "github.com/netfoundry/ziti-sdk-golang/ziti/edge" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "net" @@ -37,6 +38,8 @@ var log = pfxlog.Logger() type probeCfg struct { dbName string dbType string + dbUser string + dbPassword string interval int } @@ -55,7 +58,6 @@ type probe struct{ } var theProbe = &probe{ - cfg: &defaultConfig, closer: make(chan interface{}), } @@ -67,8 +69,8 @@ func (p *probe) sendMetrics() { for { select { case <-time.After(time.Duration(p.cfg.interval) * time.Second): - message := p.latest.Load().(*metrics_pb.MetricsMessage) - if message == nil { + message, ok := p.latest.Load().(*metrics_pb.MetricsMessage) + if !ok { continue } bp, err := metrics.AsBatch(message) @@ -95,23 +97,29 @@ func (p *probe) run(cmd *cobra.Command, args []string) { if cfg, err := config.NewFromFile(args[0]); err != nil { log.Fatalf("failed to load config from file[%s]", args[0], err) } else { + cfg.ConfigTypes = append(cfg.ConfigTypes, "ziti-probe-config.v1") p.ctx = ziti.NewContextWithConfig(cfg) } } - _, _ = p.ctx.GetServices() + if _, err = p.ctx.GetServices(); err != nil { + log.Fatal("failed to load available services") + } - _, found := p.ctx.GetService("probe-service") + service, found := p.ctx.GetService("probe-service") if !found { log.Fatal("required service was not found") } - p.indb, err = p.createInfluxDbClient() + p.cfg = getProbeConfig(service) + + p.indb, err = p.createInfluxDbClient(p.cfg) if err != nil { log.Error(err) return } + _, res, err := p.indb.Ping() if err != nil { log.Fatalf("failed to get server info", err) @@ -129,7 +137,22 @@ func (p *probe) run(cmd *cobra.Command, args []string) { p.ctx.Close() } -func (p *probe) createInfluxDbClient() (*influxdb.Client, error) { +func getProbeConfig(service *edge.Service) *probeCfg { + cfg, found := service.Configs["ziti-probe-config.v1"] + if !found { + return &defaultConfig + } + + return &probeCfg{ + dbName: cfg["dbName"].(string), + dbType: cfg["dbType"].(string), + dbUser: cfg["dbUser"].(string), + dbPassword: cfg["dbPassword"].(string), + interval: int(cfg["interval"].(float64)), + } +} + +func (p *probe) createInfluxDbClient(cfg *probeCfg) (*influxdb.Client, error) { httpC := &http.Client{ Transport: &http.Transport{ @@ -143,6 +166,8 @@ func (p *probe) createInfluxDbClient() (*influxdb.Client, error) { config := influxdb.NewConfig() u, _ := url.Parse("http://probe-service") config.URL = *u + config.Username = cfg.dbUser + config.Password = cfg.dbPassword clt, _ := influxdb.NewClient(config) ic := reflect.ValueOf(clt)