diff --git a/plugins/inputs/opcua/opcua_client.go b/plugins/inputs/opcua/opcua_client.go index ac7becbe09e4d..7654887387ef2 100644 --- a/plugins/inputs/opcua/opcua_client.go +++ b/plugins/inputs/opcua/opcua_client.go @@ -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 } diff --git a/plugins/inputs/opcua/opcua_client_test.go b/plugins/inputs/opcua/opcua_client_test.go index ffa8521dd05a8..b509d2eaf67a3 100644 --- a/plugins/inputs/opcua/opcua_client_test.go +++ b/plugins/inputs/opcua/opcua_client_test.go @@ -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()) })