Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignoremetrics: add feature to ignore Values returned by modbus device #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ type MetricDef struct {

// Scaling factor
Factor *float64 `yaml:"factor,omitempty"`

// Special return values with meaning
IgnoredValues []IgnoredValueDef `yaml:"ignoredValues,omitempty"`
}

// IgnoredValueDef defines an array of special values and the return error
type IgnoredValueDef struct {
Value *float64 `yaml:"value"`
Meaning string `yaml:"meaning"`
}

// Validate semantically validates the given metric definition.
Expand Down
12 changes: 12 additions & 0 deletions modbus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ modules:
dataType: bool
bitOffset: 0
metricType: gauge

- name: "some_special_device_with_buggy_values"
help: "some help for some coil"
address: 301220
dataType: int16
metricType: counter
ignoredValues:
- value: 4294934512
meaning: Data pending, acquisition in progress
- value: 4294934513
meaning: Reserved

16 changes: 16 additions & 0 deletions modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,25 @@ func scrapeMetric(definition config.MetricDef, f modbusFunc, modAddress uint64)
return metric{}, err
}

// Check Value with special meaning from config.yml
for _, iv := range definition.IgnoredValues {
if v == *iv.Value {
return metric{}, &IgnoredValueMeaningError{fmt.Sprintf("value is %v - %s", *iv.Value, iv.Meaning)}
}
}

return metric{definition.Name, definition.Help, definition.Labels, v, definition.MetricType}, nil
}

// IgnoredValueMeaningError is returned whenever a value equals the ignoredValue defined in config.yaml
type IgnoredValueMeaningError struct {
e string
}

func (e *IgnoredValueMeaningError) Error() string {
return fmt.Sprintf("ignored value returned as value: %v", e.e)
}

// InsufficientRegistersError is returned in Parse() whenever not enough
// registers are provided for the given data type.
type InsufficientRegistersError struct {
Expand Down