From 4faf3306797e722ffd12e0a9f240916ab7c6b87b Mon Sep 17 00:00:00 2001 From: Kakuya Ando Date: Thu, 6 Jul 2023 00:15:51 +0900 Subject: [PATCH 1/2] Improved Lookup process for label information to utilize TypeMapping (#908) Signed-off-by: Kakuya Ando --- collector/collector.go | 28 +++++++++++++++++++++++----- collector/collector_test.go | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index f31e1460..1879bf39 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -566,10 +566,7 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o 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)) + prevOid := fmt.Sprintf("%s.%s", getPrevOid(metric.Oid), listToOid(indexOids)) if prevPdu, ok := oidToPdu[prevOid]; ok { val := int(getPduValue(&prevPdu)) if t, ok := typeMapping[val]; ok { @@ -872,6 +869,13 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool, } } +func getPrevOid(oid string) string { + oids := strings.Split(oid, ".") + i, _ := strconv.Atoi(oids[len(oids)-1]) + oids[len(oids)-1] = strconv.Itoa(i - 1) + return strings.Join(oids, ".") +} + func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string]gosnmp.SnmpPDU, metrics internalMetrics) map[string]string { labels := map[string]string{} labelOids := map[string][]int{} @@ -898,7 +902,21 @@ func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string oid = fmt.Sprintf("%s.%s", oid, listToOid(labelOids[label])) } if pdu, ok := oidToPdu[oid]; ok { - labels[lookup.Labelname] = pduValueAsString(&pdu, lookup.Type, metrics) + t := lookup.Type + if typeMapping, ok := combinedTypeMapping[lookup.Type]; ok { + // Lookup associated sub type in previous object. + prevOid := getPrevOid(lookup.Oid) + for _, label := range lookup.Labels { + prevOid = fmt.Sprintf("%s.%s", prevOid, listToOid(labelOids[label])) + } + if prevPdu, ok := oidToPdu[prevOid]; ok { + val := int(getPduValue(&prevPdu)) + if ty, ok := typeMapping[val]; ok { + t = ty + } + } + } + labels[lookup.Labelname] = pduValueAsString(&pdu, t, metrics) labelOids[lookup.Labelname] = []int{int(gosnmp.ToBigInt(pdu.Value).Int64())} } else { labels[lookup.Labelname] = "" diff --git a/collector/collector_test.go b/collector/collector_test.go index 7a5fae23..51382bd8 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -1006,6 +1006,25 @@ func TestIndexesToLabels(t *testing.T) { }, result: map[string]string{"a": "1", "chainable_id": "42", "targetlabel": "targetvalue"}, }, + { + oid: []int{1, 8, 1}, + metric: config.Metric{ + Indexes: []*config.Index{ + {Labelname: "lldpRemTimeMark", Type: "gauge"}, + {Labelname: "lldpRemLocalPortNum", Type: "gauge"}, + {Labelname: "lldpRemIndex", Type: "gauge"}, + }, + Lookups: []*config.Lookup{ + {Labels: []string{"lldpRemLocalPortNum"}, Labelname: "lldpLocPortId", Oid: "1.1.3", Type: "LldpPortId"}, + }, + }, + oidToPdu: map[string]gosnmp.SnmpPDU{ + "1.1.9.1.8.1": gosnmp.SnmpPDU{Value: "hostname"}, + "1.1.2.8": gosnmp.SnmpPDU{Value: 3}, + "1.1.3.8": gosnmp.SnmpPDU{Value: []byte{4, 5, 6, 7, 8, 9}}, + }, + result: map[string]string{"lldpRemTimeMark": "1", "lldpRemLocalPortNum": "8", "lldpRemIndex": "1", "lldpLocPortId": "04:05:06:07:08:09"}, + }, } for _, c := range cases { got := indexesToLabels(c.oid, &c.metric, c.oidToPdu, internalMetrics{}) From 81a8aa625048f4345e75c5feec757773cb320fc8 Mon Sep 17 00:00:00 2001 From: PrometheusBot Date: Thu, 6 Jul 2023 05:29:57 +0200 Subject: [PATCH 2/2] Synchronize common files from prometheus/prometheus (#907) * Update common Prometheus files Signed-off-by: prombot * Fixup linting issues Disable unused-parameter check. Signed-off-by: SuperQ --------- Signed-off-by: prombot Signed-off-by: SuperQ Co-authored-by: SuperQ --- .github/workflows/golangci-lint.yml | 10 ++++++---- .golangci.yml | 6 ++++++ .yamllint | 3 +-- collector/collector_test.go | 3 +-- config/config.go | 10 ++-------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 6034bcbf..433f71b8 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -1,3 +1,5 @@ +--- +# This action is synced from https://github.com/prometheus/prometheus name: golangci-lint on: push: @@ -18,13 +20,13 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - name: install Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.18.x + go-version: 1.20.x - name: Install snmp_exporter/generator dependencies run: sudo apt-get update && sudo apt-get -y install libsnmp-dev if: github.repository == 'prometheus/snmp_exporter' - name: Lint - uses: golangci/golangci-lint-action@v3.2.0 + uses: golangci/golangci-lint-action@v3.4.0 with: - version: v1.45.2 + version: v1.53.3 diff --git a/.golangci.yml b/.golangci.yml index fa108af5..8ae9b68e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,3 +26,9 @@ linters-settings: - (net/http.ResponseWriter).Write # Never check for logger errors. - (github.com/go-kit/log.Logger).Log + revive: + rules: + # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter + - name: unused-parameter + severity: warning + disabled: true diff --git a/.yamllint b/.yamllint index 19552574..955a5a62 100644 --- a/.yamllint +++ b/.yamllint @@ -20,5 +20,4 @@ rules: config/testdata/section_key_dup.bad.yml line-length: disable truthy: - ignore: | - .github/workflows/*.yml + check-keys: false diff --git a/collector/collector_test.go b/collector/collector_test.go index 51382bd8..a1a009d8 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -531,9 +531,8 @@ func TestPduToSample(t *testing.T) { if c.shouldErr { errHappened = true continue - } else { - t.Fatalf("Error writing metric: %v", err) } + t.Fatalf("Error writing metric: %v", err) } got := strings.ReplaceAll(m.Desc().String()+" "+metric.String(), " ", " ") if _, ok := expected[got]; !ok { diff --git a/config/config.go b/config/config.go index e7004d88..0c15c41a 100644 --- a/config/config.go +++ b/config/config.go @@ -94,10 +94,7 @@ type Module struct { func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error { *c = DefaultModule type plain Module - if err := unmarshal((*plain)(c)); err != nil { - return err - } - return nil + return unmarshal((*plain)(c)) } // ConfigureSNMP sets the various version and auth settings. @@ -286,10 +283,7 @@ type RegexpExtract struct { func (c *RegexpExtract) UnmarshalYAML(unmarshal func(interface{}) error) error { *c = DefaultRegexpExtract type plain RegexpExtract - if err := unmarshal((*plain)(c)); err != nil { - return err - } - return nil + return unmarshal((*plain)(c)) } // Regexp encapsulates a regexp.Regexp and makes it YAML marshalable.