diff --git a/config/device-classes/generic.yaml b/config/device-classes/generic.yaml index b736f9f..673bad8 100644 --- a/config/device-classes/generic.yaml +++ b/config/device-classes/generic.yaml @@ -146,4 +146,11 @@ components: dot3HCStatsFCSErrors: oid: 1.3.6.1.2.1.10.7.11.1.2 etherStatsCRCAlignErrors: - oid: 1.3.6.1.2.1.16.1.1.1.8 \ No newline at end of file + oid: 1.3.6.1.2.1.16.1.1.1.8 + indices_mapping: + oid: 1.3.6.1.2.1.16.1.1.1.2 + operators: + - type: modify + modify_method: regexSubmatch + regex: '\.([0-9]+)$' + format: "$1" diff --git a/core/communicator/device_class.go b/core/communicator/device_class.go index e10c62b..d7c31b9 100644 --- a/core/communicator/device_class.go +++ b/core/communicator/device_class.go @@ -276,7 +276,8 @@ type yamlComponentsInterfaces struct { type yamlComponentsOID struct { network.SNMPGetConfiguration `yaml:",inline" mapstructure:",squash"` - Operators []interface{} `yaml:"operators"` + Operators []interface{} `yaml:"operators"` + IndicesMapping *yamlComponentsOID `yaml:"indices_mapping" mapstructure:"indices_mapping"` } var genericDeviceClass struct { @@ -720,6 +721,15 @@ func (y *yamlComponentsInterfaces) convert(parentComponentsInterfaces *deviceCla } func (y *yamlComponentsOID) convert() (deviceClassOID, error) { + var idxMappings *deviceClassOID + if y.IndicesMapping != nil { + mappings, err := y.IndicesMapping.convert() + if err != nil { + return deviceClassOID{}, errors.New("failed to convert indices mappings") + } + idxMappings = &mappings + } + if y.Operators != nil { operators, err := interfaceSlice2propertyOperators(y.Operators, propertyDefault) if err != nil { @@ -730,7 +740,8 @@ func (y *yamlComponentsOID) convert() (deviceClassOID, error) { OID: y.OID, UseRawResult: y.UseRawResult, }, - operators: operators, + operators: operators, + indicesMapping: idxMappings, }, nil } @@ -739,7 +750,8 @@ func (y *yamlComponentsOID) convert() (deviceClassOID, error) { OID: y.OID, UseRawResult: y.UseRawResult, }, - operators: nil, + operators: nil, + indicesMapping: idxMappings, }, nil } diff --git a/core/communicator/device_class_group_properties.go b/core/communicator/device_class_group_properties.go index e7a497f..5922e81 100644 --- a/core/communicator/device_class_group_properties.go +++ b/core/communicator/device_class_group_properties.go @@ -136,7 +136,8 @@ func (d *deviceClassOIDs) merge(overwrite deviceClassOIDs) deviceClassOIDs { type deviceClassOID struct { network.SNMPGetConfiguration - operators propertyOperators + operators propertyOperators + indicesMapping *deviceClassOID } func (d *deviceClassOID) readOID(ctx context.Context) (map[int]interface{}, error) { @@ -179,5 +180,33 @@ func (d *deviceClassOID) readOID(ctx context.Context) (map[int]interface{}, erro result[index] = resNormalized } } + + //change indices if necessary + if d.indicesMapping != nil { + indices, err := d.indicesMapping.readOID(ctx) + if err != nil { + return nil, errors.Wrap(err, "failed to read indices") + } + mappedResult := make(map[int]interface{}) + + for k, v := range result { + var idx int + if _, ok := indices[k]; ok { + idx, err = indices[k].(value.Value).Int() + if err != nil { + return nil, errors.Wrap(err, "failed to convert Value to int") + } + } else { + idx = k + } + + if _, ok := mappedResult[idx]; ok { + return nil, fmt.Errorf("index mappings resulted in duplicated index '%d'", idx) + } + + mappedResult[idx] = v + } + result = mappedResult + } return result, nil }