master
go 66 lines 1.65 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package snmp
4
5 import "strings"
6
7 func chartIDFromName(name string) string {
8 if isBGPChartMetricName(name) {
9 return "snmp_" + cleanMetricName.Replace(name)
10 }
11 return "snmp_device_prof_" + cleanMetricName.Replace(name)
12 }
13
14 func chartIDFromKey(key string) string {
15 if isBGPChartMetricKey(key) {
16 return "snmp_" + cleanMetricName.Replace(key)
17 }
18 return "snmp_device_prof_" + cleanMetricName.Replace(key)
19 }
20
21 func metricIDFromName(name string, subkeys ...string) string {
22 if isBGPChartMetricName(name) {
23 return cleanedMetricID("snmp_", name, subkeys...)
24 }
25 return rawMetricID("snmp_device_prof_", name, subkeys...)
26 }
27
28 func metricIDFromKey(key string, subkeys ...string) string {
29 if isBGPChartMetricKey(key) {
30 return cleanedMetricID("snmp_", key, subkeys...)
31 }
32 return rawMetricID("snmp_device_prof_", key, subkeys...)
33 }
34
35 func rawMetricID(prefix, base string, subkeys ...string) string {
36 var id strings.Builder
37 id.WriteString(prefix + base)
38 for _, subkey := range subkeys {
39 id.WriteString("_" + subkey)
40 }
41 return id.String()
42 }
43
44 func cleanedMetricID(prefix, base string, subkeys ...string) string {
45 var id strings.Builder
46 id.WriteString(prefix + cleanMetricName.Replace(base))
47 for _, subkey := range subkeys {
48 id.WriteString("_" + cleanMetricName.Replace(subkey))
49 }
50 return id.String()
51 }
52
53 func chartContextID(name string) string {
54 if isBGPChartMetricName(name) {
55 return "snmp." + name
56 }
57 return "snmp.device_prof_" + cleanMetricName.Replace(name)
58 }
59
60 func isBGPChartMetricName(name string) bool {
61 return strings.HasPrefix(name, "bgp.")
62 }
63
64 func isBGPChartMetricKey(key string) bool {
65 return strings.HasPrefix(key, "bgp.")
66 }