-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathnodevalue.go
More file actions
393 lines (348 loc) · 11.8 KB
/
nodevalue.go
File metadata and controls
393 lines (348 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package nodevalue
import (
"time"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
"github.com/elastic/beats/v7/metricbeat/mb"
"context"
"errors"
_ "fmt"
"math"
"reflect"
"golang.org/x/sync/semaphore"
)
// init registers the MetricSet with the central registry as soon as the program
// starts. The New function will be called later to instantiate an instance of
// the MetricSet for each host defined in the module's configuration. After the
// MetricSet has been created then Fetch will begin to be called periodically.
func init() {
mb.Registry.MustAddMetricSet("opcua", "nodevalue", New)
}
// MetricSet holds any configuration or state information. It must implement
// the mb.MetricSet interface. And this is best achieved by embedding
// mb.BaseMetricSet because it implements all of the required mb.MetricSet
// interface methods except for Fetch.
type MetricSet struct {
mb.BaseMetricSet
Endpoint string `config:"endpoint"`
Nodes []Node `config:"nodes"`
Browse Browse `config:"browse"`
RetryOnErrorCount int `config:"retryOnError"`
MaxThreads int `config:"maxThreads"`
MaxTriesToReconnect int `config:"maxTriesToReconnect"`
Subscribe bool `config:"subscribe"`
Subscription Subscription `config:"subscription"`
Monitoring Monitoring `config:"monitoring"`
Username string `config:"username"`
Password string `config:"password"`
Policy string `config:"policy"`
Mode string `config:"securityMode"`
ClientCert string `config:"clientCert"`
ClientKey string `config:"clientKey"`
AppName string `config:"appName"`
Client Client
LegacyFields bool `config:"legacyFields"`
ECSFields bool `config:"ECSFields"`
Debug bool `config:"debug"`
}
type Browse struct {
Enabled bool `config:"enabled"`
MaxLevel int `config:"maxLevel"`
MaxNodePerParent int `config:"maxNodePerParent"`
}
type Subscription struct {
PublishInterval int `config:"publishInterval"`
LifeTimeCount uint32 `config:"lifeTimeCount"`
MaxKeepAliveCount uint32 `config:"maxKeepAliveCount"`
MaxNotificationsPerPublish uint32 `config:"maxNotificationsPerPublish"`
Priority uint8 `config:"priority"`
}
type Monitoring struct {
QueueSize uint32 `config:"queueSize"`
SamplingInterval float64 `config:"samplingInterval"`
Filter Filter `config:"filter"`
}
type Filter struct {
DataChangeTrigger string `config:"dataChangeTrigger"`
DeadbandType string `config:"deadbandType"`
DeadbandValue float64 `config:"deadbandValue"`
}
var subscriptionDefaults = Subscription{
PublishInterval: 10,
}
var monitoringDefaults = Monitoring{
Filter: filterDefaults,
QueueSize: 10,
SamplingInterval: 1.0,
}
var filterDefaults = Filter{
DataChangeTrigger: "none",
}
var browseDefaults = Browse{
Enabled: true,
MaxLevel: 0,
MaxNodePerParent: 0,
}
var clientDefaults = Client{
connected: false,
}
var DefaultConfig = MetricSet{
Endpoint: "opc.tcp://localhost:4840",
RetryOnErrorCount: 5,
MaxThreads: 50,
Subscribe: true,
Policy: "None",
Mode: "None",
Username: "",
Password: "",
ClientCert: "",
ClientKey: "",
AppName: "machinebeat",
Nodes: []Node{},
Browse: browseDefaults,
MaxTriesToReconnect: 5,
Client: clientDefaults,
LegacyFields: false,
ECSFields: true,
Debug: false,
Subscription: subscriptionDefaults,
Monitoring: monitoringDefaults,
}
// New creates a new instance of the MetricSet. New is responsible for unpacking
// any MetricSet specific configuration options if there are any.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The OPCUA metricset is beta.")
config := DefaultConfig
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
metricset := &MetricSet{
BaseMetricSet: base,
Endpoint: config.Endpoint,
RetryOnErrorCount: config.RetryOnErrorCount,
MaxThreads: config.MaxThreads,
Subscribe: config.Subscribe,
Username: config.Username,
Password: config.Password,
Policy: config.Policy,
Mode: config.Mode,
ClientCert: config.ClientCert,
ClientKey: config.ClientKey,
AppName: config.AppName,
Nodes: config.Nodes,
Browse: config.Browse,
MaxTriesToReconnect: config.MaxTriesToReconnect,
Client: config.Client,
LegacyFields: config.LegacyFields,
ECSFields: config.ECSFields,
Debug: config.Debug,
Subscription: config.Subscription,
Monitoring: config.Monitoring,
}
metricset.Client.counter = metricset.MaxTriesToReconnect
metricset.Client.config = metricset
_, err := establishConnection(metricset, 1)
if err != nil {
return nil, err
}
//Check if browsing is activated in general.
// If yes the collection will be started after browsing
// If no the collection will be started with the configured nodes directly
if metricset.Browse.Enabled {
logp.Info("Browsing is enabled. Data collection will start after discovery. Based on your server and browsing configuration this can take some time.")
//Implements the browsing service of OPC UA.
metricset.Client.startBrowse()
logp.Debug("Browse", "Nodes to collect data from")
for _, nodeConfig := range metricset.Client.nodesToCollect {
logp.Debug("Browse", "Node: %v", nodeConfig.ID)
}
logp.Info("Browsing finished")
} else {
//If browsing is disabled we will collect directly from the configured nodes
for i := range metricset.Nodes {
metricset.Client.nodesToCollect = append(metricset.Client.nodesToCollect, &metricset.Nodes[i])
}
err := metricset.Client.appendNodeInformation()
if err != nil {
return nil, err
}
}
if len(metricset.Client.nodesToCollect) == 0 {
logp.Info("Found 0 nodes to collect data from.")
} else {
if metricset.Subscribe {
metricset.Client.startSubscription()
} else {
metricset.Client.sem = semaphore.NewWeighted(int64(metricset.MaxThreads))
}
}
return metricset, nil
}
func establishConnection(config *MetricSet, retryCounter int) (bool, error) {
for i := retryCounter; i > 0; i-- {
newConnection, err := config.Client.connect()
if err == nil {
return newConnection, err
}
logp.Error(err)
time.Sleep(1 * time.Second)
}
logp.Critical("[OPCUA] Tried to connect to OPC UA server %v time(s). Without success.", retryCounter)
return false, errors.New("Connection was not possible")
}
func collect(m *MetricSet, report mb.ReporterV2) error {
logp.Debug("Collector", "Event collector instance started")
defer func() {
if r := recover(); r != nil {
logp.Info("Recovered from panic. The beat will reconnect now")
logp.Info("Panic message: %v", r)
m.Client.closeConnection()
}
}()
data, err := m.Client.collectData()
if err != nil {
logp.Info("error: %v", err)
logp.Error(err)
m.Client.closeConnection()
return err
}
publishResponses(data, report, m)
logp.Debug("Collector", "Event collector instance finished sucessfully.")
return nil
}
func handleCounter(eventCount int, resetValue int, config *MetricSet) {
if eventCount == 0 {
config.Client.counter = config.Client.counter - 1
if config.Client.counter == 0 {
logp.Info("[OPCUA] Too much zero publish attempts.")
config.Client.closeConnection()
}
} else {
config.Client.counter = resetValue
}
}
func publishResponses(data []*ResponseObject, report mb.ReporterV2, config *MetricSet) {
logp.Info("[OPCUA] Publishing %v new events", len(data))
handleCounter(len(data), config.MaxTriesToReconnect, config)
for _, response := range data {
var mbEvent mb.Event
event := make(mapstr.M)
module := make(mapstr.M)
root := make(mapstr.M)
//Publish the event with the legacy field schema
if config.LegacyFields {
if response.value.Status == 0 {
event.Put("state", "OK")
} else {
event.Put("state", "ERROR")
}
event.Put("created", response.value.SourceTimestamp.String())
if response.value.Value != nil {
if response.node.DataType != "" {
if response.node.DataType == "float64" {
if !isArray(response.value.Value.Value()) {
if !math.IsNaN(response.value.Value.Value().(float64)) {
root.Put(response.node.DataType, response.value.Value.Value())
}
}
} else {
root.Put(response.node.DataType, response.value.Value.Value())
}
} else {
event.Put("value", response.value.Value.Value())
}
}
module.Put("node", response.node)
module.Put("endpoint", config.Endpoint)
}
//Publish the event with ECS field schema
if config.ECSFields {
root.Put("event.provider", "opcua")
root.Put("event.url", config.Endpoint)
root.Put("event.creation", time.Now())
root.Put("event.dataset", response.node.Path)
root.Put("sensor.id", response.node.ID)
root.Put("sensor.name", response.node.Name)
root.Put("sensor.label", response.node.Label)
root.Put("value.source_timestamp", response.value.SourceTimestamp.String())
if response.value.Value != nil {
if response.node.DataType != "" {
root.Put("value.datatype", response.node.DataType)
if response.node.DataType == "float64" {
if !isArray(response.value.Value.Value()) {
if !math.IsNaN(response.value.Value.Value().(float64)) {
root.Put("value.value_"+response.node.DataType, response.value.Value.Value())
}
}
} else {
root.Put("value.value_"+response.node.DataType, response.value.Value.Value())
}
} else {
root.Put("value.value", response.value.Value.Value())
}
}
}
mbEvent.RootFields = root
mbEvent.ModuleFields = module
mbEvent.MetricSetFields = event
report.Event(mbEvent)
}
}
// Fetch methods implements the data gathering and data conversion to the right
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
if m.Client.connected {
if m.Subscribe {
var data []*ResponseObject
for {
select {
case response := <-m.Client.subscription:
data = append(data, response)
default:
publishResponses(data, report, m)
return nil
}
}
} else {
ctx := context.Background()
if err := m.Client.sem.Acquire(ctx, 1); err != nil {
logp.Err("[OPCUA] Max threads reached. This means that it takes too long to get the data from your OPC UA server. You should consider to increase the max Thread counter or the period of getting the data.")
} else {
go func() {
collect(m, report)
m.Client.sem.Release(1)
}()
}
}
} else {
//It seems that there was an error, we will try to reconnect
logp.Info("[OPCUA] Lets wait a while before reconnect happens")
time.Sleep(5 * time.Second)
_, err := establishConnection(m, m.RetryOnErrorCount)
if err != nil {
logp.Info("[OPCUA] Reconnect was not successful")
return err
}
if m.Subscribe {
m.Client.startSubscription()
}
}
return nil
}
func isArray(v interface{}) bool {
rt := reflect.TypeOf(v)
switch rt.Kind() {
case reflect.Slice:
//fmt.Println(v, "is a slice with element type", rt.Elem())
return true
case reflect.Array:
//fmt.Println(v, "is an array with element type", rt.Elem())
return true
default:
//fmt.Println(v, "is something else entirely")
return false
}
return false
}