| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package snmp |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "path/filepath" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/snmp/ddsnmp" |
| 12 | ) |
| 13 | |
| 14 | func (c *Collector) collectSNMP(mx map[string]int64) error { |
| 15 | if c.ddSnmpColl == nil { |
| 16 | return nil |
| 17 | } |
| 18 | |
| 19 | pms, err := c.ddSnmpColl.Collect() |
| 20 | if err != nil { |
| 21 | c.markBGPCollectFailed(err) |
| 22 | return err |
| 23 | } |
| 24 | if c.bgp == nil && profileMetricsHaveBGP(pms) { |
| 25 | c.enableBGPIntegration() |
| 26 | } |
| 27 | |
| 28 | c.resetIfaceCache() |
| 29 | c.collectLicensing(mx, pms) |
| 30 | |
| 31 | metrics := c.prepareProfileMetrics(pms) |
| 32 | c.collectProfileScalarMetrics(mx, metrics) |
| 33 | c.collectProfileTableMetrics(mx, metrics) |
| 34 | c.collectProfileStats(mx, pms) |
| 35 | |
| 36 | c.finalizeIfaceCache() |
| 37 | c.finalizeProfileMetrics() |
| 38 | |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | func (c *Collector) collectProfileScalarMetrics(mx map[string]int64, metrics []ddsnmp.Metric) { |
| 43 | for _, m := range metrics { |
| 44 | if m.IsTable || m.Name == "" { |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | if !c.seenScalarMetrics[m.Name] { |
| 49 | c.seenScalarMetrics[m.Name] = true |
| 50 | c.addProfileScalarMetricChart(m) |
| 51 | } |
| 52 | |
| 53 | if len(m.MultiValue) == 0 { |
| 54 | mx[metricIDFromName(m.Name)] = m.Value |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | for k, v := range m.MultiValue { |
| 59 | mx[metricIDFromName(m.Name, k)] = v |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (c *Collector) collectProfileTableMetrics(mx map[string]int64, metrics []ddsnmp.Metric) { |
| 65 | seen := make(map[string]bool) |
| 66 | |
| 67 | for _, m := range metrics { |
| 68 | if !m.IsTable || m.Name == "" || len(m.Tags) == 0 { |
| 69 | continue |
| 70 | } |
| 71 | |
| 72 | key := tableMetricKey(m) |
| 73 | if key == "" { |
| 74 | continue |
| 75 | } |
| 76 | |
| 77 | seen[key] = true |
| 78 | |
| 79 | if !c.seenTableMetrics[key] { |
| 80 | c.seenTableMetrics[key] = true |
| 81 | c.addProfileTableMetricChart(m) |
| 82 | } |
| 83 | |
| 84 | if len(m.MultiValue) == 0 { |
| 85 | mx[metricIDFromKey(key)] += m.Value |
| 86 | } else { |
| 87 | for k, v := range m.MultiValue { |
| 88 | mx[metricIDFromKey(key, k)] = v |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if isIfaceMetric(m.Name) { |
| 93 | c.updateIfaceCacheEntry(m) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | for key := range c.seenTableMetrics { |
| 98 | if !seen[key] { |
| 99 | delete(c.seenTableMetrics, key) |
| 100 | c.removeProfileTableMetricChart(key) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func (c *Collector) collectProfileStats(mx map[string]int64, pms []*ddsnmp.ProfileMetrics) { |
| 106 | for _, pm := range pms { |
| 107 | name := stripFileNameExt(pm.Source) |
| 108 | |
| 109 | if !c.seenProfiles[name] { |
| 110 | c.seenProfiles[name] = true |
| 111 | c.addProfileStatsCharts(name) |
| 112 | } |
| 113 | |
| 114 | px := fmt.Sprintf("snmp_device_prof_%s_stats_", name) |
| 115 | mx[px+"timings_scalar"] = pm.Stats.Timing.Scalar.Milliseconds() |
| 116 | mx[px+"timings_table"] = pm.Stats.Timing.Table.Milliseconds() |
| 117 | mx[px+"timings_licensing"] = pm.Stats.Timing.Licensing.Milliseconds() |
| 118 | mx[px+"timings_bgp"] = pm.Stats.Timing.BGP.Milliseconds() |
| 119 | mx[px+"timings_virtual"] = pm.Stats.Timing.VirtualMetrics.Milliseconds() |
| 120 | mx[px+"snmp_get_requests"] = pm.Stats.SNMP.GetRequests |
| 121 | mx[px+"snmp_get_oids"] = pm.Stats.SNMP.GetOIDs |
| 122 | mx[px+"snmp_walk_pdus"] = pm.Stats.SNMP.WalkPDUs |
| 123 | mx[px+"snmp_walk_requests"] = pm.Stats.SNMP.WalkRequests |
| 124 | mx[px+"snmp_tables_walked"] = pm.Stats.SNMP.TablesWalked |
| 125 | mx[px+"snmp_tables_cached"] = pm.Stats.SNMP.TablesCached |
| 126 | mx[px+"metrics_scalar"] = pm.Stats.Metrics.Scalar |
| 127 | mx[px+"metrics_table"] = pm.Stats.Metrics.Table |
| 128 | mx[px+"metrics_virtual"] = pm.Stats.Metrics.Virtual |
| 129 | mx[px+"metrics_licensing"] = pm.Stats.Metrics.Licensing |
| 130 | mx[px+"metrics_bgp"] = pm.Stats.Metrics.BGP |
| 131 | mx[px+"metrics_tables"] = pm.Stats.Metrics.Tables |
| 132 | mx[px+"metrics_rows"] = pm.Stats.Metrics.Rows |
| 133 | mx[px+"table_cache_hits"] = pm.Stats.TableCache.Hits |
| 134 | mx[px+"table_cache_misses"] = pm.Stats.TableCache.Misses |
| 135 | mx[px+"errors_snmp"] = pm.Stats.Errors.SNMP |
| 136 | mx[px+"errors_processing_scalar"] = pm.Stats.Errors.Processing.Scalar |
| 137 | mx[px+"errors_processing_table"] = pm.Stats.Errors.Processing.Table |
| 138 | mx[px+"errors_processing_licensing"] = pm.Stats.Errors.Processing.Licensing |
| 139 | mx[px+"errors_processing_bgp"] = pm.Stats.Errors.Processing.BGP |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func tableMetricKey(m ddsnmp.Metric) string { |
| 144 | if m.Name == "" { |
| 145 | return "" |
| 146 | } |
| 147 | |
| 148 | // Filter keys we actually use (skip "_" and empty values) and precompute final length. |
| 149 | include := make([]string, 0, len(m.Tags)) |
| 150 | totalLen := len(m.Name) |
| 151 | for k, v := range m.Tags { |
| 152 | if v == "" || strings.HasPrefix(k, "_") { |
| 153 | continue |
| 154 | } |
| 155 | include = append(include, k) |
| 156 | totalLen += len("_") + len(v) |
| 157 | } |
| 158 | if len(include) == 0 { |
| 159 | return m.Name |
| 160 | } |
| 161 | |
| 162 | sort.Strings(include) |
| 163 | |
| 164 | var sb strings.Builder |
| 165 | sb.Grow(totalLen) |
| 166 | |
| 167 | sb.WriteString(m.Name) |
| 168 | for _, k := range include { |
| 169 | sb.WriteByte('_') |
| 170 | sb.WriteString(m.Tags[k]) |
| 171 | } |
| 172 | |
| 173 | return sb.String() |
| 174 | } |
| 175 | |
| 176 | func stripFileNameExt(path string) string { |
| 177 | return strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) |
| 178 | } |