forked from prometheus/snmp_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.go
1228 lines (1154 loc) · 33.5 KB
/
tree.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The Prometheus Authors
// Portions Copyright 2022 Jens Elkner ([email protected])
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/jelmd/snmp-export/config"
)
// These types have one following the other.
// We need to check indexes and sequences have them
// in the right order, so the exporter can handle them.
var combinedTypes = map[string]string{
"InetAddress": "InetAddressType",
"InetAddressMissingSize": "InetAddressType",
"LldpPortId": "LldpPortIdSubtype",
}
// Just to avoid endles loops if someone gets too creative wrt. brace expressions
const maxBraceExpansionRounds = 1000
const maxBraceNestingDepth = 1000
// Helper to walk MIB nodes.
func walkNode(n *Node, f func(n *Node)) {
f(n)
for _, c := range n.Children {
walkNode(c, f)
}
}
// Transform the tree.
func prepareTree(nodes *Node, logger log.Logger) map[string]*Node {
// Build a map from names and oids to nodes.
nameToNode := map[string]*Node{}
walkNode(nodes, func(n *Node) {
nameToNode[n.Oid] = n
nameToNode[n.Label] = n
})
// Trim down description to first sentence, removing extra whitespace.
walkNode(nodes, func(n *Node) {
s := strings.Join(strings.Fields(n.Description), " ")
n.Description = strings.Split(s, ". ")[0]
})
// Fix indexes to "INTEGER" rather than an object name.
// Example: snSlotsEntry in LANOPTICS-HUB-MIB.
walkNode(nodes, func(n *Node) {
indexes := []string{}
for _, i := range n.Indexes {
if i == "INTEGER" {
// Use the TableEntry name.
indexes = append(indexes, n.Label)
} else {
indexes = append(indexes, i)
}
}
n.Indexes = indexes
})
// Copy over indexes based on augments.
walkNode(nodes, func(n *Node) {
if n.Augments == "" {
return
}
augmented, ok := nameToNode[n.Augments]
if !ok {
level.Warn(logger).Log("msg", "Can't find augmenting node", "augments", n.Augments, "node", n.Label)
return
}
for _, c := range n.Children {
c.Indexes = augmented.Indexes
c.ImpliedIndex = augmented.ImpliedIndex
}
n.Indexes = augmented.Indexes
n.ImpliedIndex = augmented.ImpliedIndex
})
// Copy indexes from table entries down to the entries.
walkNode(nodes, func(n *Node) {
if len(n.Indexes) != 0 {
for _, c := range n.Children {
c.Indexes = n.Indexes
c.ImpliedIndex = n.ImpliedIndex
}
}
})
// Include both ASCII and UTF-8 in DisplayString, even though DisplayString
// is technically only ASCII.
displayStringRe := regexp.MustCompile(`^\d+[at]$`)
// Apply various tweaks to the types.
walkNode(nodes, func(n *Node) {
// Set type on MAC addresses and strings.
// RFC 2579
switch n.Hint {
case "1x:":
n.Type = "PhysAddress48"
}
if displayStringRe.MatchString(n.Hint) {
n.Type = "DisplayString"
}
// Some MIBs refer to RFC1213 for this, which is too
// old to have the right hint set.
if n.TextualConvention == "DisplayString" {
n.Type = "DisplayString"
}
if n.TextualConvention == "PhysAddress" {
n.Type = "PhysAddress48"
}
// Promote Opaque Float/Double textual convention to type.
if n.TextualConvention == "Float" || n.TextualConvention == "Double" {
n.Type = n.TextualConvention
}
// Convert RFC 2579 DateAndTime textual convention to type.
if n.TextualConvention == "DateAndTime" {
n.Type = "DateAndTime"
}
// Convert RFC 4001 InetAddress types textual convention to type.
if n.TextualConvention == "InetAddressIPv4" || n.TextualConvention == "InetAddressIPv6" || n.TextualConvention == "InetAddress" {
n.Type = n.TextualConvention
}
// Convert LLDP-MIB LldpPortId type textual convention to type.
if n.TextualConvention == "LldpPortId" {
n.Type = n.TextualConvention
}
})
return nameToNode
}
func metricType(t string) (string, bool) {
if _, ok := combinedTypes[t]; ok {
return t, true
}
switch t {
case "gauge", "INTEGER", "GAUGE", "TIMETICKS", "UINTEGER", "UNSIGNED32", "INTEGER32":
return "gauge", true
case "counter", "COUNTER", "COUNTER64":
return "counter", true
case "uptime", "UPTIME", "UPTIME64":
return "uptime", true
case "OctetString", "OCTETSTR", "OBJID":
return "OctetString", true
case "BITSTRING":
return "Bits", true
case "InetAddressIPv4", "IpAddr", "IPADDR", "NETADDR":
return "InetAddressIPv4", true
case "PhysAddress48", "DisplayString", "Float", "Double", "InetAddressIPv6":
return t, true
case "DateAndTime":
return t, true
case "EnumAsInfo", "EnumAsStateSet":
return t, true
default:
// Unsupported type.
return "", false
}
}
func metricAccess(a string) bool {
switch a {
case "ACCESS_READONLY", "ACCESS_READWRITE", "ACCESS_CREATE", "ACCESS_NOACCESS":
return true
default:
// the others are inaccessible metrics.
return false
}
}
// Reduce a set of overlapping OID subtrees.
func minimizeOids(oids []string) []string {
sort.Strings(oids)
prevOid := ""
minimized := []string{}
for _, oid := range oids {
if !strings.HasPrefix(oid+".", prevOid) || prevOid == "" {
minimized = append(minimized, oid)
prevOid = oid + "."
}
}
return minimized
}
// Search node tree for the longest OID match.
func searchNodeTree(oid string, node *Node) *Node {
if node == nil || !strings.HasPrefix(oid+".", node.Oid+".") {
return nil
}
for _, child := range node.Children {
match := searchNodeTree(oid, child)
if match != nil {
return match
}
}
return node
}
type oidMetricType uint8
const (
oidNotFound oidMetricType = iota
oidScalar
oidInstance
oidSubtree
)
// Find node in SNMP MIB tree that represents the metric.
func getMetricNode(oid string, node *Node, nameToNode map[string]*Node) (*Node, oidMetricType) {
// Check if is a known OID/name.
n, ok := nameToNode[oid]
if ok {
// Known node, check if OID is a valid metric or a subtree.
_, ok = metricType(n.Type)
if ok && metricAccess(n.Access) && len(n.Indexes) == 0 {
return n, oidScalar
} else {
return n, oidSubtree
}
}
// Unknown OID/name, search Node tree for longest match.
n = searchNodeTree(oid, node)
if n == nil {
return nil, oidNotFound
}
// Table instances must be a valid metric node and have an index.
_, ok = metricType(n.Type)
ok = ok && metricAccess(n.Access)
if !ok || len(n.Indexes) == 0 {
return nil, oidNotFound
}
return n, oidInstance
}
func expandCfgItem(s string, logger log.Logger) []string {
if len(s) < 1 {
return []string{}
}
a := strings.Split(s, "¦")
if strings.IndexByte(s, '{') == -1 {
return a
}
res := []string{}
for _, o := range a {
if len(o) == 0 {
continue
}
l, _ := expandBraces(o, 0, logger);
res = append(res, l...)
}
return res
}
func expandList(prefix string, list []string, suffix string, brace bool, depth int, logger log.Logger) string {
// e.g. ucb/ ex,edit ,lib/{ex,how_ex} => ucb/ex,ucb/edit,lib/{ex,how_ex}
if len(list) == 0 {
return ""
}
var sb strings.Builder
pprefix := ""
iprefix := "," + prefix
isuffix := suffix
psuffix := ""
i := -1
if depth > 0 {
i = strings.LastIndexByte(prefix, ',')
}
if i != -1 {
pprefix = prefix[:i+1]
iprefix = prefix[i:]
}
// i = strings.IndexByte(suffix, ',') doesn't always work
brace_count := 0
i = -1
if depth > 0 {
for k := 0; k < len(suffix); k++ {
if suffix[k] == ',' && brace_count == 0 {
i = k
break;
}
if suffix[k] == '{' && (k == 0 || suffix[k-1] != '\\') {
brace_count++
} else if suffix[k] == '}' && (k == 0 || suffix[k-1] != '\\') && brace_count != 0 {
brace_count--
}
}
}
if i != -1 {
isuffix = suffix[:i]
psuffix = suffix[i:]
}
level.Debug(logger).Log("ListExpansion",
prefix + "|" + strings.Join(list, "¦") + "|" + suffix,
"pprefix", pprefix, "iprefix", iprefix, "isuffix", isuffix, "psuffix", psuffix, "brace", brace)
for _,s := range list {
for _,t := range strings.Split(s, ",") {
sb.WriteString(iprefix)
sb.WriteString(t)
sb.WriteString(isuffix)
}
}
t := ""
if brace {
t = pprefix + "{" + sb.String()[1:] + "}" + psuffix
} else {
t = pprefix + sb.String()[1:] + psuffix
}
level.Debug(logger).Log("ListExpansion_result", t)
return t
}
func expandBraces(s string, depth int, logger log.Logger) ([]string, bool) {
/*
If l1,l2 are either all lower case or all upper case letters in C locale,
n1,n2,n3 signed numbers, and
fmt a string specified as in fmt.Printf we support the following
brace expansions similar to ksh93:
(1) `{s[,s1]...}`
(2) `{l1..l2[..n3][%fmt]}`
(3) `{n1..n2[..n3][%fmt]}`
The curly braces, dots and percent sign are literals, the brackets mark an
optional part of the brace expression - need to be ommitted.
In the first form the function iterates over the comma separated list of
strings and generates for each member a new string by replacing the brace
expression with the member.
E.g. `foo{bar,sel,l}` becomes `foobar|foosel|fool`.
In the second and third form the generator iterates from l1 through l2
or n1 through n2 using the given step width n3. If n3 is not given, it
gets set to 1 or -1 depending on the first and second argument. If %fmt
is given, it will be used to create the string from the generated character
or number. Otherwise `%c` (2nd form) or `%d`(3rd form) will be used.
Finally a new list of strings gets generated, where the brace expression
gets replaced by the members of the one-letter list one-by-one.
E.g. `chapter{A..F}.1` becomes
`chapterA.1|chapterB.1|chapterC.1|chapterD.1|chapterE.1|chapterF.1`,
and `{a,z}{1..5..3%02d}{b..c}x` expands to 2x2x2 == 8 strings:
`a01bx|a01cx|a04bx|a04cx|z01bx|z01cx|z04bx|z04cx`.
One may escape curly braces with a backslash(`\`), but since they are not
allowed in metric names, it doesn't make much sense for the generator case.
Any brace expression which cannot be parsed or uses invalid arguments gets
handled as literal without the enclosing curly braces. Note that in the
2nd form only ASCII letters in the range of `a-z` and `A-Z` are accepted,
only.
*/
if len(s) == 0 {
return []string{ s } , false
}
if depth >= maxBraceNestingDepth {
level.Warn(logger).Log("msg", fmt.Sprintf("Brace expansion aborted because of too many nested brace expressions (%d). Check '%s' for brace insertion.", depth, s))
return []string{ s } , false
}
level.Debug(logger).Log("Expand_Brace", s, "depth", depth)
src := []string{ s }
src_modified := []bool{ true }
res := []string{}
res_modified := []bool{}
modified := true
count := 0
for modified {
if count >= maxBraceExpansionRounds {
level.Warn(logger).Log("msg", fmt.Sprintf("Brace expansion aborted: still not fully expanded after %d rounds. Check '%s' for brace insertion.", count, s))
return []string{ s } , false
}
count++
level.Debug(logger).Log("Depth", depth, "count", count,
"src", strings.Join(src, ","))
modified = false
for i, t := range src {
if ! src_modified[i] {
// since we need to keep the order this is much simpler instead
// of taking them out from src and do add. book keeping
res = append(res, t)
res_modified = append(res_modified, false)
continue
}
b := -1
e := -1
for k:= 0 ; k < len(t); k++ {
if t[k] == '{' && (k == 0 || t[k-1] != '\\') {
b = k
break
}
}
if b != -1 {
open := 0
// find the closing brace
for k:= b+1 ; k < len(t); k++ {
if t[k] == '}' && t[k-1] != '\\' {
if open == 0 {
e = k
break
}
open--
} else if t[k] == '{' && t[k-1] != '\\' {
open++
}
}
}
if (b != -1 || e != -1) && (b == -1 || e == -1 || b > e) {
res = append(res, t) // unbalanced
res_modified = append(res_modified, false)
continue
}
if b == -1 {
res = append(res, t) // no braces
res_modified = append(res_modified, false)
continue
}
prefix := t[:b]
suffix := t[e+1:]
expr := t[b+1:e]
level.Debug(logger).Log("Prefix",prefix,"Suffix",suffix,"Expr",expr)
l, mod := expandBraces(expr, depth + 1, logger)
if mod {
if (depth > 0) {
t = expandList(prefix, l, suffix, true, depth, logger)
res = append(res, t)
} else {
t = strings.Join(l, ",")
res = append(res, prefix + "{" + t + "}" + suffix)
}
level.Debug(logger).Log("Re-insert", t,
"depth", depth, "count", count,)
res_modified = append(res_modified, true)
modified = true
continue
}
// not modified, so expand ranges
//idx := strings.LastIndexByte(prefix, ',')
t, mod := expandRange(expr, logger)
if mod {
modified = true
res = append(res, prefix + "{" + t + "}" + suffix)
res_modified = append(res_modified, true)
continue
}
// range not modified, so expand commas
level.Debug(logger).Log("Splitting", expr)
mod = false
xl := strings.Split(expr, ",")
if len(xl) > 1 {
modified = true
mod = true
}
t = expandList(prefix, xl, suffix, false, depth, logger)
if depth > 0 {
res = append(res, t)
res_modified = append(res_modified, mod)
} else {
for _, t := range xl {
res = append(res, prefix + t + suffix)
res_modified = append(res_modified, mod)
}
}
}
if modified {
src = res
res = []string{}
src_modified = res_modified
res_modified = []bool{}
} else {
level.Debug(logger).Log("Done_round",count,
"res", strings.Join(res, "¦"))
}
}
level.Debug(logger).Log("depth",depth, "Returning", strings.Join(res, "¦"))
return res , count > 1
}
func expandRange(expr string, logger log.Logger) (string, bool) {
if len(expr) < 4 {
return expr , false
}
level.Debug(logger).Log("Expanding_range", expr)
idx := strings.Index(expr, "..")
if idx == -1 {
return expr , false
}
var sb strings.Builder
start := expr[:idx]
stop := expr[idx+2:]
step := ""
var sw int64 = 0
fmtspec := ""
// n3
idx = strings.Index(stop, "..")
if idx != -1 {
step = stop[idx+2:]
stop = stop[:idx]
}
// %fmt
if len(step) == 0 {
fidx := strings.IndexByte(stop, '%')
if fidx != -1 {
fmtspec = stop[fidx:]
stop = stop[:fidx]
}
} else {
fidx := strings.IndexByte(step, '%')
if fidx != -1 {
fmtspec = step[fidx:]
step = step[:fidx]
}
}
if len(step) != 0 {
swo, err := strconv.ParseInt(step, 0, 64)
if err != nil || swo == 0 {
return expr , false
}
sw = swo
}
level.Debug(logger).Log("Range_params", ":",
"start",start, "stop",stop, "step", step, "fmt", fmtspec)
if len(start) == 1 && len(stop) == 1 &&
(start[0] < '0' || start[0] > '9' || stop[0] < '0' || stop[0] > '9') {
level.Debug(logger).Log("RangeType", "l1..l2")
// l1..l2
b := start[0]
e := stop[0]
reverse := false
if b > e {
reverse = true
c := b
b = e
e = c
}
if e >= 'a' {
if b < 'a' || e > 'z' {
return expr , false
}
} else if e >= 'A' {
if b < 'A' || e > 'Z' {
return expr , false
}
} else {
return expr , false
}
if reverse {
c := b
b = e
e = c
}
if (sw == 0) {
sw = 1
if b > e {
sw = -1
}
}
if (b > e) && (sw > 0) {
// invalid step width
return expr , false
}
var w byte = byte(sw)
if len(fmtspec) == 0 {
fmtspec = "%c"
} else {
t := fmt.Sprintf(fmtspec, b)
if strings.HasPrefix(t, "%!") {
return expr , false // invalid format specifier
}
}
level.Debug(logger).Log("Range_params", ":",
"start",b, "stop",e, "step",w, "fmt", fmtspec)
if b <= e {
for i := b; i <= e; i += w {
sb.WriteString(",")
sb.WriteString(fmt.Sprintf(fmtspec, i))
}
} else {
for i := b; i >= e; i += w {
sb.WriteString(",")
sb.WriteString(fmt.Sprintf(fmtspec, i))
}
}
level.Debug(logger).Log("Range_Result", sb.String()[1:])
return sb.String()[1:] , true
}
level.Debug(logger).Log("RangeType", "n1..n2")
b, err := strconv.ParseInt(start, 0, 64)
if err != nil {
return expr , false
}
e, err := strconv.ParseInt(stop, 0, 64)
if err != nil {
return expr , false
}
if len(fmtspec) == 0 {
fmtspec = "%d"
} else {
t := fmt.Sprintf(fmtspec, b)
if strings.HasPrefix(t, "%!") {
return expr , false // invalid format specifier
}
}
if sw == 0 {
sw = 1
if b > e {
sw = -1
}
} else if (b > e && sw > 0) || (b < e && sw < 0) {
return fmt.Sprintf(fmtspec, b), true
}
if b <= e {
for i := b; i <= e; i += sw {
sb.WriteString(",")
sb.WriteString(fmt.Sprintf(fmtspec, i))
}
} else {
for i := b; i >= e; i += sw {
sb.WriteString(",")
sb.WriteString(fmt.Sprintf(fmtspec, i))
}
}
level.Debug(logger).Log("Range_Result", sb.String()[1:])
return sb.String()[1:], true
}
func generateConfigModule(mname string, cfg *ModuleConfig, node *Node, nameToNode map[string]*Node, logger log.Logger) (*config.Module, error) {
out := &config.Module{}
needToWalk := map[string]struct{}{}
tableInstances := map[string][]string{}
ignore := map[string]bool{}
relink := map[string]string{}
if len(cfg.WalkParams.FallbackLabel) != 0 {
cfg.WalkParams.FallbackLabel = sanitizeLabelName(cfg.WalkParams.FallbackLabel)
}
// Apply type overrides for the current module.
for key, params := range cfg.Overrides {
level.Debug(logger).Log("module", mname, "Override", key, "TypeToForce", params.Type)
mnames := expandCfgItem(key, logger)
if len(mnames) > 1 {
// expand once, only.
t := strings.Join(mnames, "¦")
level.Debug(logger).Log("module", mname, "OverrideResult", t)
if key != t {
relink[key] = t
}
}
for _, name := range mnames {
if name == "_dummy" {
continue
}
ignore[name] = params.Ignore
if params.Type == "" {
continue
}
// Find node to override.
n, ok := nameToNode[name]
if !ok {
level.Warn(logger).Log("msg", "SNMP variable not found -> ignored", "module", mname, "name", name)
continue
}
// params.Type validated on unmarshall
level.Debug(logger).Log("module", mname, "metric", name, "metricType", n.Type, "forcedTo", params.Type)
n.Type = params.Type
}
}
for key, val := range relink {
cfg.Overrides[val] = cfg.Overrides[key]
delete(cfg.Overrides, key)
}
// Remove redundant OIDs to be walked.
toWalk := []string{}
forceWalk := map[string]bool{}
for _, oid := range cfg.Walk {
// Resolve name to OID if possible.
if oid == "_dummy" {
continue
}
a := expandCfgItem(oid, logger)
for _, name := range a {
force := false
if name[0] == '^' {
name = name[1:]
force = true
}
n, ok := nameToNode[name]
if ok {
name = n.Oid
}
toWalk = append(toWalk, name)
forceWalk[name] = force
}
}
toWalk = minimizeOids(toWalk)
// Find all top-level nodes.
metricNodes := map[*Node]struct{}{}
for _, oid := range toWalk {
metricNode, oidType := getMetricNode(oid, node, nameToNode)
if forceWalk[oid] {
n, _ := nameToNode[oid]
label := oid
if n != nil {
label = n.Label
}
level.Warn(logger).Log("module", mname, "node", label, "oid", oid, "oidType", oidType, "forcedOidTyp", oidSubtree)
oidType = oidSubtree
}
switch oidType {
case oidNotFound:
return nil, fmt.Errorf("cannot find oid '%s' to walk (module: %s)", oid, mname)
case oidSubtree:
needToWalk[oid] = struct{}{}
case oidInstance:
// Add a trailing period to the OID to indicate a "Get" instead of a "Walk".
needToWalk[oid+"."] = struct{}{}
// Save instance index for lookup.
index := strings.Replace(oid, metricNode.Oid, "", 1)
tableInstances[metricNode.Oid] = append(tableInstances[metricNode.Oid], index)
case oidScalar:
// Scalar OIDs must be accessed using index 0.
needToWalk[oid+".0."] = struct{}{}
}
metricNodes[metricNode] = struct{}{}
}
// Sort the metrics by OID to make the output deterministic.
metrics := make([]*Node, 0, len(metricNodes))
for key := range metricNodes {
metrics = append(metrics, key)
}
sort.Slice(metrics, func(i, j int) bool {
return metrics[i].Oid < metrics[j].Oid
})
cfg.Prefix = sanitizeLabelName(cfg.Prefix)
// Find all the usable metrics.
for _, metricNode := range metrics {
walkNode(metricNode, func(n *Node) {
t, ok := metricType(n.Type)
if !ok {
return // Unsupported type.
}
if !metricAccess(n.Access) {
return // Inaccessible metrics.
}
if ignore[n.Label] {
return // Ignored metric.
}
metric := &config.Metric{
Name: sanitizeMetricName(n.Label, cfg.Prefix),
Oid: n.Oid,
Type: t,
Help: n.Description + " - " + n.Oid,
Indexes: []*config.Index{},
Lookups: []*config.Lookup{},
EnumValues: n.EnumValues,
}
prevType := ""
// "copy" Indexes from the MIB node into the metric node
for count, i := range n.Indexes {
index := &config.Index{Labelname: i, IsNative: true}
indexNode, ok := nameToNode[i]
if !ok {
level.Warn(logger).Log("msg", "Could not find index for node", "module", mname, "node", n.Label, "index", i)
return
}
index.Type, ok = metricType(indexNode.Type)
if !ok {
level.Warn(logger).Log("msg", "Can't handle index type on node", "module", mname, "node", n.Label, "index", i, "type", indexNode.Type)
return
}
index.FixedSize = indexNode.FixedSize
if n.ImpliedIndex && count+1 == len(n.Indexes) {
index.Implied = true
}
index.EnumValues = indexNode.EnumValues
index.Oid = indexNode.Oid
// Convert (InetAddressType,InetAddress) to (InetAddress)
if subtype, ok := combinedTypes[index.Type]; ok {
if prevType == subtype {
metric.Indexes = metric.Indexes[:len(metric.Indexes)-1]
} else {
level.Warn(logger).Log("msg", "Can't handle index type on node, missing preceding", "module", mname, "node", n.Label, "type", index.Type, "missing", subtype)
return
}
}
prevType = indexNode.TextualConvention
metric.Indexes = append(metric.Indexes, index)
}
out.Metrics = append(out.Metrics, metric)
})
}
// Apply lookups.
// first normalize possible metric prefixes and compile regex if needed
mprefix_re := map[string]*regexp.Regexp{}
for _, lookup := range cfg.Lookups {
newList := []string{}
for n, name := range lookup.Mprefix {
if name[0] == '_' {
// regex variant
if len(cfg.Prefix) != 0 {
lookup.Mprefix[n] = "_" + cfg.Prefix + name
}
x, err := regexp.Compile("^(?:" + lookup.Mprefix[n][1:] + ")")
if err != nil {
return nil, fmt.Errorf("invalid mprefix regex '%s' (module: %s)", name, mname)
}
mprefix_re[lookup.Mprefix[n]] = x
newList = append(newList, lookup.Mprefix[n])
} else {
l := expandCfgItem(strings.TrimSpace(name), logger)
if len(l) > 1 {
level.Debug(logger).Log("mprefix", name, "expanded", strings.Join(l, "¦"))
}
for _, prefix := range l {
newList = append(newList, sanitizeMetricName(strings.TrimSpace(prefix), cfg.Prefix))
}
}
}
lookup.Mprefix = newList
}
// now apply to relevant metrics
for _, metric := range out.Metrics {
toDelete := map[string]int{}
s := ""
for _, lookup := range cfg.Lookups {
if lookup.DropSourceIndexes {
for _, idx := range metric.Indexes {
toDelete[sanitizeLabelName(idx.Labelname)] = 1
}
break
}
}
lookupSeen := map[string]int{}
_idxSeen := false
for _, lookup := range cfg.Lookups {
mprefix_idxs := [][]int{}
mprefix_used := ""
if len(lookup.Mprefix) > 0 {
found := false
for _, mname := range lookup.Mprefix {
if mname[0] == '_' {
// need the official name here
n, _ := nameToNode[metric.Oid]
mprefix_idxs = mprefix_re[mname].FindAllStringSubmatchIndex(n.Label, -1)
level.Debug(logger).Log("mprefix", mname, "metric", n.Label, "matched", len(mprefix_idxs) != 0)
if len(mprefix_idxs) == 0 {
continue
}
// match: TBD: keep indexes
mprefix_used = mname
found = true
break
} else if strings.HasPrefix(metric.Name, mname) || strings.HasPrefix(metric.Oid, mname) {
found = true
break
}
}
if ! found {
continue
}
}
foundIndexes := 0
// See if all source indexes are defined for the target.
// metric.Indexes are the indexes found in the MIB definition of
// the related target aka table.
for _, lookupIndex := range lookup.SourceIndexes {
found := false
for _, index := range metric.Indexes {
if index.Labelname == lookupIndex {
foundIndexes++
found = true
break
}
}
if (! found) && (lookupIndex != "_dummy") {
s += ", " + lookupIndex
}
}
l := &config.Lookup {
Labelvalue: lookup.Revalue,
Remap: lookup.Remap,
SubOidRemap: lookup.SubOidRemap,
SubOids: lookup.SubOids,
}
l.Inject = len(lookup.SourceIndexes) == 0
if (! l.Inject) && (foundIndexes != len(lookup.SourceIndexes) || foundIndexes == 0) {
if len(s) > 0 {
m := metric.Name
if len(cfg.Prefix) > 0 {
m = metric.Name[len(cfg.Prefix)+1:]
}
level.Debug(logger).Log("msg", "Skipping lookup", "module", mname, "metric", m, "not defined in MIB", s[2:])
}
continue
}
oid_name := strings.Split(lookup.Lookup, "¦")
last := len(oid_name) - 1
if l.Inject {
nextLabel:
for c, label := range oid_name {
// add as pseudo index so that we get subOids as needed
orig_label := ""
if len(mprefix_idxs) > 0 && strings.IndexByte(label, '$') != -1 {
b := []byte{}
for _, submatches := range mprefix_idxs {
b = mprefix_re[mprefix_used].ExpandString(b, label, metric.Name, submatches)
}
if len(b) != 0 {
orig_label = label
label = string(b)
}
}
var idx *config.Index
var idxNode *Node
if label == "_idx" {
if _idxSeen {
continue nextLabel
}
idx = &config.Index{Labelname: label, Type: "DisplayString", Oid: "0", IsNative: false, }
_idxSeen = true
} else {
ok := false
idxNode, ok = nameToNode[label]
if !ok {
if len(orig_label) != 0 {
return nil, fmt.Errorf("Could not find generated pseudo index '%s' for %s::%s (orig: %s)", label, mname, metric.Name, orig_label)
} else {
return nil, fmt.Errorf("Could not find pseudo index '%s' for %s::%s", label, mname, metric.Name)
}
}
for _, midx := range metric.Indexes {
if midx.Oid == idxNode.Oid {
continue nextLabel
}
}
idx = &config.Index{Labelname: label}
idx.Type, ok = metricType(idxNode.Type)
if !ok {
return nil, fmt.Errorf("No type info found for pseudo index '%s' for %s::%s", label, mname, metric.Name)
}
idx.Oid = idxNode.Oid
idx.FixedSize = idxNode.FixedSize
idx.Implied = idxNode.ImpliedIndex
idx.EnumValues = idxNode.EnumValues
}
metric.Indexes = append(metric.Indexes, idx)
if c < last && label != "_idx" {
if len(tableInstances[metric.Oid]) > 0 {
for _, idx := range tableInstances[metric.Oid] {
needToWalk[idxNode.Oid + idx + "."] = struct{}{}
}