forked from prometheus/snmp_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.go
1177 lines (1119 loc) · 36 KB
/
collector.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 (
"context"
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gosnmp/gosnmp"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/jelmd/snmp-export/config"
)
var (
snmpUnexpectedPduType = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "snmp_unexpected_pdu_type_total",
Help: "Unexpected Go types in a PDU.",
},
)
// 64-bit float mantissa: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
float64Mantissa uint64 = 9007199254740992
wrapCounters = kingpin.Flag("snmp.wrap-large-counters", "Wrap 64-bit counters to avoid floating point rounding.").Default("true").Bool()
nullRegexp config.Regexp // for the golang null bullshit bingo
)
func init() {
prometheus.MustRegister(snmpUnexpectedPduType)
}
// Types preceded by an enum with their actual type.
var combinedTypeMapping = map[string]map[int]string{
"InetAddress": {
1: "InetAddressIPv4",
2: "InetAddressIPv6",
},
"InetAddressMissingSize": {
1: "InetAddressIPv4",
2: "InetAddressIPv6",
},
"LldpPortId": {
1: "DisplayString",
2: "DisplayString",
3: "PhysAddress48",
5: "DisplayString",
7: "DisplayString",
},
}
func oidToList(oid string) []int {
result := []int{}
for _, x := range strings.Split(oid, ".") {
o, _ := strconv.Atoi(x)
result = append(result, o)
}
return result
}
func listToOid(l []int) string {
var result []string
for _, o := range l {
result = append(result, strconv.Itoa(o))
}
return strings.Join(result, ".")
}
var SnmpError = [...]string {
"No error occurred.",
"The size of the Response-PDU would be too large to transport.",
"The name of a requested object was not found.",
"A value in the request didn't match the structure that the recipient of the request had for the object, e.g. incorrect length or type.",
"An attempt was made to set a variable that has an Access value indicating that it is read-only.",
"An unexpected error occurred.",
"Access was denied to the object for security reasons.",
"The object type in a variable binding is incorrect for the object.",
"A variable binding specifies a length incorrect for the object.",
"A variable binding specifies an encoding incorrect for the object.",
"The value given in a variable binding is not possible for the object.",
"A specified variable does not exist and cannot be created.",
"A variable binding specifies a value that could be held by the variable but cannot be assigned to it at this time.",
"An attempt to set a variable required a resource that is not available.",
"An attempt to set a particular variable failed.",
"An attempt to set a particular variable as part of a group of variables failed, and the attempt to then undo the setting of other variables was not successful.",
"A problem occurred in authorization.",
"The variable cannot be written or created.",
"The name in a variable binding specifies a variable that does not exist.",
}
func ScrapeTarget(ctx context.Context, target string, config *config.Module, logger log.Logger) ([]gosnmp.SnmpPDU, error) {
// Set the options.
snmp := gosnmp.GoSNMP{}
snmp.Context = ctx
snmp.MaxRepetitions = uint32(config.WalkParams.MaxRepetitions)
snmp.Retries = config.WalkParams.Retries
snmp.Timeout = config.WalkParams.Timeout
snmp.Target = target
snmp.Port = 161
if host, port, err := net.SplitHostPort(target); err == nil {
snmp.Target = host
p, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("error converting port number to int for target %s: %s", target, err)
}
snmp.Port = uint16(p)
}
// Configure auth.
config.WalkParams.ConfigureSNMP(&snmp)
// Do the actual walk.
err := snmp.Connect()
if err != nil {
if err == context.Canceled {
return nil, fmt.Errorf("scrape canceled (possible timeout) connecting to target %s", snmp.Target)
}
return nil, fmt.Errorf("error connecting to target %s: %s", target, err)
}
defer snmp.Conn.Close()
result := []gosnmp.SnmpPDU{}
getOids := config.Get
maxOids := int(config.WalkParams.MaxRepetitions)
// Max Repetition can be 0, maxOids cannot. SNMPv1 can only report one OID error per call.
if maxOids == 0 || snmp.Version == gosnmp.Version1 {
maxOids = 1
}
for len(getOids) > 0 {
oids := len(getOids)
if oids > maxOids {
oids = maxOids
}
if DebugEnabled {
level.Debug(logger).Log("msg", "Getting OIDs", "oids", oids)
}
getStart := time.Now()
packet, err := snmp.Get(getOids[:oids])
if err != nil {
if err == context.Canceled {
return nil, fmt.Errorf("scrape canceled (possible timeout) getting target %s", snmp.Target)
}
return nil, fmt.Errorf("error getting target %s: %s", snmp.Target, err)
}
if DebugEnabled {
level.Debug(logger).Log("msg", "Get of OIDs completed", "oids", oids, "duration_seconds", time.Since(getStart))
}
// SNMPv1 will return packet error for unsupported OIDs.
if packet.Error == gosnmp.NoSuchName && snmp.Version == gosnmp.Version1 {
if DebugEnabled {
level.Debug(logger).Log("msg", "OID not supported by target", "oids", getOids[0])
}
getOids = getOids[oids:]
continue
}
// Response received with errors.
// TODO: "stringify" gosnmp errors instead of showing error code.
if packet.Error != gosnmp.NoError {
// see include/net-snmp/library/snmp.h
s := "unknown cause"
if packet.Error > 0 && packet.Error < 19 {
s = SnmpError[packet.Error]
}
return nil, fmt.Errorf("error reported by target %s: Error Status %d (%s)", snmp.Target, packet.Error, s)
}
for _, v := range packet.Variables {
if v.Type == gosnmp.NoSuchObject || v.Type == gosnmp.NoSuchInstance {
if DebugEnabled {
level.Debug(logger).Log("msg", "OID not supported by target", "oids", v.Name)
}
continue
}
result = append(result, v)
}
getOids = getOids[oids:]
}
for _, subtree := range config.Walk {
var pdus []gosnmp.SnmpPDU
if DebugEnabled {
level.Debug(logger).Log("msg", "Walking subtree", "oid", subtree)
}
walkStart := time.Now()
if snmp.Version == gosnmp.Version1 {
pdus, err = snmp.WalkAll(subtree)
} else {
pdus, err = snmp.BulkWalkAll(subtree)
}
if err != nil {
if err == context.Canceled {
return nil, fmt.Errorf("scrape canceled (possible timeout) walking target %s", snmp.Target)
}
return nil, fmt.Errorf("error walking target %s: %s", snmp.Target, err)
}
if DebugEnabled {
level.Debug(logger).Log("msg", "Walk of subtree completed", "oid", subtree, "duration_seconds", time.Since(walkStart))
}
result = append(result, pdus...)
}
return result, nil
}
type MetricNode struct {
metric *config.Metric
children map[int]*MetricNode
}
// Build a tree of metrics from the config, for fast lookup when there's lots of them.
func buildMetricTree(metrics []*config.Metric) *MetricNode {
metricTree := &MetricNode{children: map[int]*MetricNode{}}
for _, metric := range metrics {
head := metricTree
for _, o := range oidToList(metric.Oid) {
_, ok := head.children[o]
if !ok {
head.children[o] = &MetricNode{children: map[int]*MetricNode{}}
}
head = head.children[o]
}
head.metric = metric
}
return metricTree
}
type collector struct {
ctx context.Context
target string
module *config.Module
logger log.Logger
compact bool
name string
}
// Describe implements Prometheus.Collector.
func (c collector) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc("dummy", "dummy", nil, nil)
}
// Collect implements Prometheus.Collector.
func (c collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
pdus, err := ScrapeTarget(c.ctx, c.target, c.module, c.logger)
if err != nil {
level.Info(c.logger).Log("msg", "Error scraping target", "err", err)
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error scraping target", nil, nil), err)
return
}
help := ""
if ! c.compact {
help = "Time SNMP walk/bulkwalk took."
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("snmp_scrape_walk_duration_seconds", help, nil, nil),
prometheus.GaugeValue,
time.Since(start).Seconds())
if ! c.compact {
help = "PDUs returned from walk."
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("snmp_scrape_pdus_returned", help, nil, nil),
prometheus.GaugeValue,
float64(len(pdus)))
oidToPdu := make(map[string]gosnmp.SnmpPDU, len(pdus))
for _, pdu := range pdus {
oidToPdu[pdu.Name[1:]] = pdu
}
idxCache := map[string]string{}
metricTree := buildMetricTree(c.module.Metrics)
// Look for metrics that match each pdu.
PduLoop:
for oid, pdu := range oidToPdu {
head := metricTree
oidList := oidToList(oid)
for i, o := range oidList {
var ok bool
head, ok = head.children[o]
if !ok {
continue PduLoop
}
if head.metric != nil {
// Found a match.
samples := pduToSamples(oidList[i+1:], &pdu, head.metric, oidToPdu, idxCache, c.logger, c.compact, c.module.WalkParams.FallbackLabel)
for _, sample := range samples {
ch <- sample
}
break
}
}
}
if ! c.compact {
help = "Total SNMP time scrape took (walk and processing)."
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("snmp_scrape_duration_seconds", help, nil, nil),
prometheus.GaugeValue,
time.Since(start).Seconds())
}
func getPduValue(pdu *gosnmp.SnmpPDU) float64 {
switch pdu.Type {
case gosnmp.Counter64:
if *wrapCounters {
// Wrap by 2^53.
return float64(gosnmp.ToBigInt(pdu.Value).Uint64() % float64Mantissa)
} else {
return float64(gosnmp.ToBigInt(pdu.Value).Uint64())
}
case gosnmp.OpaqueFloat:
return float64(pdu.Value.(float32))
case gosnmp.OpaqueDouble:
return pdu.Value.(float64)
default:
return float64(gosnmp.ToBigInt(pdu.Value).Int64())
}
}
var timeZone, timeLocalDelta = time.Now().Zone()
// parseDateAndTime extracts a UNIX timestamp from an RFC 2579 DateAndTime.
func parseDateAndTime(pdu *gosnmp.SnmpPDU) (float64, error) {
var (
v []byte
tz *time.Location
err error
)
// DateAndTime should be a slice of bytes.
switch pduType := pdu.Value.(type) {
case []byte:
v = pdu.Value.([]byte)
default:
return 0, fmt.Errorf("invalid DateAndTime type %v", pduType)
}
pduLength := len(v)
// DateAndTime can be 8 or 11 bytes depending if the time zone is included.
switch pduLength {
case 5: // HP: YMDHM - so ancient => propably not UTC and day light saving
if v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 && v[4] == 0 {
return 0, nil
}
loc, _ := time.LoadLocation("Local")
t := time.Date(2000 + int(v[0]), time.Month(v[1]), int(v[2]), int(v[3]), int(v[4]), 0, 0, loc)
// Go time is a nightmare - what a bullshit compared to java
offset := int64(timeLocalDelta)
if t.IsDST() {
offset -= 3600
}
return float64(t.Unix() - int64(offset)), nil
case 7: // HP: YMDuHMS - so ancient => propably not UTC and day light saving
if v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 && v[4] == 0 && v[5] == 0 && v[6] == 0 {
return 0, nil
}
loc, _ := time.LoadLocation("Local")
t := time.Date(2000 + int(v[0]), time.Month(v[1]), int(v[2]), int(v[4]), int(v[5]), int(v[6]), 0, loc)
// Go time is a nightmare - what a bullshit compared to java
offset := int64(timeLocalDelta)
if t.IsDST() {
offset -= 3600
}
return float64(t.Unix() - int64(offset)), nil
case 8:
// No time zone included, assume UTC.
tz = time.UTC
case 11:
// Extract the timezone from the last 3 bytes.
locString := fmt.Sprintf("%c%02d%02d", v[8], v[9], v[10])
loc, err := time.Parse("-0700", locString)
if err != nil {
return 0, fmt.Errorf("error parsing DateAndTime location string: %q, error: %s", locString, err)
}
tz = loc.Location()
default:
return 0, fmt.Errorf("invalid DateAndTime length %v", pduLength)
}
if err != nil {
return 0, fmt.Errorf("unable to parse DateAndTime %q, error: %s", v, err)
}
// Build the date from the various fields and time zone.
t := time.Date(
int(binary.BigEndian.Uint16(v[0:2])),
time.Month(v[2]),
int(v[3]),
int(v[4]),
int(v[5]),
int(v[6]),
int(v[7])*1e+8,
tz)
return float64(t.Unix()), nil
}
func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, oidToPdu map[string]gosnmp.SnmpPDU, idxCache map[string]string, logger log.Logger, compact bool, fallbackLabel string) []prometheus.Metric {
var err error
// The part of the OID that is the indexes.
labels, subOid := indexesToLabels(indexOids, metric, pdu, oidToPdu, idxCache, logger)
_, ok := labels["@drop@"]
if ok {
return []prometheus.Metric{}
}
newName := metric.Name
if len(subOid) == 0 {
i := strings.LastIndexByte(metric.Oid, '.')
if i != -1 {
subOid = metric.Oid[i+1:]
}
}
if len(metric.Rename) != 0 && len(subOid) != 0 {
for _, e := range metric.Rename {
if e.SubOids == nullRegexp {
continue
}
idx := e.SubOids.FindStringIndex(subOid)
if idx != nil {
newName = e.Value;
break;
}
}
}
value := getPduValue(pdu)
t := prometheus.UntypedValue
labelnames := make([]string, 0, len(labels)+1)
labelvalues := make([]string, 0, len(labels)+1)
for k, v := range labels {
labelnames = append(labelnames, k)
labelvalues = append(labelvalues, v)
}
needRemap := len(metric.Remap) != 0
hasRegex := len(metric.RegexpExtracts) != 0
switch metric.Type {
case "counter":
t = prometheus.CounterValue
case "uptime":
t = prometheus.CounterValue
n := - int64(value)
if pdu.Type == 0x43 {
n /= 100 // Timeticks are usally given in 100 Hz
}
n += time.Now().Unix()
value = float64(n - (n & 1)) // n % 2 == n & 1
case "gauge":
t = prometheus.GaugeValue
case "Float", "Double":
t = prometheus.GaugeValue
case "DateAndTime":
t = prometheus.GaugeValue
value, err = parseDateAndTime(pdu)
if err != nil {
level.Warn(logger).Log("msg", err, "metric", metric.Name)
return []prometheus.Metric{}
}
case "EnumAsInfo":
return enumAsInfo(metric, newName, int(value), labelnames, labelvalues, compact, fallbackLabel)
case "EnumAsStateSet":
return enumAsStateSet(metric, newName, int(value), labelnames, labelvalues, compact, fallbackLabel)
case "Bits":
return bits(metric, newName, pdu.Value, labelnames, labelvalues, compact, fallbackLabel)
default:
// It's some form of string.
t = prometheus.GaugeValue
value = 1.0
metricType := metric.Type
if typeMapping, ok := combinedTypeMapping[metricType]; ok {
// Lookup associated sub type in previous object.
oids := strings.Split(metric.Oid, ".")
i, _ := strconv.Atoi(oids[len(oids)-1])
oids[len(oids)-1] = strconv.Itoa(i - 1)
prevOid := fmt.Sprintf("%s.%s", strings.Join(oids, "."), listToOid(indexOids))
if prevPdu, ok := oidToPdu[prevOid]; ok {
val := int(getPduValue(&prevPdu))
if t, ok := typeMapping[val]; ok {
metricType = t
} else {
metricType = "OctetString"
if DebugEnabled {
level.Debug(logger).Log("msg", "Unable to handle type value", "value", val, "oid", prevOid, "metric", newName)
}
}
} else {
metricType = "OctetString"
if DebugEnabled {
level.Debug(logger).Log("msg", "Unable to find type at oid for metric", "oid", prevOid, "metric", newName)
}
}
}
if hasRegex {
return applyRegexExtracts(metric, newName, subOid, strings.TrimSpace(pduValueAsString(pdu, metricType)), labelnames, labelvalues, logger, compact, fallbackLabel)
}
s := strings.TrimSpace(pduValueAsString(pdu, metricType))
// Put in the value as a label with the same name as the metric.
addLabel := true
if needRemap {
v , x := metric.Remap[s]
if x {
if v == "@drop@" {
return []prometheus.Metric{}
}
s = v
f, err := strconv.ParseFloat(v, 64)
if err == nil {
value = f
addLabel = false
}
}
}
needRemap = false
if addLabel {
// unlikely that it is already there
if len(metric.FallbackLabel) != 0 {
labelnames = append(labelnames, metric.FallbackLabel)
} else if len(fallbackLabel) != 0 {
labelnames = append(labelnames, fallbackLabel)
} else {
labelnames = append(labelnames, newName)
}
labelvalues = append(labelvalues, s)
}
}
help := ""
if ! compact {
help = metric.Help
}
if hasRegex {
return applyRegexExtracts(metric, newName, subOid, strconv.FormatFloat(value, 'f', -1, 64), labelnames, labelvalues, logger, compact, fallbackLabel)
}
if needRemap {
v, ok := metric.Remap[strconv.FormatFloat(value, 'f', -1, 64)]
if ok {
if v == "@drop@" {
return []prometheus.Metric{}
}
f, err := strconv.ParseFloat(v, 64)
if err == nil {
value = f
} else {
// unlikely that it is already there
if len(metric.FallbackLabel) != 0 {
labelnames = append(labelnames, metric.FallbackLabel)
} else if len(fallbackLabel) != 0 {
labelnames = append(labelnames, fallbackLabel)
} else {
labelnames = append(labelnames, newName)
}
labelvalues = append(labelvalues, v)
// value = 1.0 // not resetting it allows more flexebility
}
}
}
sample, err := prometheus.NewConstMetric(prometheus.NewDesc(newName, help, labelnames, nil),
t, value, labelvalues...)
if err != nil {
sample = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric", nil, nil),
fmt.Errorf("error for metric %s with labels %v from indexOids %v: %v", newName, labelvalues, indexOids, err))
}
return []prometheus.Metric{sample}
}
func applyRegexExtracts(metric *config.Metric, mName string, subOids string, pduValue string, labelnames, labelvalues []string, logger log.Logger, compact bool, fallbackLabel string) []prometheus.Metric {
results := []prometheus.Metric{}
help := ""
if ! compact {
help = metric.Help + " (regex extracted)"
}
for name, strMetricSlice := range metric.RegexpExtracts {
var newName string
if len(name) > 0 && name[0] == '.' {
newName = name[1:]
} else {
newName = mName + name
}
for _, strMetric := range strMetricSlice {
if strMetric.SubOids != nullRegexp {
idx := strMetric.SubOids.FindStringIndex(subOids)
if idx == nil {
continue
}
}
indexes := strMetric.Regex.FindStringSubmatchIndex(pduValue)
if (indexes == nil && !strMetric.Invert) || (indexes != nil && strMetric.Invert) {
if DebugEnabled {
level.Debug(logger).Log("msg", "No regex match", "metric", newName, "value", pduValue, "regex", strMetric.Regex.String(), "invert", strMetric.Invert)
}
continue
}
res := strMetric.Regex.ExpandString([]byte{}, strMetric.Value, pduValue, indexes)
s := string(res)
t, ok := metric.Remap[s]
if ok {
s = t
}
if s == "@drop@" {
if DebugEnabled {
level.Debug(logger).Log("msg", "Dropping metric", "metric", newName, "value", pduValue, "regex", strMetric.Regex.String(), "extracted_value", res)
}
return []prometheus.Metric{}
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
if DebugEnabled {
level.Debug(logger).Log("msg", "Error parsing float64 from value", "metric", newName, "value", pduValue, "regex", strMetric.Regex.String(), "extracted_value", res)
}
if len(metric.FallbackLabel) != 0 {
labelnames = append(labelnames, metric.FallbackLabel)
} else if len(fallbackLabel) != 0 {
labelnames = append(labelnames, fallbackLabel)
} else {
labelnames = append(labelnames, newName)
}
labelvalues = append(labelvalues, s)
v = 1.0
}
newMetric, err := prometheus.NewConstMetric(prometheus.NewDesc(newName, help, labelnames, nil),
prometheus.GaugeValue, v, labelvalues...)
if err != nil {
newMetric = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric for regex_extract", nil, nil),
fmt.Errorf("error for metric %s with labels %v: %v", newName+name, labelvalues, err))
}
results = append(results, newMetric)
break
}
}
return results
}
func enumAsInfo(metric *config.Metric, newName string, value int, labelnames, labelvalues []string, compact bool, fallbackLabel string) []prometheus.Metric {
// Lookup enum, default to the value.
state, ok := metric.EnumValues[int(value)]
if !ok {
state = strconv.Itoa(int(value))
}
t, ok := metric.Remap[state]
if ok {
if t == "@drop@" {
return []prometheus.Metric{}
}
state = t
}
if len(metric.FallbackLabel) != 0 {
labelnames = append(labelnames, metric.FallbackLabel)
} else if len(fallbackLabel) != 0 {
labelnames = append(labelnames, fallbackLabel)
} else {
labelnames = append(labelnames, newName)
}
labelvalues = append(labelvalues, state)
help := ""
if ! compact {
help = metric.Help + " (EnumAsInfo)"
}
newMetric, err := prometheus.NewConstMetric(prometheus.NewDesc(newName+"_info", help, labelnames, nil),
prometheus.GaugeValue, 1.0, labelvalues...)
if err != nil {
newMetric = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric for EnumAsInfo", nil, nil),
fmt.Errorf("error for metric %s with labels %v: %v", newName, labelvalues, err))
}
return []prometheus.Metric{newMetric}
}
func enumAsStateSet(metric *config.Metric, newName string, value int, labelnames, labelvalues []string, compact bool, fallbackLabel string) []prometheus.Metric {
if len(metric.FallbackLabel) != 0 {
labelnames = append(labelnames, metric.FallbackLabel)
} else if len(fallbackLabel) != 0 {
labelnames = append(labelnames, fallbackLabel)
} else {
labelnames = append(labelnames, newName)
}
results := []prometheus.Metric{}
state, ok := metric.EnumValues[value]
if !ok {
// Fallback to using the value.
state = strconv.Itoa(value)
}
t, ok := metric.Remap[state]
if ok {
if t == "@drop@" {
return []prometheus.Metric{}
}
state = t
}
help := ""
if ! compact {
help = metric.Help + " (EnumAsStateSet)"
}
newMetric, err := prometheus.NewConstMetric(prometheus.NewDesc(newName, help, labelnames, nil),
prometheus.GaugeValue, 1.0, append(labelvalues, state)...)
if err != nil {
newMetric = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric for EnumAsStateSet", nil, nil),
fmt.Errorf("error for metric %s with labels %v: %v", newName, labelvalues, err))
}
results = append(results, newMetric)
for k, v := range metric.EnumValues {
if k == value {
continue
}
t, ok := metric.Remap[v]
if ok {
if t == "@drop@" {
continue
}
v = t
}
newMetric, err := prometheus.NewConstMetric(prometheus.NewDesc(newName, help, labelnames, nil),
prometheus.GaugeValue, 0.0, append(labelvalues, v)...)
if err != nil {
newMetric = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric for EnumAsStateSet", nil, nil),
fmt.Errorf("error for metric %s with labels %v: %v", newName, labelvalues, err))
}
results = append(results, newMetric)
}
return results
}
func bits(metric *config.Metric, newName string, value interface{}, labelnames, labelvalues []string, compact bool, fallbackLabel string) []prometheus.Metric {
bytes, ok := value.([]byte)
if !ok {
return []prometheus.Metric{prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "BITS type was not a BISTRING on the wire.", nil, nil),
fmt.Errorf("error for metric %s with labels %v: %T", newName, labelvalues, value))}
}
if len(metric.FallbackLabel) != 0 {
labelnames = append(labelnames, metric.FallbackLabel)
} else if len(fallbackLabel) != 0 {
labelnames = append(labelnames, fallbackLabel)
} else {
labelnames = append(labelnames, newName)
}
results := []prometheus.Metric{}
help := ""
if ! compact {
help = metric.Help + " (Bits)"
}
for k, v := range metric.EnumValues {
bit := 0.0
// Most significant byte most significant bit, then most significant byte 2nd most significant bit etc.
if k < len(bytes)*8 {
if (bytes[k/8] & (128 >> (k % 8))) != 0 {
bit = 1.0
}
}
newMetric, err := prometheus.NewConstMetric(prometheus.NewDesc(newName, help, labelnames, nil),
prometheus.GaugeValue, bit, append(labelvalues, v)...)
if err != nil {
newMetric = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric for Bits", nil, nil),
fmt.Errorf("error for metric %s with labels %v: %v", newName, labelvalues, err))
}
results = append(results, newMetric)
}
return results
}
// Right pad oid with zeros, and split at the given point.
// Some routers exclude trailing 0s in responses.
func splitOid(oid []int, count int) ([]int, []int) {
head := make([]int, count)
tail := []int{}
for i, v := range oid {
if i < count {
head[i] = v
} else {
tail = append(tail, v)
}
}
return head, tail
}
// This mirrors decodeValue in gosnmp's helper.go.
func pduValueAsString(pdu *gosnmp.SnmpPDU, typ string) string {
switch pdu.Value.(type) {
case int:
return strconv.Itoa(pdu.Value.(int))
case uint:
return strconv.FormatUint(uint64(pdu.Value.(uint)), 10)
case uint64:
return strconv.FormatUint(pdu.Value.(uint64), 10)
case float32:
return strconv.FormatFloat(float64(pdu.Value.(float32)), 'f', -1, 32)
case float64:
return strconv.FormatFloat(pdu.Value.(float64), 'f', -1, 64)
case string:
if pdu.Type == gosnmp.ObjectIdentifier {
// Trim leading period.
return pdu.Value.(string)[1:]
}
// DisplayString.
return pdu.Value.(string)
case []byte:
if typ == "" {
typ = "OctetString"
}
// Reuse the OID index parsing code.
parts := make([]int, len(pdu.Value.([]byte)))
for i, o := range pdu.Value.([]byte) {
parts[i] = int(o)
}
if typ == "OctetString" || typ == "DisplayString" {
// Prepend the length, as it is explicit in an index.
parts = append([]int{len(pdu.Value.([]byte))}, parts...)
}
str, _, _ := indexOidsAsString(parts, typ, 0, false, nil)
return str
case nil:
return ""
default:
// This shouldn't happen.
snmpUnexpectedPduType.Inc()
return fmt.Sprintf("%s", pdu.Value)
}
}
// similar to ToValidUTF8, but replaces _each_ invalid byte with a single char
func makeValidUtf8(s []byte) []byte {
b := make([]byte, 0, len(s)+3)
for i := 0; i < len(s); {
c := s[i]
if c < utf8.RuneSelf {
i++
b = append(b, c)
continue
}
_, wid := utf8.DecodeRune(s[i:])
if wid == 1 {
i++
b = append(b, []byte { 0xe2, 0x9c, 0x8b }...)
continue
}
b = append(b, s[i:i+wid]...)
i += wid
}
return b
}
// Convert oids to a string index value.
//
// Returns the string, the oids that were used and the oids left over.
func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool, enumValues map[int]string) (string, []int, []int) {
if typeMapping, ok := combinedTypeMapping[typ]; ok {
subOid, valueOids := splitOid(indexOids, 2)
if typ == "InetAddressMissingSize" {
// The size of the main index value is missing.
subOid, valueOids = splitOid(indexOids, 1)
}
var str string
var used, remaining []int
if t, ok := typeMapping[subOid[0]]; ok {
str, used, remaining = indexOidsAsString(valueOids, t, 0, false, enumValues)
return str, append(subOid, used...), remaining
}
if typ == "InetAddressMissingSize" {
// We don't know the size, so pass everything remaining.
return indexOidsAsString(indexOids, "OctetString", 0, true, enumValues)
}
// The 2nd oid is the length.
return indexOidsAsString(indexOids, "OctetString", subOid[1]+2, false, enumValues)
}
switch typ {
case "Integer32", "Integer", "gauge", "counter", "uptime":
// Extract the oid for this index, and keep the remainder for the next index.
subOid, indexOids := splitOid(indexOids, 1)
return fmt.Sprintf("%d", subOid[0]), subOid, indexOids
case "PhysAddress48":
subOid, indexOids := splitOid(indexOids, 6)
parts := make([]string, 6)
for i, o := range subOid {
parts[i] = fmt.Sprintf("%02X", o)
}
return strings.Join(parts, ":"), subOid, indexOids
case "OctetString":
var subOid []int
// The length of fixed size indexes come from the MIB.
// For varying size, we read it from the first oid.
length := fixedSize
if implied {
length = len(indexOids)
}
if length == 0 {
subOid, indexOids = splitOid(indexOids, 1)
length = subOid[0]
}
content, indexOids := splitOid(indexOids, length)
subOid = append(subOid, content...)
parts := make([]byte, length)
for i, o := range content {
parts[i] = byte(o)
}
if len(parts) == 0 {
return "", subOid, indexOids
} else {
return fmt.Sprintf("0x%X", string(parts)), subOid, indexOids
}
case "DisplayString":
var subOid []int
length := fixedSize
if implied {
length = len(indexOids)
}
if length == 0 {
subOid, indexOids = splitOid(indexOids, 1)
length = subOid[0]
}
content, indexOids := splitOid(indexOids, length)
subOid = append(subOid, content...)
parts := make([]byte, length)
for i, o := range content {
parts[i] = byte(o)
}
// per default ASCII, but not if typ was forced to DisplayString.
// However, we take the optimistic approach.
if ! utf8.Valid(parts) {
parts = makeValidUtf8(parts)
}
return string(parts), subOid, indexOids
case "InetAddressIPv4":
subOid, indexOids := splitOid(indexOids, 4)
parts := make([]string, 4)
for i, o := range subOid {
parts[i] = strconv.Itoa(o)
}
return strings.Join(parts, "."), subOid, indexOids
case "InetAddressIPv6":
subOid, indexOids := splitOid(indexOids, 16)
parts := make([]interface{}, 16)
for i, o := range subOid {
parts[i] = o
}
return fmt.Sprintf("%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X", parts...), subOid, indexOids
case "EnumAsInfo":
subOid, indexOids := splitOid(indexOids, 1)
value, ok := enumValues[subOid[0]]
if ok {
return value, subOid, indexOids
} else {
return fmt.Sprintf("%d", subOid[0]), subOid, indexOids
}
default:
panic(fmt.Sprintf("Unknown index type %s", typ))
return "", nil, nil
}
}
func indexesToLabels(indexOids []int, metric *config.Metric, pdu *gosnmp.SnmpPDU, oidToPdu map[string]gosnmp.SnmpPDU, idxCache map[string]string, logger log.Logger) (map[string]string, string) {
labels := map[string]string{}
labelSubOids := map[string][]int{}
subOids := ""
// Prepare index info for the source indexes to lookup
poid := ""
for _, index := range metric.Indexes {
if index.Labelname == "_idx" {
if pdu == nil {
continue
}
if DebugEnabled {
level.Debug(logger).Log("indexOids", fmt.Sprintf("%v", indexOids), "moid", metric.Oid, "poid", pdu.Name[1:])
}
oid := metric.Oid
poid = pdu.Name[1:] //drop the leading .
if strings.HasSuffix(poid, ".0") {
poid = poid[:len(poid)-2]
}
i := -1
if len(poid) < len(oid) {
continue
} else if len(poid) == len(oid) {