-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
480 lines (412 loc) · 10.5 KB
/
config.go
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/NETWAYS/check_akcp_sensorprobeXplus/internal/akcp"
"github.com/NETWAYS/check_akcp_sensorprobeXplus/internal/akcp/sensorProbePlus"
"github.com/NETWAYS/go-check"
"github.com/NETWAYS/go-check/perfdata"
"github.com/NETWAYS/go-check/result"
"github.com/gosnmp/gosnmp"
"github.com/spf13/pflag"
)
type Config struct {
hostname string
snmpVersionParam string
snmpVersion gosnmp.SnmpVersion
community string
port uint16
mode string
device string
deviceType int
sensorPort string
excludeSensorType []string
excludeSensorTypeInteger []uint32
// authProtocol string
// authPassword string
// privProtocol string
// privPassword string
}
// Modes
const (
queryAllSensors uint64 = iota
listPossibleSensors
single
temperaturSensors
humiditySensors
airflowSensors
drycontactSensors
current4to20mA
dcVoltage
acVoltage
waterRope
power
fuel
tankSender
temperatureArray
towerLED
runTestSuccess
)
var modes = map[string]uint64{
"queryAllSensors": queryAllSensors,
"single": single,
"temperatureSensors": temperaturSensors,
"humiditySensors": humiditySensors,
"run_test_success": runTestSuccess,
}
func (c *Config) BindArguments(fs *pflag.FlagSet) {
fs.StringVarP(&c.hostname, "host", "h", "", "Hostname or IP of the targeted device (required)")
fs.StringVarP(&c.snmpVersionParam, "snmp_version", "", "2c", "Version of SNMP to use (1|2c)")
fs.StringVarP(&c.community, "community", "c", "public", "SNMP Community string")
fs.Uint16VarP(&c.port, "port", "p", 161, "SNMP Port")
fs.StringVarP(&c.device, "device", "", "sensorProbe+", `Device type, may be one of:
- sensorProbe
- securityProbe
- sensorProbe+
`)
fs.StringVarP(&c.mode, "mode", "m", "queryAllSensors", `Usage mode (default: queryAllSensors)
Possible modes:
- queryAllSensors: Query all the sensors and show their value and state
- single: Query a single sensor (sensorPort must be set)
- temperatureSensors: Query all the temperature sensors
- humiditySensors: Query all the humidity sensors
The following modes will query the respective sensor types:
- temperature
- humidity_dual
- temperature_dual
- four_20mA
- dcvoltage
- airflow
- dry_inout
- dry_in
- motion
- water
- security
- siren
- acvoltage
- relay
- thermocouple
- smoke
- drycontact_array
- temperature_array
- waterrope
- fuellevel
- tanksender
- five_drycontacts
- irms
- vrms
- watt
- energy
- powerfactor
- reactive
- cbstatus
- handlelock
- air_pressure
- ir_remote
- digital_amp
- digital_watt
- valve_status
- lcd
- buzzer
- tower_led
- pulse_counter
- flow
- edge_counter
- tanklevel_height
- tanklevel_volume
- diff_pressure
- tanklevel_2m
- tanklevel_5m
- tanklevel_10m
- tanklevel_15m
- tanklevel_20m
- thermostat
- virtual
- sound
- software_motion
- board_state
- power_meter
- access
- door
- reader
`)
fs.StringVarP(&c.sensorPort, "sensorPort", "", "", "Sensor Port (required for single mode)")
fs.StringArrayVarP(&c.excludeSensorType, "exclude", "e", nil, "Exclude specific sensor type, valid types are the same as are available for querying above, can be used multiple times")
}
func (c *Config) Validate() error {
val, ok := modes[c.mode]
if !ok { // nolint: gocritic,nestif
if c.device == "sensorProbePlus" {
_, ok = sensorProbePlus.SensorsTypes[c.mode]
if !ok {
return errors.New("mode is not a valid value")
}
}
} else if val == runTestSuccess {
check.ExitRaw(0, "It seems like you can execute this programm")
} else if val == single && c.sensorPort == "" {
return errors.New("no sensorPort was given")
}
switch c.snmpVersionParam {
case "1":
c.snmpVersion = gosnmp.Version1
case "2c":
c.snmpVersion = gosnmp.Version2c
case "3":
return errors.New("SNMP v3 not yet implemented")
default:
return errors.New("invalid SNMP version string")
}
switch c.device {
case "sensorProbe":
{
// TODO
return errors.New("not yet implemented")
}
case "securityProbe":
{
// TODO
return errors.New("not yet implemented")
}
case "sensorProbe+":
{
c.deviceType = akcp.SensorProbePlusType
}
default:
{
return errors.New("invalid device type")
}
}
if c.excludeSensorType == nil {
c.excludeSensorType = append(c.excludeSensorType, "buzzer")
}
if c.excludeSensorType[0] != "" {
for _, tmp := range c.excludeSensorType {
if tmp == "" {
continue
}
val, err := akcp.GetSensorTypeInt(tmp, c.deviceType)
if err != nil {
return err
}
c.excludeSensorTypeInteger = append(c.excludeSensorTypeInteger, val)
}
}
return nil
}
func (c *Config) Run(overall *result.Overall) (err error) {
timeout, err := time.ParseDuration("30s")
if err != nil {
check.ExitError(err)
}
params := &gosnmp.GoSNMP{
Target: c.hostname,
Port: c.port,
Community: c.community,
Version: c.snmpVersion,
Timeout: timeout,
Retries: 3,
}
err = params.Connect()
if err != nil {
check.ExitError(err)
}
defer params.Conn.Close()
// Get name, location and type
query, err := params.Get([]string{".1.3.6.1.4.1.3854.3.2.1.9.0", ".1.3.6.1.4.1.3854.3.2.1.10.0", ".1.3.6.1.4.1.3854.3.2.1.8.0"})
if err != nil {
check.ExitError(err)
}
name := query.Variables[0].Value
location := query.Variables[1].Value
devType := query.Variables[2].Value
overall.Summary = fmt.Sprintf("Device %s at location %s (%s)", name, location, devType)
val, ok := modes[c.mode]
if !ok { //nolint:nestif
// not one of the main modes, look for specifics
if c.deviceType == akcp.SensorProbePlusType {
val, ok := sensorProbePlus.SensorsTypes[c.mode]
if !ok {
return errors.New("mode is not a valid value")
}
err = querySensorByType(params, c, overall, c.deviceType, uint64(val))
if err != nil {
check.ExitError(err)
}
return nil
}
// TODO
return errors.New("device not yet implemented")
}
switch val {
case queryAllSensors:
err = queryAllSensorsMode(params, c, overall, c.deviceType)
if err != nil {
check.ExitError(err)
}
return nil
case temperaturSensors:
err = queryTemperatureSensors(params, c, overall, c.deviceType)
if err != nil {
check.ExitError(err)
}
return nil
case humiditySensors:
err = queryHumiditySensors(params, c, overall, c.deviceType)
if err != nil {
check.ExitError(err)
}
return nil
default:
return errors.New("not yet implemented")
}
}
// nolint: gocognit
func queryAllSensorsMode(params *gosnmp.GoSNMP, c *Config, overall *result.Overall, deviceType int) (err error) {
sensors, err := akcp.QuerySensorList(params, deviceType) // Get all sensors
if err != nil {
check.ExitError(err)
}
for _, sensor := range sensors {
details, err := akcp.QuerySensorDetails(params, sensor, deviceType)
if err != nil {
check.ExitError(err)
}
exclude := false
for _, excludedType := range c.excludeSensorTypeInteger {
if uint64(excludedType) == details.SensorType {
exclude = true
}
}
if exclude {
continue
}
if details.SensorType == sensorProbePlus.Temperature ||
details.SensorType == sensorProbePlus.Temperature_dual {
tempSensors, err := akcp.QueryTemperatureTable(params, deviceType)
if err != nil {
return err
}
for _, tempSensor := range tempSensors {
if details.Name == tempSensor.Name {
details.Warning = tempSensor.Warning
details.Critical = tempSensor.Critical
}
}
}
if details.SensorType == sensorProbePlus.Humidity_dual {
humiSensors, err := akcp.QueryHumidityTable(params, deviceType)
if err != nil {
return err
}
for _, humSensor := range humiSensors {
if details.Name == humSensor.Name {
details.Warning = humSensor.Warning
details.Critical = humSensor.Critical
}
}
}
err = mapSensorStatus(details, overall)
if err != nil {
return err
}
}
return nil
}
func mapSensorStatus(sensor akcp.SensorDetails, overall *result.Overall) error {
var sensorString string
if sensor.SensorType == sensorProbePlus.Motion {
sensorString = fmt.Sprintf("%s: %s", sensor.Name, sensor.Description)
} else {
sensorString = fmt.Sprintf("%s: %.1f", sensor.Name, sensor.Value)
}
unit := ""
if sensor.Unit == "C" {
unit = "℃"
} else if sensor.Unit != "" {
unit += sensor.Unit
}
sensorString += unit
var pf perfdata.Perfdata
pf.Label = sensor.Name
pf.Value = sensor.Value
if sensor.Warning.Present {
pf.Warn = &sensor.Warning.Val
}
if sensor.Critical.Present {
pf.Crit = &sensor.Critical.Val
}
if sensor.Unit != "C" {
pf.Uom = strings.ToLower(sensor.Unit)
} else {
pf.Uom = "C"
}
sc := result.PartialResult{}
_ = sc.SetDefaultState(check.Unknown)
sc.Output = sensorString
switch sensor.Status {
case akcp.Normal:
_ = sc.SetState(check.OK)
sc.Perfdata.Add(&pf)
case akcp.HighWarning, akcp.LowWarning:
_ = sc.SetState(check.Warning)
sc.Perfdata.Add(&pf)
case akcp.HighCritical, akcp.LowCritical:
_ = sc.SetState(check.Critical)
sc.Perfdata.Add(&pf)
case akcp.SensorError:
_ = sc.SetState(check.Critical)
sc.Output = sensor.Name + " ERROR!"
sc.Perfdata.Add(&pf)
case akcp.NoStatus:
_ = sc.SetState(check.Unknown)
sc.Output = sensor.Name + " is unknown (No Status)!"
}
overall.AddSubcheck(sc)
return nil
}
func querySensorByType(params *gosnmp.GoSNMP, _ *Config, overall *result.Overall, deviceType int, sensorType uint64) error { //nolint:unparam
sensors, err := akcp.QuerySensorList(params, deviceType) // Get all sensors
if err != nil {
check.ExitError(err)
}
for _, sensor := range sensors {
details, err := akcp.QuerySensorDetails(params, sensor, deviceType)
if err != nil {
check.ExitError(err)
}
if details.SensorType == sensorType {
err = mapSensorStatus(details, overall)
if err != nil {
check.ExitError(err)
}
}
}
return nil
}
func queryTemperatureSensors(params *gosnmp.GoSNMP, _ *Config, overall *result.Overall, deviceType int) (err error) {
sensors, _ := akcp.QueryTemperatureTable(params, deviceType) // Get all sensors
// TODO: Error Handling
for _, details := range sensors {
err = mapSensorStatus(details, overall)
if err != nil {
return err
}
}
return nil
}
func queryHumiditySensors(params *gosnmp.GoSNMP, _ *Config, overall *result.Overall, deviceType int) (err error) {
sensors, err := akcp.QueryHumidityTable(params, deviceType) // Get all sensors
if err != nil {
check.ExitError(err)
}
for _, sensor := range sensors {
err = mapSensorStatus(sensor, overall)
if err != nil {
return err
}
}
return nil
}