forked from nileshsimaria/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe_gnmi.go
557 lines (477 loc) · 16.2 KB
/
subscribe_gnmi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package main
import (
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"syscall"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
google_protobuf "github.com/golang/protobuf/ptypes/any"
"github.com/influxdata/influxdb/client/v2"
"github.com/nileshsimaria/jtimon/dialout"
gnmi "github.com/nileshsimaria/jtimon/gnmi/gnmi"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Only for unit test and coverage purposes
var gGnmiUnitTestCoverage bool
// Convert data to float64, Prometheus sampling is only in float64
func convToFloatForPrometheus(v interface{}) (float64, error) {
var fieldValue float64
switch v.(type) {
case int64:
fieldValue = float64(v.(int64))
case float64:
fieldValue = v.(float64)
case bool:
if v.(bool) == true {
fieldValue = 1
} else {
fieldValue = 0
}
case string:
floatVal, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
errmsg := fmt.Sprintf("Unable to convert string val \"%v\"", v)
return 0, errors.New(errmsg)
}
fieldValue = floatVal
case *google_protobuf.Any:
case []interface{}:
case []byte:
default:
errMsg := fmt.Sprintf("Unsupported type %T", v)
return 0, errors.New(errMsg)
}
return fieldValue, nil
}
/*
* Publish metrics to Prometheus. Below is the terminology:
* 1. Field - Metric
* 2. Tags - Labels
*/
func publishToPrometheus(jctx *JCtx, parseOutput *gnmiParseOutputT) {
var (
promKvpairs = map[string]string{}
alias = jctx.alias
)
for k, v := range parseOutput.kvpairs {
promKvpairs[promName(getAlias(alias, k))] = v
}
for p, v := range parseOutput.xpaths {
splits := strings.Split(p, gXPathTokenPathSep)
if strings.HasPrefix(splits[len(splits)-1], gGnmiJuniperInternalFieldsPrefix) {
continue
}
floatVal, err := convToFloatForPrometheus(v)
if err != nil {
jLog(jctx, fmt.Sprintf("Value conversion failed for %v, error: %v", p, err))
continue
}
metric := &jtimonMetric{
metricName: promName(getAlias(alias, p)),
metricExpiration: time.Now(),
metricValue: floatVal,
metricLabels: promKvpairs,
}
metric.mapKey = getMapKey(metric)
if *print || IsVerboseLogging(jctx) {
jLog(jctx, fmt.Sprintf("metricName: %v, metricValue: %v, metricLabels: %v, mapKey: %v \n", metric.metricName, metric.metricValue, metric.metricLabels, metric.mapKey))
}
if !gGnmiUnitTestCoverage {
exporter.ch <- metric
}
}
return
}
/*
* Publish parsed output to Influx. Make sure there are only inegers,
* floats and strings. Influx Line Protocol doesn't support other types
*/
func publishToInflux(jctx *JCtx, mName string, prefixPath string, kvpairs map[string]string, xpaths map[string]interface{}) error {
jLog(jctx, fmt.Sprintf("jctx.influxCtx.influxClient: %v.. ", jctx.influxCtx.influxClient))
if !gGnmiUnitTestCoverage && jctx.influxCtx.influxClient == nil {
return nil
}
pt, err := client.NewPoint(mName, kvpairs, xpaths, time.Now())
if err != nil {
msg := fmt.Sprintf("New point creation failed for (key: %v, xpaths: %v): %v", kvpairs, xpaths, err)
jLog(jctx, msg)
return errors.New(msg)
}
jLog(jctx, fmt.Sprintf("jctx.config.Influx.WritePerMeasurement: %v.. ", jctx.config.Influx.WritePerMeasurement))
if jctx.config.Influx.WritePerMeasurement {
if *print || IsVerboseLogging(jctx) {
msg := fmt.Sprintf("New point (per measurement): %v", pt.String())
jLog(jctx, msg)
}
if !gGnmiUnitTestCoverage {
jctx.influxCtx.batchWMCh <- &batchWMData{
measurement: mName,
points: []*client.Point{pt},
}
}
} else {
if *print || IsVerboseLogging(jctx) {
msg := fmt.Sprintf("New point: %v", pt.String())
jLog(jctx, msg)
}
if !gGnmiUnitTestCoverage {
jctx.influxCtx.batchWCh <- []*client.Point{pt}
}
}
return nil
}
/*
* Extract the following from gNMI response and already parsed output:
* 1. Juniper telemetry header, if it is a Juniper packet
* 2. Value for the tag "sensor"
* 3. Measuremnet name
* 4. Timestamps:
* a) Producer timestamp
* b) Export timestamp - Only available in Juniper packet, the time at which device(?? actually NA) published
*/
func gnmiParseHeader(rsp *gnmi.SubscribeResponse, parseOutput *gnmiParseOutputT) (*gnmiParseOutputT, error) {
var (
juniperHdrDetails *juniperGnmiHeaderDetails
ok bool
err error
verboseSensorDetails, mName string
)
prefixPath := parseOutput.prefixPath
jXpaths := parseOutput.jXpaths
xpathVal := parseOutput.xpaths
// Identify the measurement name
// Try using the proper gnmi_ext.proto's path in gnmi.proto, now it is manually edited
juniperHdrDetails, ok, err = formJuniperTelemetryHdr(jXpaths, rsp.GetExtension())
if !ok {
// Not a juniper packet, take prefix as the path subscribed
ps := rsp.GetUpdate().GetTimestamp()
// Specifically added for Cisco, the device sends spurious messages with timestamp 0
if ps == 0 {
errMsg := fmt.Sprintf("Invalid message, producer timestamp is 0")
return nil, errors.New(errMsg)
}
parseOutput.sensorVal = prefixPath
parseOutput.mName = prefixPath + gXPathTokenPathSep // To be compatible with that of OC
tsInMillisecs := (ps / gGnmiFreqToMilli)
xpathVal[prefixPath+gXPathTokenPathSep+gGnmiJtimonProducerTsName] = tsInMillisecs
xpathVal[gGnmiJtimonDeviceTsName] = tsInMillisecs
return parseOutput, nil
}
if err != nil {
return parseOutput, err
}
if juniperHdrDetails.hdr != nil {
var hdr = juniperHdrDetails.hdr
verboseSensorDetails = hdr.GetPath()
splits := strings.Split(verboseSensorDetails, gGnmiVerboseSensorDetailsDelim)
mName = splits[2] // Denotes subscribed path
if jXpaths.publishTsXpath != "" {
xpathVal[prefixPath+gXPathTokenPathSep+gGnmiJtimonExportTsName] = jXpaths.xPaths[jXpaths.publishTsXpath]
}
tsInMillisecs := (rsp.GetUpdate().GetTimestamp() / gGnmiFreqToMilli)
xpathVal[prefixPath+gXPathTokenPathSep+gGnmiJtimonProducerTsName] = tsInMillisecs
xpathVal[gGnmiJtimonDeviceTsName] = tsInMillisecs
} else {
var hdr = juniperHdrDetails.hdrExt
verboseSensorDetails = hdr.GetSensorName() + gGnmiVerboseSensorDetailsDelim +
hdr.GetStreamedPath() + gGnmiVerboseSensorDetailsDelim +
hdr.GetSubscribedPath() + gGnmiVerboseSensorDetailsDelim +
hdr.GetComponent()
mName = hdr.GetSubscribedPath()
xpathVal[prefixPath+gXPathTokenPathSep+gGnmiJtimonExportTsName] = hdr.GetExportTimestamp()
tsInMillisecs := (rsp.GetUpdate().GetTimestamp() / gGnmiFreqToMilli)
xpathVal[prefixPath+gXPathTokenPathSep+gGnmiJtimonProducerTsName] = tsInMillisecs
xpathVal[gGnmiJtimonDeviceTsName] = tsInMillisecs
}
parseOutput.jHeader = juniperHdrDetails
parseOutput.sensorVal = verboseSensorDetails
parseOutput.mName = mName
return parseOutput, nil
}
/*
* Extract the following from gNMI response and already parsed output:
* 1. Tags aka kvpairs
* 2. Fields aka xpaths
* 3. Juniper telemery header, "sensor" value and measurement name
*/
func gnmiParseNotification(parseOrigin bool, rsp *gnmi.SubscribeResponse, parseOutput *gnmiParseOutputT, enableUint bool) (*gnmiParseOutputT, error) {
var (
errMsg string
err error
)
notif := rsp.GetUpdate()
if notif == nil {
errMsg = fmt.Sprintf("Not any of error/sync/update !!")
return parseOutput, errors.New(errMsg)
}
if len(notif.GetUpdate()) != 0 {
parseOutput, err = gnmiParseUpdates(parseOrigin, notif.GetPrefix(), notif.GetUpdate(), parseOutput, enableUint)
if err != nil {
errMsg = fmt.Sprintf("gnmiParseUpdates failed: %v", err)
return parseOutput, errors.New(errMsg)
}
}
if len(notif.GetDelete()) != 0 {
parseOutput, err = gnmiParseDeletes(parseOrigin, notif.GetPrefix(), notif.GetDelete(), parseOutput)
if err != nil {
return parseOutput, err
}
}
/*
* Update in-kvs immediately after we form xpaths from the rsp because
* down the line xpaths will get updated with additional jtimon specific
* fields to be written to influx
*/
parseOutput.inKvs += uint64(len(parseOutput.xpaths))
if parseOutput.jXpaths != nil {
parseOutput.inKvs += uint64(len(parseOutput.jXpaths.xPaths))
}
parseOutput, err = gnmiParseHeader(rsp, parseOutput)
if err != nil {
errMsg = fmt.Sprintf("gnmiParseHeader failed: %v", err)
return parseOutput, errors.New(errMsg)
}
return parseOutput, nil
}
/*
* Parse gNMI response and publish to Influx and Prometheus
*/
func gnmiHandleResponse(jctx *JCtx, rsp *gnmi.SubscribeResponse) error {
var (
tmpParseOp = gnmiParseOutputT{kvpairs: map[string]string{}, xpaths: map[string]interface{}{}}
parseOutput = &tmpParseOp
err error
hostname = jctx.config.Host + ":" + strconv.Itoa(jctx.config.Port)
)
// Update packet stats
updateStats(jctx, nil, true)
if syncRsp := rsp.GetSyncResponse(); syncRsp {
jLog(jctx, fmt.Sprintf("gNMI host: %v, received sync response", hostname))
parseOutput.syncRsp = true
jctx.receivedSyncRsp = true
return nil
}
/*
* Extract prefix, tags, values and juniper speecific header info if present
*/
parseOutput, err = gnmiParseNotification(!jctx.config.Vendor.RemoveNS, rsp, parseOutput, jctx.config.EnableUintSupport)
if err != nil {
jLog(jctx, fmt.Sprintf("gNMI host: %v, parsing notification failed: %v", hostname, err.Error()))
return err
}
// Update kv stats
updateStatsKV(jctx, true, parseOutput.inKvs)
// Ignore all packets till sync response is received.
if !jctx.config.EOS {
if !jctx.receivedSyncRsp {
if parseOutput.jHeader != nil {
// For juniper packets, ignore only the packets which are numbered in initial sync sequence range
if parseOutput.jHeader.hdr != nil {
if parseOutput.jHeader.hdr.GetSequenceNumber() >= gGnmiJuniperIsyncSeqNumBegin &&
parseOutput.jHeader.hdr.GetSequenceNumber() <= gGnmiJuniperIsyncSeqNumEnd {
errMsg := fmt.Sprintf("%s. Dropping initial sync packet, seq num: %v", gGnmiJtimonIgnoreErrorSubstr, parseOutput.jHeader.hdr.GetSequenceNumber())
return errors.New(errMsg)
}
}
if parseOutput.jHeader.hdrExt != nil {
if parseOutput.jHeader.hdrExt.GetSequenceNumber() >= gGnmiJuniperIsyncSeqNumBegin &&
parseOutput.jHeader.hdrExt.GetSequenceNumber() <= gGnmiJuniperIsyncSeqNumEnd {
errMsg := fmt.Sprintf("%s. Dropping initial sync packet, seq num: %v", gGnmiJtimonIgnoreErrorSubstr, parseOutput.jHeader.hdrExt.GetSequenceNumber())
return errors.New(errMsg)
}
}
} else {
errMsg := fmt.Sprintf("%s. Dropping initial sync packet", gGnmiJtimonIgnoreErrorSubstr)
return errors.New(errMsg)
}
}
}
if parseOutput.mName == "" {
jLog(jctx, fmt.Sprintf("gNMI host: %v, measurement name extraction failed", hostname))
return errors.New("Measurement name extraction failed")
}
parseOutput.kvpairs["device"] = jctx.config.Host
parseOutput.kvpairs["sensor"] = parseOutput.sensorVal
parseOutput.xpaths["vendor"] = "gnmi"
if *prom {
if *noppgoroutines {
publishToPrometheus(jctx, parseOutput)
} else {
go publishToPrometheus(jctx, parseOutput)
}
}
if *print || IsVerboseLogging(jctx) {
var (
jxpaths map[string]interface{}
jGnmiHdr string
)
if parseOutput.jXpaths != nil {
jxpaths = parseOutput.jXpaths.xPaths
}
if parseOutput.jHeader != nil {
if parseOutput.jHeader.hdr != nil {
jGnmiHdr = "updates header{" + parseOutput.jHeader.hdr.String() + "}"
} else {
jGnmiHdr = "extension header{" + parseOutput.jHeader.hdrExt.String() + "}"
}
}
jLog(jctx, fmt.Sprintf("prefix: %v, kvpairs: %v, xpathVal: %v, juniperXpathVal: %v, juniperhdr: %v, measurement: %v, rsp: %v\n\n",
parseOutput.prefixPath, parseOutput.kvpairs, parseOutput.xpaths, jxpaths, jGnmiHdr, parseOutput.mName, *rsp))
}
jLog(jctx, fmt.Sprintf("publishToInflux.. "))
err = publishToInflux(jctx, parseOutput.mName, parseOutput.prefixPath, parseOutput.kvpairs, parseOutput.xpaths)
if err != nil {
jLog(jctx, fmt.Sprintf("Publish to Influx fails: %v\n\n", parseOutput.mName))
return err
}
return err
}
// Convert xpaths config to gNMI subscription
func xPathsTognmiSubscription(pathsCfg []PathsConfig, dialOutpathsCfg []*dialout.PathsConfig) ([]*gnmi.Subscription, error) {
var subs []*gnmi.Subscription
if dialOutpathsCfg == nil {
for _, p := range pathsCfg {
gp, err := xPathTognmiPath(p.Path)
if err != nil {
return nil, err
}
mode := gnmiMode(p.Mode)
mode, freq := gnmiFreq(mode, p.Freq)
gp.Origin = p.Origin
subs = append(subs, &gnmi.Subscription{Path: gp, Mode: mode, SampleInterval: freq})
}
} else {
for _, p := range dialOutpathsCfg {
gp, err := xPathTognmiPath(p.Path)
if err != nil {
return nil, err
}
mode := gnmiMode(p.Mode)
mode, freq := gnmiFreq(mode, p.Frequency)
subs = append(subs, &gnmi.Subscription{Path: gp, Mode: mode, SampleInterval: freq})
}
}
return subs, nil
}
// subscribe routine constructs the subscription paths and calls
// the function to start the streaming connection.
//
// In case of SIGHUP, the paths are formed again and streaming
// is restarted.
func subscribegNMI(conn *grpc.ClientConn, jctx *JCtx) SubErrorCode {
var (
subs gnmi.SubscriptionList
sub = gnmi.SubscribeRequest_Subscribe{Subscribe: &subs}
req = gnmi.SubscribeRequest{Request: &sub}
err error
hostname = jctx.config.Host + ":" + strconv.Itoa(jctx.config.Port)
ctx context.Context
)
// 1. Form request
// Support only STREAM
subs.Mode = gnmi.SubscriptionList_STREAM
// PROTO encoding
if jctx.config.Vendor.Gnmi != nil {
switch jctx.config.Vendor.Gnmi.Encoding {
case "json":
subs.Encoding = gnmi.Encoding_JSON
case "json_ietf":
subs.Encoding = gnmi.Encoding_JSON_IETF
default:
subs.Encoding = gnmi.Encoding_PROTO
}
}
// Form subscription from xpaths config
subs.Subscription, err = xPathsTognmiSubscription(jctx.config.Paths, nil)
if err != nil {
jLog(jctx, fmt.Sprintf("gNMI host: %v, Invalid path: %v", hostname, err))
// To make worker absorb any further config changes
return SubRcConnRetry
}
// 2. Subscribe
if jctx.config.User != "" && jctx.config.Password != "" {
md := metadata.New(map[string]string{"username": jctx.config.User, "password": jctx.config.Password})
ctx = metadata.NewOutgoingContext(context.Background(), md)
} else {
ctx = context.Background()
}
gNMISubHandle, err := gnmi.NewGNMIClient(conn).Subscribe(ctx)
if err != nil {
jLog(jctx, fmt.Sprintf("gNMI host: %v, subscribe handle creation failed, err: %v", hostname, err))
return SubRcConnRetry
}
err = gNMISubHandle.Send(&req)
if err != nil {
jLog(jctx, fmt.Sprintf("gNMI host: %v, send request failed: %v", hostname, err))
return SubRcConnRetry
}
datach := make(chan SubErrorCode)
// 3. Receive rsp
go func() {
var (
rsp *gnmi.SubscribeResponse
)
jLog(jctx, fmt.Sprintf("gNMI host: %v, receiving data..", hostname))
for {
rsp, err = gNMISubHandle.Recv()
if err == io.EOF {
printSummary(jctx)
jLog(jctx, fmt.Sprintf("gNMI host: %v, received eof", hostname))
datach <- SubRcConnRetry
return
}
if err != nil {
jLog(jctx, fmt.Sprintf("gNMI host: %v, receive response failed: %v", hostname, err))
sc, _ := status.FromError(err)
/*
* Unavailable is just a cover-up for JUNOS, ideally the device is expected to return:
* 1. Unimplemented if RPC is not available yet
* 2. InvalidArgument is RPC is not able to honour the input
*/
if sc.Code() == codes.Unimplemented || sc.Code() == codes.InvalidArgument || sc.Code() == codes.Unavailable {
datach <- SubRcRPCFailedNoRetry
return
}
datach <- SubRcConnRetry
return
}
if *noppgoroutines {
err = gnmiHandleResponse(jctx, rsp)
if err != nil && strings.Contains(err.Error(), gGnmiJtimonIgnoreErrorSubstr) {
jLog(jctx, fmt.Sprintf("gNMI host: %v, parsing response failed: %v", hostname, err))
continue
}
} else {
go func() {
err = gnmiHandleResponse(jctx, rsp)
if err != nil && strings.Contains(err.Error(), gGnmiJtimonIgnoreErrorSubstr) {
jLog(jctx, fmt.Sprintf("gNMI host: %v, parsing response failed: %v", hostname, err))
}
}()
}
}
}()
for {
select {
case s := <-jctx.control:
switch s {
case syscall.SIGHUP:
// config has been updated restart the streaming
return SubRcSighupRestart
case os.Interrupt:
// we are done
return SubRcSighupNoRestart
}
case subCode := <-datach:
// return the subcode, proper action will be taken by caller
return subCode
}
}
}