Do not skip good quality nodes after a bad quality node is encountered (#9550)

This commit is contained in:
R290 2021-07-30 22:26:47 +02:00 committed by GitHub
parent 9fcd5a5b54
commit 3633853235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 14 deletions

View File

@ -31,6 +31,7 @@ type OpcUA struct {
RequestTimeout config.Duration `toml:"request_timeout"`
RootNodes []NodeSettings `toml:"nodes"`
Groups []GroupSettings `toml:"group"`
Log telegraf.Logger `toml:"-"`
nodes []Node
nodeData []OPCData
@ -470,15 +471,16 @@ func (o *OpcUA) getData() error {
}
o.ReadSuccess.Incr(1)
for i, d := range resp.Results {
o.nodeData[i].Quality = d.Status
if d.Status != ua.StatusOK {
return fmt.Errorf("status not OK: %v", d.Status)
o.Log.Errorf("status not OK for node %v: %v", o.nodes[i].tag.FieldName, d.Status)
continue
}
o.nodeData[i].TagName = o.nodes[i].tag.FieldName
if d.Value != nil {
o.nodeData[i].Value = d.Value.Value()
o.nodeData[i].DataType = d.Value.Type()
}
o.nodeData[i].Quality = d.Status
o.nodeData[i].TimeStamp = d.ServerTimestamp.String()
o.nodeData[i].Time = d.SourceTimestamp.String()
}
@ -532,17 +534,19 @@ func (o *OpcUA) Gather(acc telegraf.Accumulator) error {
}
for i, n := range o.nodes {
fields := make(map[string]interface{})
tags := map[string]string{
"id": n.idStr,
}
for k, v := range n.metricTags {
tags[k] = v
}
if o.nodeData[i].Quality == ua.StatusOK {
fields := make(map[string]interface{})
tags := map[string]string{
"id": n.idStr,
}
for k, v := range n.metricTags {
tags[k] = v
}
fields[o.nodeData[i].TagName] = o.nodeData[i].Value
fields["Quality"] = strings.TrimSpace(fmt.Sprint(o.nodeData[i].Quality))
acc.AddFields(n.metricName, fields, tags)
fields[o.nodeData[i].TagName] = o.nodeData[i].Value
fields["Quality"] = strings.TrimSpace(fmt.Sprint(o.nodeData[i].Quality))
acc.AddFields(n.metricName, fields, tags)
}
}
return nil
}

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -16,7 +17,7 @@ type OPCTags struct {
Namespace string
IdentifierType string
Identifier string
Want string
Want interface{}
}
func TestClient1Integration(t *testing.T) {
@ -28,6 +29,8 @@ func TestClient1Integration(t *testing.T) {
{"ProductName", "0", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
{"ManufacturerName", "0", "i", "2263", "open62541"},
{"badnode", "1", "i", "1337", nil},
{"goodnode", "1", "s", "the.answer", "42"},
}
var o OpcUA
@ -40,6 +43,8 @@ func TestClient1Integration(t *testing.T) {
o.RequestTimeout = config.Duration(1 * time.Second)
o.SecurityPolicy = "None"
o.SecurityMode = "None"
o.Log = testutil.Logger{}
for _, tags := range testopctags {
o.RootNodes = append(o.RootNodes, MapOPCTag(tags))
}
@ -60,7 +65,7 @@ func TestClient1Integration(t *testing.T) {
if compare != testopctags[i].Want {
t.Errorf("Tag %s: Values %v for type %s does not match record", o.nodes[i].tag.FieldName, value.Interface(), types)
}
} else {
} else if testopctags[i].Want != nil {
t.Errorf("Tag: %s has value: %v", o.nodes[i].tag.FieldName, v.Value)
}
}
@ -250,6 +255,7 @@ func TestValidateOPCTags(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
o := OpcUA{
nodes: tt.nodes,
Log: testutil.Logger{},
}
require.Equal(t, tt.err, o.validateOPCTags())
})