Skip to content

Commit

Permalink
Add a scaling factor (#1026)
Browse files Browse the repository at this point in the history
Allow the metric value to be pre-scaled based on a float64 multiplier.
This allows for simple unit conversions like bits to bytes,
centi-degrees, etc.

Fixes: #734

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ authored Oct 22, 2023
1 parent 2cd2b5c commit dbfe95c
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ dependencies-stamp

generator/generator
generator/mibs
generator/snmp.yml
4 changes: 4 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o
}
}

if metric.Scale != 0.0 {
value *= metric.Scale
}

sample, err := prometheus.NewConstMetric(prometheus.NewDesc(metric.Name, metric.Help, labelnames, nil),
t, value, labelvalues...)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ func TestPduToSample(t *testing.T) {
oidToPdu: make(map[string]gosnmp.SnmpPDU),
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{value:2}`},
},
{
pdu: &gosnmp.SnmpPDU{
Name: "1.1.1.1.1",
Type: gosnmp.Integer,
Value: 420,
},
indexOids: []int{},
metric: &config.Metric{
Name: "test_metric",
Oid: "1.1.1.1.1",
Type: "gauge",
Help: "Help string",
Scale: 0.1,
},
oidToPdu: make(map[string]gosnmp.SnmpPDU),
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{value:42}`},
},
{
pdu: &gosnmp.SnmpPDU{
Name: "1.1.1.1.1",
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ type Metric struct {
Lookups []*Lookup `yaml:"lookups,omitempty"`
RegexpExtracts map[string][]RegexpExtract `yaml:"regex_extracts,omitempty"`
EnumValues map[int]string `yaml:"enum_values,omitempty"`
Scale float64 `yaml:"scale,omitempty"`
}

type Index struct {
Expand Down
1 change: 1 addition & 0 deletions generator/FORMAT.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ modules:
Temp: # A new metric will be created appending this to the metricName to become metricNameTemp.
- regex: '(.*)' # Regex to extract a value from the returned SNMP walks's value.
value: '$1' # Parsed as float64, defaults to $1.
scale: 0.125 # Scale the sample by this value, for example bits to bytes.
enum_values: # Enum for this metric. Only used with the enum types.
0: true
1: false
Expand Down
1 change: 1 addition & 0 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ modules:
value: '1' # The first entry whose regex matches and whose value parses wins.
- regex: '.*'
value: '0'
scale: 1.0 # Scale the value of the sample by this value.
type: DisplayString # Override the metric type, possible types are:
# gauge: An integer with type gauge.
# counter: An integer with type counter.
Expand Down
1 change: 1 addition & 0 deletions generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
type MetricOverrides struct {
Ignore bool `yaml:"ignore,omitempty"`
RegexpExtracts map[string][]config.RegexpExtract `yaml:"regex_extracts,omitempty"`
Scale float64 `yaml:"scale,omitempty"`
Type string `yaml:"type,omitempty"`
}

Expand Down
4 changes: 4 additions & 0 deletions generator/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ modules:
lookup: st4AdcSensorName
drop_source_indexes: true

overrides:
st4TempSensorValue:
scale: 0.1

# Palo Alto Firewalls
#
# Palo Alto MIBs can be found here:
Expand Down
1 change: 1 addition & 0 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*
for _, metric := range out.Metrics {
if name == metric.Name || name == metric.Oid {
metric.RegexpExtracts = params.RegexpExtracts
metric.Scale = params.Scale
}
}
}
Expand Down
1 change: 1 addition & 0 deletions snmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23225,6 +23225,7 @@ modules:
labelname: st4UnitName
oid: 1.3.6.1.4.1.1718.4.1.2.2.1.3
type: DisplayString
scale: 0.1
- name: st4TempSensorStatus
oid: 1.3.6.1.4.1.1718.4.1.9.3.1.2
type: gauge
Expand Down

0 comments on commit dbfe95c

Please sign in to comment.