Created
May 22, 2021 09:27
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd /home/$USER | |
openssl ecparam -genkey -name secp384r1 -out server.key | |
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650 -subj "/CN=selfsigned.gnmi-gateway.local" | |
git clone https://github.com/openconfig/gnmi | |
cd /home/$USER/gnmi/cmd/gnmi_cli && go build ~/gnmi/cmd/gnmi_cli/gnmi_cli.go | |
go get github.com/golang/glog && go get github.com/openconfig/gnmi/testing/fake/proto | |
go get github.com/kylelemons/godebug/pretty && go get github.com/openconfig/grpctunnel/proto/tunnel | |
go get github.com/cenkalti/backoff/v4 | |
go run /home/$USER/gnmi/testing/fake/gnmi/cmd/gen_fake_config/gen_config.go | |
go build /home/$USER/gnmi/testing/fake/gnmi/cmd/fake_server/server.go | |
~/gnmi/testing/fake/gnmi/cmd/fake_server/server --config ~/gnmi/testing/fake/gnmi/cmd/fake_server/config.pb.txt --text --port 8080 --server_crt ~/server.crt --server_key ~/server.key --allow_no_client_auth --logtostderr | |
# | |
# Open a new terminal window... | |
# | |
git clone https://github.com/openconfig/gnmi-gateway | |
cat <<EOF>> ~/gnmi-gateway/targets.json | |
{ | |
"request": { | |
"default": { | |
"subscribe": { | |
"prefix": { | |
}, | |
"subscription": [ | |
{ | |
"path": { | |
"elem": [ | |
{ | |
"name": "interfaces" | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
}, | |
"target": { | |
"localhost": { | |
"addresses": [ | |
"localhost:8080" | |
], | |
"request": "default", | |
"meta": { | |
"NoTLSVerify": "yes" | |
} | |
} | |
} | |
} | |
EOF | |
rm ~/gnmi-gateway/gateway/exporters/all/all.go | |
cat <<EOF>> ~/gnmi-gateway/gateway/exporters/all/all.go | |
package all | |
import ( | |
_ "github.com/openconfig/gnmi-gateway/gateway/exporters/debug" | |
_ "github.com/openconfig/gnmi-gateway/gateway/exporters/influxdb" | |
_ "github.com/openconfig/gnmi-gateway/gateway/exporters/kafka" | |
_ "github.com/openconfig/gnmi-gateway/gateway/exporters/prometheus" | |
_ "github.com/openconfig/gnmi-gateway/gateway/exporters/dynatrace" | |
) | |
EOF | |
mkdir ~/gnmi-gateway/gateway/exporters/dynatrace && touch ~/gnmi-gateway/gateway/exporters/dynatrace/dynatrace.go | |
cat <<EOF>> ~/gnmi-gateway/gateway/exporters/dynatrace/dynatrace.go | |
package dynatrace | |
import ( | |
"github.com/openconfig/gnmi/cache" | |
"github.com/openconfig/gnmi/ctree" | |
gnmipb "github.com/openconfig/gnmi/proto/gnmi" | |
"github.com/openconfig/gnmi-gateway/gateway/configuration" | |
"github.com/openconfig/gnmi-gateway/gateway/exporters" | |
"github.com/openconfig/gnmi-gateway/gateway/utils" | |
"strings" | |
"fmt" | |
) | |
const Name = "dynatrace" | |
var _ exporters.Exporter = new(DynatraceExporter) | |
func init() { | |
exporters.Register(Name, NewDynatraceExporter) | |
} | |
func NewDynatraceExporter(config *configuration.GatewayConfig) exporters.Exporter { | |
exporter := &DynatraceExporter{ | |
config: config, | |
} | |
return exporter | |
} | |
type DynatraceExporter struct { | |
cache *cache.Cache | |
config *configuration.GatewayConfig | |
} | |
func (e *DynatraceExporter) Name() string { | |
return Name | |
} | |
func (e *DynatraceExporter) Export(leaf *ctree.Leaf) { | |
// STILL A WORK IN PROGRESS... NOT READY FOR USE... | |
notification := leaf.Value().(*gnmipb.Notification) | |
for _, update := range notification.Update { | |
value, isNumber := utils.GetNumberValues(update.Val) | |
val := fmt.Sprintf("%f", value) | |
if !isNumber { | |
e.config.Log.Info().Msg("Value is: " + val) | |
e.config.Log.Info().Msg("[dynatrace.go] Skipping value because its not a number: " + val) | |
continue | |
} | |
//metricName, labels := UpdateToMetricNameAndLabels(notification.GetPrefix(), update) | |
metricName, _ := UpdateToMetricNameAndLabels(notification.GetPrefix(), update) | |
e.config.Log.Info().Msg("[dynatrace.go] Got: " + metricName + ", " + val) | |
} | |
// TODO export to Dynatrace | |
} | |
func (e *DynatraceExporter) Start(cache *cache.Cache) error { | |
_ = cache | |
e.config.Log.Info().Msg("Starting dynatrace exporter...") | |
return nil | |
} | |
func UpdateToMetricNameAndLabels(prefix *gnmipb.Path, update *gnmipb.Update) (string, map[string]string) { | |
metricName := "" | |
labels := make(map[string]string) | |
if prefix != nil { | |
target := prefix.GetTarget() | |
if target != "" { | |
labels["target"] = target | |
} | |
} | |
for _, elem := range update.Path.Elem { | |
elemName := strings.ReplaceAll(elem.Name, "-", "_") | |
if metricName == "" { | |
metricName = elemName | |
} else { | |
metricName = metricName + "_" + elemName | |
} | |
for key, value := range elem.Key { | |
labelKey := metricName + "_" + strings.ReplaceAll(key, "-", "_") | |
labels[labelKey] = value | |
} | |
} | |
return metricName, labels | |
} | |
EOF | |
cd ~/gnmi-gateway && make build | |
/home/$USER/gnmi-gateway/gnmi-gateway -TargetLoaders=json -TargetJSONFile=/home/$USER/gnmi-gateway/targets.json -Exporters=dynatrace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment