2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2020-07-01 14:19:16 +08:00
|
|
|
package gnmi
|
2019-06-05 05:39:46 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2019-06-05 05:39:46 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2020-03-18 03:56:51 +08:00
|
|
|
"math"
|
2019-06-05 05:39:46 +08:00
|
|
|
"net"
|
2019-09-25 02:05:56 +08:00
|
|
|
"path"
|
2022-11-22 03:59:26 +08:00
|
|
|
"regexp"
|
2019-06-05 05:39:46 +08:00
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2022-04-26 21:38:02 +08:00
|
|
|
"github.com/google/gnxi/utils/xpath"
|
2021-07-28 05:28:26 +08:00
|
|
|
gnmiLib "github.com/openconfig/gnmi/proto/gnmi"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/credentials"
|
2022-08-02 19:52:17 +08:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2021-07-28 05:28:26 +08:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2021-04-10 01:15:04 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2019-06-05 14:00:24 +08:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2020-06-26 02:44:22 +08:00
|
|
|
internaltls "github.com/influxdata/telegraf/plugins/common/tls"
|
2019-06-05 05:39:46 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
jsonparser "github.com/influxdata/telegraf/plugins/parsers/json"
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2022-11-22 03:59:26 +08:00
|
|
|
// Regular expression to see if a path element contains an origin
|
|
|
|
|
var originPattern = regexp.MustCompile(`^([\w-_]+):`)
|
|
|
|
|
|
2020-07-01 14:19:16 +08:00
|
|
|
// gNMI plugin instance
|
|
|
|
|
type GNMI struct {
|
2022-07-08 02:50:40 +08:00
|
|
|
Addresses []string `toml:"addresses"`
|
|
|
|
|
Subscriptions []Subscription `toml:"subscription"`
|
|
|
|
|
TagSubscriptions []TagSubscription `toml:"tag_subscription"`
|
|
|
|
|
Aliases map[string]string `toml:"aliases"`
|
2019-06-05 05:39:46 +08:00
|
|
|
|
|
|
|
|
// Optional subscription configuration
|
|
|
|
|
Encoding string
|
|
|
|
|
Origin string
|
|
|
|
|
Prefix string
|
|
|
|
|
Target string
|
|
|
|
|
UpdatesOnly bool `toml:"updates_only"`
|
|
|
|
|
|
2020-07-01 14:19:16 +08:00
|
|
|
// gNMI target credentials
|
2019-06-05 05:39:46 +08:00
|
|
|
Username string
|
|
|
|
|
Password string
|
|
|
|
|
|
|
|
|
|
// Redial
|
2021-04-10 01:15:04 +08:00
|
|
|
Redial config.Duration
|
2019-06-05 05:39:46 +08:00
|
|
|
|
|
|
|
|
// GRPC TLS settings
|
|
|
|
|
EnableTLS bool `toml:"enable_tls"`
|
|
|
|
|
internaltls.ClientConfig
|
|
|
|
|
|
|
|
|
|
// Internal state
|
2021-07-28 05:28:26 +08:00
|
|
|
internalAliases map[string]string
|
|
|
|
|
acc telegraf.Accumulator
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
wg sync.WaitGroup
|
2022-07-08 02:50:40 +08:00
|
|
|
legacyTags bool
|
2019-09-24 06:39:50 +08:00
|
|
|
|
|
|
|
|
Log telegraf.Logger
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
type Worker struct {
|
|
|
|
|
address string
|
|
|
|
|
tagStore *tagNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tagNode struct {
|
|
|
|
|
elem *gnmiLib.PathElem
|
|
|
|
|
tagName string
|
|
|
|
|
value *gnmiLib.TypedValue
|
|
|
|
|
tagStore map[string][]*tagNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tagResults struct {
|
|
|
|
|
names []string
|
|
|
|
|
values []*gnmiLib.TypedValue
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 14:19:16 +08:00
|
|
|
// Subscription for a gNMI client
|
2019-06-05 05:39:46 +08:00
|
|
|
type Subscription struct {
|
|
|
|
|
Name string
|
|
|
|
|
Origin string
|
|
|
|
|
Path string
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
fullPath *gnmiLib.Path
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Subscription mode and interval
|
2021-04-10 01:15:04 +08:00
|
|
|
SubscriptionMode string `toml:"subscription_mode"`
|
|
|
|
|
SampleInterval config.Duration `toml:"sample_interval"`
|
2019-06-05 05:39:46 +08:00
|
|
|
|
|
|
|
|
// Duplicate suppression
|
2021-04-10 01:15:04 +08:00
|
|
|
SuppressRedundant bool `toml:"suppress_redundant"`
|
|
|
|
|
HeartbeatInterval config.Duration `toml:"heartbeat_interval"`
|
2022-02-07 23:18:53 +08:00
|
|
|
|
|
|
|
|
// Mark this subscription as a tag-only lookup source, not emitting any metric
|
|
|
|
|
TagOnly bool `toml:"tag_only"`
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
// Tag Subscription for a gNMI client
|
|
|
|
|
type TagSubscription struct {
|
|
|
|
|
Subscription
|
|
|
|
|
Elements []string
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
func (*GNMI) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Start the http listener service
|
2020-07-01 14:19:16 +08:00
|
|
|
func (c *GNMI) Start(acc telegraf.Accumulator) error {
|
2019-06-05 05:39:46 +08:00
|
|
|
var err error
|
|
|
|
|
var ctx context.Context
|
|
|
|
|
var tlscfg *tls.Config
|
2021-07-28 05:28:26 +08:00
|
|
|
var request *gnmiLib.SubscribeRequest
|
2019-06-05 05:39:46 +08:00
|
|
|
c.acc = acc
|
|
|
|
|
ctx, c.cancel = context.WithCancel(context.Background())
|
2022-07-08 02:50:40 +08:00
|
|
|
|
|
|
|
|
for i := len(c.Subscriptions) - 1; i >= 0; i-- {
|
|
|
|
|
subscription := c.Subscriptions[i]
|
|
|
|
|
// Support legacy TagOnly subscriptions
|
|
|
|
|
if subscription.TagOnly {
|
|
|
|
|
tagSub := convertTagOnlySubscription(subscription)
|
|
|
|
|
c.TagSubscriptions = append(c.TagSubscriptions, tagSub)
|
|
|
|
|
// Remove from the original subscriptions list
|
|
|
|
|
c.Subscriptions = append(c.Subscriptions[:i], c.Subscriptions[i+1:]...)
|
|
|
|
|
c.legacyTags = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err = subscription.buildFullPath(c); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for idx := range c.TagSubscriptions {
|
|
|
|
|
if err = c.TagSubscriptions[idx].buildFullPath(c); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if c.TagSubscriptions[idx].TagOnly != c.TagSubscriptions[0].TagOnly {
|
|
|
|
|
return fmt.Errorf("do not mix legacy tag_only subscriptions and tag subscriptions")
|
|
|
|
|
}
|
|
|
|
|
if len(c.TagSubscriptions[idx].Elements) == 0 {
|
|
|
|
|
return fmt.Errorf("tag_subscription must have at least one element")
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
|
|
|
|
|
// Validate configuration
|
|
|
|
|
if request, err = c.newSubscribeRequest(); err != nil {
|
|
|
|
|
return err
|
2021-04-10 01:15:04 +08:00
|
|
|
} else if time.Duration(c.Redial).Nanoseconds() <= 0 {
|
2019-06-05 05:39:46 +08:00
|
|
|
return fmt.Errorf("redial duration must be positive")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse TLS config
|
|
|
|
|
if c.EnableTLS {
|
|
|
|
|
if tlscfg, err = c.ClientConfig.TLSConfig(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(c.Username) > 0 {
|
|
|
|
|
ctx = metadata.AppendToOutgoingContext(ctx, "username", c.Username, "password", c.Password)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invert explicit alias list and prefill subscription names
|
2022-07-08 02:50:40 +08:00
|
|
|
c.internalAliases = make(map[string]string, len(c.Subscriptions)+len(c.Aliases)+len(c.TagSubscriptions))
|
2019-09-25 02:05:56 +08:00
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
for _, s := range c.Subscriptions {
|
|
|
|
|
if err := s.buildAlias(c.internalAliases); err != nil {
|
2019-09-25 02:05:56 +08:00
|
|
|
return err
|
|
|
|
|
}
|
2022-07-08 02:50:40 +08:00
|
|
|
}
|
|
|
|
|
for _, s := range c.TagSubscriptions {
|
|
|
|
|
if err := s.buildAlias(c.internalAliases); err != nil {
|
2019-06-15 02:29:06 +08:00
|
|
|
return err
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-08 02:50:40 +08:00
|
|
|
|
2021-07-28 05:28:26 +08:00
|
|
|
for alias, encodingPath := range c.Aliases {
|
|
|
|
|
c.internalAliases[encodingPath] = alias
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
2022-11-22 03:59:26 +08:00
|
|
|
c.Log.Debugf("Internal alias mapping: %+v", c.internalAliases)
|
2019-06-05 05:39:46 +08:00
|
|
|
|
|
|
|
|
// Create a goroutine for each device, dial and subscribe
|
|
|
|
|
c.wg.Add(len(c.Addresses))
|
|
|
|
|
for _, addr := range c.Addresses {
|
2022-07-08 02:50:40 +08:00
|
|
|
worker := Worker{address: addr}
|
|
|
|
|
worker.tagStore = &tagNode{}
|
|
|
|
|
go func(worker Worker) {
|
2019-06-05 05:39:46 +08:00
|
|
|
defer c.wg.Done()
|
|
|
|
|
for ctx.Err() == nil {
|
2022-07-08 02:50:40 +08:00
|
|
|
if err := c.subscribeGNMI(ctx, &worker, tlscfg, request); err != nil && ctx.Err() == nil {
|
2019-06-05 05:39:46 +08:00
|
|
|
acc.AddError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
2021-04-10 01:15:04 +08:00
|
|
|
case <-time.After(time.Duration(c.Redial)):
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-08 02:50:40 +08:00
|
|
|
}(worker)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
func (s *Subscription) buildSubscription() (*gnmiLib.Subscription, error) {
|
|
|
|
|
gnmiPath, err := parsePath(s.Origin, s.Path, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
mode, ok := gnmiLib.SubscriptionMode_value[strings.ToUpper(s.SubscriptionMode)]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("invalid subscription mode %s", s.SubscriptionMode)
|
|
|
|
|
}
|
|
|
|
|
return &gnmiLib.Subscription{
|
|
|
|
|
Path: gnmiPath,
|
|
|
|
|
Mode: gnmiLib.SubscriptionMode(mode),
|
|
|
|
|
HeartbeatInterval: uint64(time.Duration(s.HeartbeatInterval).Nanoseconds()),
|
|
|
|
|
SampleInterval: uint64(time.Duration(s.SampleInterval).Nanoseconds()),
|
|
|
|
|
SuppressRedundant: s.SuppressRedundant,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 14:19:16 +08:00
|
|
|
// Create a new gNMI SubscribeRequest
|
2021-07-28 05:28:26 +08:00
|
|
|
func (c *GNMI) newSubscribeRequest() (*gnmiLib.SubscribeRequest, error) {
|
2019-06-05 05:39:46 +08:00
|
|
|
// Create subscription objects
|
2022-07-08 02:50:40 +08:00
|
|
|
var err error
|
|
|
|
|
subscriptions := make([]*gnmiLib.Subscription, len(c.Subscriptions)+len(c.TagSubscriptions))
|
|
|
|
|
for i, subscription := range c.TagSubscriptions {
|
|
|
|
|
if subscriptions[i], err = subscription.buildSubscription(); err != nil {
|
2019-06-05 05:39:46 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-07-08 02:50:40 +08:00
|
|
|
}
|
|
|
|
|
for i, subscription := range c.Subscriptions {
|
|
|
|
|
if subscriptions[i+len(c.TagSubscriptions)], err = subscription.buildSubscription(); err != nil {
|
|
|
|
|
return nil, err
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct subscribe request
|
|
|
|
|
gnmiPath, err := parsePath(c.Origin, c.Prefix, c.Target)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 03:58:43 +08:00
|
|
|
// Do not provide an empty prefix. Required for Huawei NE40 router v8.21
|
|
|
|
|
// (and possibly others). See https://github.com/influxdata/telegraf/issues/12273.
|
|
|
|
|
if gnmiPath.Origin == "" && gnmiPath.Target == "" && len(gnmiPath.Elem) == 0 {
|
|
|
|
|
gnmiPath = nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 00:11:28 +08:00
|
|
|
if c.Encoding != "proto" && c.Encoding != "json" && c.Encoding != "json_ietf" && c.Encoding != "bytes" {
|
2019-06-05 05:39:46 +08:00
|
|
|
return nil, fmt.Errorf("unsupported encoding %s", c.Encoding)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 05:28:26 +08:00
|
|
|
return &gnmiLib.SubscribeRequest{
|
|
|
|
|
Request: &gnmiLib.SubscribeRequest_Subscribe{
|
|
|
|
|
Subscribe: &gnmiLib.SubscriptionList{
|
2019-06-05 05:39:46 +08:00
|
|
|
Prefix: gnmiPath,
|
2021-07-28 05:28:26 +08:00
|
|
|
Mode: gnmiLib.SubscriptionList_STREAM,
|
|
|
|
|
Encoding: gnmiLib.Encoding(gnmiLib.Encoding_value[strings.ToUpper(c.Encoding)]),
|
2019-06-05 05:39:46 +08:00
|
|
|
Subscription: subscriptions,
|
|
|
|
|
UpdatesOnly: c.UpdatesOnly,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SubscribeGNMI and extract telemetry data
|
2022-07-08 02:50:40 +08:00
|
|
|
func (c *GNMI) subscribeGNMI(ctx context.Context, worker *Worker, tlscfg *tls.Config, request *gnmiLib.SubscribeRequest) error {
|
2022-08-02 19:52:17 +08:00
|
|
|
var creds credentials.TransportCredentials
|
2019-06-05 05:39:46 +08:00
|
|
|
if tlscfg != nil {
|
2022-08-02 19:52:17 +08:00
|
|
|
creds = credentials.NewTLS(tlscfg)
|
2019-06-05 05:39:46 +08:00
|
|
|
} else {
|
2022-08-02 19:52:17 +08:00
|
|
|
creds = insecure.NewCredentials()
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
2022-08-02 19:52:17 +08:00
|
|
|
opt := grpc.WithTransportCredentials(creds)
|
2019-06-05 05:39:46 +08:00
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
client, err := grpc.DialContext(ctx, worker.address, opt)
|
2019-06-05 05:39:46 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to dial: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer client.Close()
|
|
|
|
|
|
2021-07-28 05:28:26 +08:00
|
|
|
subscribeClient, err := gnmiLib.NewGNMIClient(client).Subscribe(ctx)
|
2019-06-05 05:39:46 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to setup subscription: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = subscribeClient.Send(request); err != nil {
|
2020-07-22 06:24:45 +08:00
|
|
|
// If io.EOF is returned, the stream may have ended and stream status
|
|
|
|
|
// can be determined by calling Recv.
|
|
|
|
|
if err != io.EOF {
|
|
|
|
|
return fmt.Errorf("failed to send subscription request: %v", err)
|
|
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
c.Log.Debugf("Connection to gNMI device %s established", worker.address)
|
|
|
|
|
defer c.Log.Debugf("Connection to gNMI device %s closed", worker.address)
|
2019-06-05 05:39:46 +08:00
|
|
|
for ctx.Err() == nil {
|
2021-07-28 05:28:26 +08:00
|
|
|
var reply *gnmiLib.SubscribeResponse
|
2019-06-05 05:39:46 +08:00
|
|
|
if reply, err = subscribeClient.Recv(); err != nil {
|
|
|
|
|
if err != io.EOF && ctx.Err() == nil {
|
2020-07-01 14:19:16 +08:00
|
|
|
return fmt.Errorf("aborted gNMI subscription: %v", err)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
c.handleSubscribeResponse(worker, reply)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
func (c *GNMI) handleSubscribeResponse(worker *Worker, reply *gnmiLib.SubscribeResponse) {
|
2022-10-26 18:06:08 +08:00
|
|
|
if response, ok := reply.Response.(*gnmiLib.SubscribeResponse_Update); ok {
|
2022-07-08 02:50:40 +08:00
|
|
|
c.handleSubscribeResponseUpdate(worker, response)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
2020-12-01 01:12:10 +08:00
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
|
2020-12-01 01:12:10 +08:00
|
|
|
// Handle SubscribeResponse_Update message from gNMI and parse contained telemetry data
|
2022-07-08 02:50:40 +08:00
|
|
|
func (c *GNMI) handleSubscribeResponseUpdate(worker *Worker, response *gnmiLib.SubscribeResponse_Update) {
|
2019-06-05 05:39:46 +08:00
|
|
|
var prefix, prefixAliasPath string
|
|
|
|
|
grouper := metric.NewSeriesGrouper()
|
|
|
|
|
timestamp := time.Unix(0, response.Update.Timestamp)
|
|
|
|
|
prefixTags := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
if response.Update.Prefix != nil {
|
2021-04-23 05:08:03 +08:00
|
|
|
var err error
|
2022-07-08 02:50:40 +08:00
|
|
|
if prefix, prefixAliasPath, err = handlePath(response.Update.Prefix, prefixTags, c.internalAliases, ""); err != nil {
|
2021-04-23 05:08:03 +08:00
|
|
|
c.Log.Errorf("handling path %q failed: %v", response.Update.Prefix, err)
|
|
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
2022-11-22 03:59:26 +08:00
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
prefixTags["source"], _, _ = net.SplitHostPort(worker.address)
|
2019-06-05 05:39:46 +08:00
|
|
|
prefixTags["path"] = prefix
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
// Process and remove tag-only updates from the response
|
|
|
|
|
for i := len(response.Update.Update) - 1; i >= 0; i-- {
|
|
|
|
|
update := response.Update.Update[i]
|
|
|
|
|
fullPath := pathWithPrefix(response.Update.Prefix, update.Path)
|
|
|
|
|
for _, tagSub := range c.TagSubscriptions {
|
|
|
|
|
if equalPathNoKeys(fullPath, tagSub.fullPath) {
|
|
|
|
|
worker.storeTags(update, tagSub)
|
|
|
|
|
response.Update.Update = append(response.Update.Update[:i], response.Update.Update[i+1:]...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Parse individual Update message and create measurements
|
|
|
|
|
var name, lastAliasPath string
|
|
|
|
|
for _, update := range response.Update.Update {
|
2022-07-08 02:50:40 +08:00
|
|
|
fullPath := pathWithPrefix(response.Update.Prefix, update.Path)
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Prepare tags from prefix
|
|
|
|
|
tags := make(map[string]string, len(prefixTags))
|
|
|
|
|
for key, val := range prefixTags {
|
|
|
|
|
tags[key] = val
|
|
|
|
|
}
|
|
|
|
|
aliasPath, fields := c.handleTelemetryField(update, tags, prefix)
|
|
|
|
|
|
2022-10-26 18:06:08 +08:00
|
|
|
if tagOnlyTags := worker.checkTags(fullPath); tagOnlyTags != nil {
|
2022-07-08 02:50:40 +08:00
|
|
|
for k, v := range tagOnlyTags {
|
|
|
|
|
if alias, ok := c.internalAliases[k]; ok {
|
|
|
|
|
tags[alias] = fmt.Sprint(v)
|
|
|
|
|
} else {
|
|
|
|
|
tags[k] = fmt.Sprint(v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Inherent valid alias from prefix parsing
|
|
|
|
|
if len(prefixAliasPath) > 0 && len(aliasPath) == 0 {
|
|
|
|
|
aliasPath = prefixAliasPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lookup alias if alias-path has changed
|
|
|
|
|
if aliasPath != lastAliasPath {
|
|
|
|
|
name = prefix
|
2021-07-28 05:28:26 +08:00
|
|
|
if alias, ok := c.internalAliases[aliasPath]; ok {
|
2019-06-05 05:39:46 +08:00
|
|
|
name = alias
|
|
|
|
|
} else {
|
2020-07-01 14:19:16 +08:00
|
|
|
c.Log.Debugf("No measurement alias for gNMI path: %s", name)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 03:59:26 +08:00
|
|
|
// Check for empty names
|
|
|
|
|
if name == "" {
|
|
|
|
|
c.acc.AddError(fmt.Errorf("got empty name for update %+v", update))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Group metrics
|
2020-01-09 02:52:36 +08:00
|
|
|
for k, v := range fields {
|
|
|
|
|
key := k
|
2021-01-14 04:48:21 +08:00
|
|
|
if len(aliasPath) < len(key) && len(aliasPath) != 0 {
|
2020-01-09 02:52:36 +08:00
|
|
|
// This may not be an exact prefix, due to naming style
|
|
|
|
|
// conversion on the key.
|
2019-06-15 02:29:06 +08:00
|
|
|
key = key[len(aliasPath)+1:]
|
2021-01-14 04:48:21 +08:00
|
|
|
} else if len(aliasPath) >= len(key) {
|
2020-01-09 02:52:36 +08:00
|
|
|
// Otherwise use the last path element as the field key.
|
|
|
|
|
key = path.Base(key)
|
|
|
|
|
|
|
|
|
|
// If there are no elements skip the item; this would be an
|
|
|
|
|
// invalid message.
|
|
|
|
|
key = strings.TrimLeft(key, "/.")
|
|
|
|
|
if key == "" {
|
|
|
|
|
c.Log.Errorf("invalid empty path: %q", k)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2019-06-15 02:29:06 +08:00
|
|
|
}
|
2022-11-09 03:04:12 +08:00
|
|
|
grouper.Add(name, tags, timestamp, key, v)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastAliasPath = aliasPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add grouped measurements
|
2021-07-28 05:28:26 +08:00
|
|
|
for _, metricToAdd := range grouper.Metrics() {
|
|
|
|
|
c.acc.AddMetric(metricToAdd)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HandleTelemetryField and add it to a measurement
|
2021-07-28 05:28:26 +08:00
|
|
|
func (c *GNMI) handleTelemetryField(update *gnmiLib.Update, tags map[string]string, prefix string) (string, map[string]interface{}) {
|
2022-07-08 02:50:40 +08:00
|
|
|
gpath, aliasPath, err := handlePath(update.Path, tags, c.internalAliases, prefix)
|
2021-04-23 05:08:03 +08:00
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Errorf("handling path %q failed: %v", update.Path, err)
|
|
|
|
|
}
|
2022-07-08 02:50:40 +08:00
|
|
|
fields, err := gnmiToFields(strings.Replace(gpath, "-", "_", -1), update.Val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Errorf("error parsing update value %q: %v", update.Val, err)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
return aliasPath, fields
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse path to path-buffer and tag-field
|
2022-07-08 02:50:40 +08:00
|
|
|
func handlePath(gnmiPath *gnmiLib.Path, tags map[string]string, aliases map[string]string, prefix string) (pathBuffer string, aliasPath string, err error) {
|
2019-06-05 05:39:46 +08:00
|
|
|
builder := bytes.NewBufferString(prefix)
|
|
|
|
|
|
2022-11-22 03:59:26 +08:00
|
|
|
// Some devices do report the origin in the first path element
|
|
|
|
|
// so try to find out if this is the case.
|
|
|
|
|
if gnmiPath.Origin == "" && len(gnmiPath.Elem) > 0 {
|
|
|
|
|
groups := originPattern.FindStringSubmatch(gnmiPath.Elem[0].Name)
|
|
|
|
|
if len(groups) == 2 {
|
|
|
|
|
gnmiPath.Origin = groups[1]
|
|
|
|
|
gnmiPath.Elem[0].Name = gnmiPath.Elem[0].Name[len(groups[1])+1:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
// Prefix with origin
|
2021-07-28 05:28:26 +08:00
|
|
|
if len(gnmiPath.Origin) > 0 {
|
|
|
|
|
if _, err := builder.WriteString(gnmiPath.Origin); err != nil {
|
2021-04-23 05:08:03 +08:00
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
if _, err := builder.WriteRune(':'); err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse generic keys from prefix
|
2021-07-28 05:28:26 +08:00
|
|
|
for _, elem := range gnmiPath.Elem {
|
2019-09-25 02:05:56 +08:00
|
|
|
if len(elem.Name) > 0 {
|
2021-04-23 05:08:03 +08:00
|
|
|
if _, err := builder.WriteRune('/'); err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
if _, err := builder.WriteString(elem.Name); err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
2019-09-25 02:05:56 +08:00
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
name := builder.String()
|
|
|
|
|
|
2022-07-08 02:50:40 +08:00
|
|
|
if _, exists := aliases[name]; exists {
|
2019-06-05 05:39:46 +08:00
|
|
|
aliasPath = name
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 02:29:06 +08:00
|
|
|
if tags != nil {
|
|
|
|
|
for key, val := range elem.Key {
|
2022-05-11 23:53:34 +08:00
|
|
|
key = strings.ReplaceAll(key, "-", "_")
|
2019-06-15 02:29:06 +08:00
|
|
|
|
|
|
|
|
// Use short-form of key if possible
|
|
|
|
|
if _, exists := tags[key]; exists {
|
|
|
|
|
tags[name+"/"+key] = val
|
|
|
|
|
} else {
|
|
|
|
|
tags[key] = val
|
|
|
|
|
}
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
return builder.String(), aliasPath, nil
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 02:49:36 +08:00
|
|
|
// ParsePath from XPath-like string to gNMI path structure
|
2021-07-28 05:28:26 +08:00
|
|
|
func parsePath(origin string, pathToParse string, target string) (*gnmiLib.Path, error) {
|
2022-04-26 21:38:02 +08:00
|
|
|
gnmiPath, err := xpath.ToGNMIPath(pathToParse)
|
2019-06-05 05:39:46 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-04-26 21:38:02 +08:00
|
|
|
gnmiPath.Origin = origin
|
|
|
|
|
gnmiPath.Target = target
|
|
|
|
|
return gnmiPath, err
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop listener and cleanup
|
2020-07-01 14:19:16 +08:00
|
|
|
func (c *GNMI) Stop() {
|
2019-06-05 05:39:46 +08:00
|
|
|
c.cancel()
|
|
|
|
|
c.wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gather plugin measurements (unused)
|
2020-07-01 14:19:16 +08:00
|
|
|
func (c *GNMI) Gather(_ telegraf.Accumulator) error {
|
2019-06-05 05:39:46 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 14:19:16 +08:00
|
|
|
func New() telegraf.Input {
|
|
|
|
|
return &GNMI{
|
|
|
|
|
Encoding: "proto",
|
2021-04-10 01:15:04 +08:00
|
|
|
Redial: config.Duration(10 * time.Second),
|
2020-07-01 14:19:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 05:39:46 +08:00
|
|
|
func init() {
|
2020-07-01 14:19:16 +08:00
|
|
|
inputs.Add("gnmi", New)
|
|
|
|
|
// Backwards compatible alias:
|
|
|
|
|
inputs.Add("cisco_telemetry_gnmi", New)
|
2019-06-05 05:39:46 +08:00
|
|
|
}
|
2022-07-08 02:50:40 +08:00
|
|
|
|
|
|
|
|
func convertTagOnlySubscription(s Subscription) TagSubscription {
|
|
|
|
|
t := TagSubscription{Subscription: s, Elements: []string{"interface"}}
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// equalPathNoKeys checks if two gNMI paths are equal, without keys
|
|
|
|
|
func equalPathNoKeys(a *gnmiLib.Path, b *gnmiLib.Path) bool {
|
|
|
|
|
if len(a.Elem) != len(b.Elem) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for i := range a.Elem {
|
|
|
|
|
if a.Elem[i].Name != b.Elem[i].Name {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pathKeys(gpath *gnmiLib.Path) []*gnmiLib.PathElem {
|
|
|
|
|
var newPath []*gnmiLib.PathElem
|
|
|
|
|
for _, elem := range gpath.Elem {
|
|
|
|
|
if elem.Key != nil {
|
|
|
|
|
newPath = append(newPath, elem)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pathWithPrefix(prefix *gnmiLib.Path, gpath *gnmiLib.Path) *gnmiLib.Path {
|
|
|
|
|
if prefix == nil {
|
|
|
|
|
return gpath
|
|
|
|
|
}
|
|
|
|
|
fullPath := new(gnmiLib.Path)
|
|
|
|
|
fullPath.Origin = prefix.Origin
|
|
|
|
|
fullPath.Target = prefix.Target
|
|
|
|
|
fullPath.Elem = append(prefix.Elem, gpath.Elem...)
|
|
|
|
|
return fullPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Subscription) buildFullPath(c *GNMI) error {
|
|
|
|
|
var err error
|
|
|
|
|
if s.fullPath, err = xpath.ToGNMIPath(s.Path); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
s.fullPath.Origin = s.Origin
|
|
|
|
|
s.fullPath.Target = c.Target
|
|
|
|
|
if c.Prefix != "" {
|
|
|
|
|
prefix, err := xpath.ToGNMIPath(c.Prefix)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
s.fullPath.Elem = append(prefix.Elem, s.fullPath.Elem...)
|
|
|
|
|
if s.Origin == "" && c.Origin != "" {
|
|
|
|
|
s.fullPath.Origin = c.Origin
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Worker) storeTags(update *gnmiLib.Update, sub TagSubscription) {
|
|
|
|
|
updateKeys := pathKeys(update.Path)
|
|
|
|
|
var foundKey bool
|
|
|
|
|
for _, requiredKey := range sub.Elements {
|
|
|
|
|
foundKey = false
|
|
|
|
|
for _, elem := range updateKeys {
|
|
|
|
|
if elem.Name == requiredKey {
|
|
|
|
|
foundKey = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !foundKey {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// All required keys present for this TagSubscription
|
|
|
|
|
w.tagStore.insert(updateKeys, sub.Name, update.Val)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (node *tagNode) insert(keys []*gnmiLib.PathElem, name string, value *gnmiLib.TypedValue) {
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
|
node.value = value
|
|
|
|
|
node.tagName = name
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var found *tagNode
|
|
|
|
|
key := keys[0]
|
|
|
|
|
keyName := key.Name
|
|
|
|
|
if node.tagStore == nil {
|
|
|
|
|
node.tagStore = make(map[string][]*tagNode)
|
|
|
|
|
}
|
|
|
|
|
if _, ok := node.tagStore[keyName]; !ok {
|
|
|
|
|
node.tagStore[keyName] = make([]*tagNode, 0)
|
|
|
|
|
}
|
|
|
|
|
for _, node := range node.tagStore[keyName] {
|
|
|
|
|
if compareKeys(node.elem.Key, key.Key) {
|
|
|
|
|
found = node
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if found == nil {
|
|
|
|
|
found = &tagNode{elem: keys[0]}
|
|
|
|
|
node.tagStore[keyName] = append(node.tagStore[keyName], found)
|
|
|
|
|
}
|
|
|
|
|
found.insert(keys[1:], name, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (node *tagNode) retrieve(keys []*gnmiLib.PathElem, tagResults *tagResults) {
|
|
|
|
|
if node.value != nil {
|
|
|
|
|
tagResults.names = append(tagResults.names, node.tagName)
|
|
|
|
|
tagResults.values = append(tagResults.values, node.value)
|
|
|
|
|
}
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
if elems, ok := node.tagStore[key.Name]; ok {
|
|
|
|
|
for _, node := range elems {
|
|
|
|
|
if compareKeys(node.elem.Key, key.Key) {
|
|
|
|
|
node.retrieve(keys, tagResults)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 18:06:08 +08:00
|
|
|
func (w *Worker) checkTags(fullPath *gnmiLib.Path) map[string]interface{} {
|
2022-07-08 02:50:40 +08:00
|
|
|
results := &tagResults{}
|
|
|
|
|
w.tagStore.retrieve(pathKeys(fullPath), results)
|
|
|
|
|
tags := make(map[string]interface{})
|
|
|
|
|
for idx := range results.names {
|
|
|
|
|
vals, _ := gnmiToFields(results.names[idx], results.values[idx])
|
|
|
|
|
for k, v := range vals {
|
|
|
|
|
tags[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tags
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Subscription) buildAlias(aliases map[string]string) error {
|
|
|
|
|
var err error
|
|
|
|
|
var gnmiLongPath, gnmiShortPath *gnmiLib.Path
|
|
|
|
|
|
|
|
|
|
// Build the subscription path without keys
|
|
|
|
|
if gnmiLongPath, err = parsePath(s.Origin, s.Path, ""); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if gnmiShortPath, err = parsePath("", s.Path, ""); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
longPath, _, err := handlePath(gnmiLongPath, nil, nil, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("handling long-path failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
shortPath, _, err := handlePath(gnmiShortPath, nil, nil, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("handling short-path failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the user didn't provide a measurement name, use last path element
|
|
|
|
|
name := s.Name
|
|
|
|
|
if len(name) == 0 {
|
|
|
|
|
name = path.Base(shortPath)
|
|
|
|
|
}
|
|
|
|
|
if len(name) > 0 {
|
|
|
|
|
aliases[longPath] = name
|
|
|
|
|
aliases[shortPath] = name
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func gnmiToFields(name string, updateVal *gnmiLib.TypedValue) (map[string]interface{}, error) {
|
|
|
|
|
var value interface{}
|
|
|
|
|
var jsondata []byte
|
|
|
|
|
|
|
|
|
|
// Make sure a value is actually set
|
|
|
|
|
if updateVal == nil || updateVal.Value == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch val := updateVal.Value.(type) {
|
|
|
|
|
case *gnmiLib.TypedValue_AsciiVal:
|
|
|
|
|
value = val.AsciiVal
|
|
|
|
|
case *gnmiLib.TypedValue_BoolVal:
|
|
|
|
|
value = val.BoolVal
|
|
|
|
|
case *gnmiLib.TypedValue_BytesVal:
|
|
|
|
|
value = val.BytesVal
|
2022-11-03 03:20:24 +08:00
|
|
|
case *gnmiLib.TypedValue_DoubleVal:
|
|
|
|
|
value = val.DoubleVal
|
2022-07-08 02:50:40 +08:00
|
|
|
case *gnmiLib.TypedValue_DecimalVal:
|
2022-11-03 21:41:20 +08:00
|
|
|
//nolint:staticcheck // to maintain backward compatibility with older gnmi specs
|
2022-07-08 02:50:40 +08:00
|
|
|
value = float64(val.DecimalVal.Digits) / math.Pow(10, float64(val.DecimalVal.Precision))
|
|
|
|
|
case *gnmiLib.TypedValue_FloatVal:
|
2022-11-03 21:41:20 +08:00
|
|
|
//nolint:staticcheck // to maintain backward compatibility with older gnmi specs
|
2022-07-08 02:50:40 +08:00
|
|
|
value = val.FloatVal
|
|
|
|
|
case *gnmiLib.TypedValue_IntVal:
|
|
|
|
|
value = val.IntVal
|
|
|
|
|
case *gnmiLib.TypedValue_StringVal:
|
|
|
|
|
value = val.StringVal
|
|
|
|
|
case *gnmiLib.TypedValue_UintVal:
|
|
|
|
|
value = val.UintVal
|
|
|
|
|
case *gnmiLib.TypedValue_JsonIetfVal:
|
|
|
|
|
jsondata = val.JsonIetfVal
|
|
|
|
|
case *gnmiLib.TypedValue_JsonVal:
|
|
|
|
|
jsondata = val.JsonVal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
if value != nil {
|
|
|
|
|
fields[name] = value
|
|
|
|
|
} else if jsondata != nil {
|
|
|
|
|
if err := json.Unmarshal(jsondata, &value); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to parse JSON value: %v", err)
|
|
|
|
|
}
|
|
|
|
|
flattener := jsonparser.JSONFlattener{Fields: fields}
|
|
|
|
|
if err := flattener.FullFlattenJSON(name, value, true, true); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to flatten JSON: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fields, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func compareKeys(a map[string]string, b map[string]string) bool {
|
|
|
|
|
if len(a) != len(b) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for k, v := range a {
|
|
|
|
|
if _, ok := b[k]; !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if b[k] != v {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|