Skip to content

Commit

Permalink
Add an offset factor (#1029)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
SuperQ committed Oct 29, 2023
1 parent f427269 commit f587fce
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o
if metric.Scale != 0.0 {
value *= metric.Scale
}
value += metric.Offset

sample, err := prometheus.NewConstMetric(prometheus.NewDesc(metric.Name, metric.Help, labelnames, nil),
t, value, labelvalues...)
Expand Down
35 changes: 35 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,41 @@ func TestPduToSample(t *testing.T) {
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",
Type: gosnmp.Integer,
Value: 70,
},
indexOids: []int{},
metric: &config.Metric{
Name: "test_metric",
Oid: "1.1.1.1.1",
Type: "gauge",
Help: "Help string",
Offset: -1.0,
},
oidToPdu: make(map[string]gosnmp.SnmpPDU),
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{value:69}`},
},
{
pdu: &gosnmp.SnmpPDU{
Name: "1.1.1.1.1",
Type: gosnmp.Integer,
Value: 2,
},
indexOids: []int{},
metric: &config.Metric{
Name: "test_metric",
Oid: "1.1.1.1.1",
Type: "gauge",
Help: "Help string",
Offset: 2.0,
Scale: -1.0,
},
oidToPdu: make(map[string]gosnmp.SnmpPDU),
expectedMetrics: []string{`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{value:0}`},
},
{
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"`
Offset float64 `yaml:"offset,omitempty"`
Scale float64 `yaml:"scale,omitempty"`
}

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.
offset: 0.0 # Adds the value to the sample. Applied after scale.
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
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'
offset: 1.0 # Add the value to the same. Applied after scale.
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.
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"`
Offset float64 `yaml:"offset,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 @@ -597,6 +597,10 @@ modules:
ignore: true # Lookup metric
ifType:
type: EnumAsInfo
# Remap enums where 1==true, 2==false to become 0==false, 1==true.
hrDiskStorageRemoveble:
scale: -1.0
offset: 2.0

# CyberPower
#
Expand Down
1 change: 1 addition & 0 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,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.Offset = params.Offset
metric.Scale = params.Scale
}
}
Expand Down
2 changes: 2 additions & 0 deletions snmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13225,6 +13225,8 @@ modules:
enum_values:
1: "true"
2: "false"
offset: 2
scale: -1
- name: hrDiskStorageCapacity
oid: 1.3.6.1.2.1.25.3.6.1.4
type: gauge
Expand Down

0 comments on commit f587fce

Please sign in to comment.