Skip to content

Commit

Permalink
Do not skip good quality nodes after a bad quality node is encountered (
Browse files Browse the repository at this point in the history
  • Loading branch information
R290 authored Jul 30, 2021
1 parent 9fcd5a5 commit 3633853
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
28 changes: 16 additions & 12 deletions plugins/inputs/opcua/opcua_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 8 additions & 2 deletions plugins/inputs/opcua/opcua_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -16,7 +17,7 @@ type OPCTags struct {
Namespace string
IdentifierType string
Identifier string
Want string
Want interface{}
}

func TestClient1Integration(t *testing.T) {
Expand All @@ -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
Expand All @@ -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))
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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())
})
Expand Down

0 comments on commit 3633853

Please sign in to comment.