-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
488 lines (423 loc) · 11.4 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
481
482
483
484
485
486
487
488
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
snmp_version_param string
snmp_version gosnmp.SnmpVersion
community string
port uint16
mode string
device string
device_type int
sensorPort string
excludeSensorType []string
excludeSensorType_int []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,
}
//var default_excluded_types []string
func (c *Config) BindArguments(fs *pflag.FlagSet) {
fs.StringVarP(&c.hostname, "host", "h", "", "Hostname or IP of the targeted device (required)")
fs.StringVarP(&c.snmp_version_param, "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 {
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")
}
}
if c.snmp_version_param == "1" {
c.snmp_version = gosnmp.Version1
} else if c.snmp_version_param == "2c" {
c.snmp_version = gosnmp.Version2c
} else if c.snmp_version_param == "3" {
return errors.New("SNMP v3 not yet implemented")
//c.snmp_version = gosnmp.Version3
} else {
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.device_type = akcp.SensorProbePlus_type
}
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.device_type)
if err != nil {
return err
}
c.excludeSensorType_int = append(c.excludeSensorType_int, 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.snmp_version,
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 {
// not one of the main modes, look for specifics
if c.device_type == akcp.SensorProbePlus_type {
val, ok := sensorProbePlus.SensorsTypes[c.mode]
if !ok {
return errors.New("Mode is not a valid value")
} else {
err = querySensorByType(params, c, overall, c.device_type, uint64(val))
if err != nil {
check.ExitError(err)
}
return nil
}
} else {
// TODO
return errors.New("Device not yet implemented.")
}
} else {
if val == queryAllSensors {
err = queryAllSensorsMode(params, c, overall, c.device_type)
if err != nil {
check.ExitError(err)
}
return nil
} else if val == temperaturSensors {
err = queryTemperatureSensors(params, c, overall, c.device_type)
if err != nil {
check.ExitError(err)
}
return nil
} else if val == humiditySensors {
err = queryHumiditySensors(params, c, overall, c.device_type)
if err != nil {
check.ExitError(err)
}
return nil
} else {
// TODO
return errors.New("Not yet implemented.")
}
}
}
func queryAllSensorsMode(params *gosnmp.GoSNMP, c *Config, overall *result.Overall, device_type int) (err error) {
sensors, err := akcp.QuerySensorList(params, device_type) // Get all sensors
if err != nil {
check.ExitError(err)
}
for _, sensor := range sensors {
details, err := akcp.QuerySensorDetails(params, sensor, device_type)
if err != nil {
check.ExitError(err)
}
var exclude bool = false
for _, excluded_type := range c.excludeSensorType_int {
if uint64(excluded_type) == details.SensorType {
exclude = true
}
}
if exclude {
continue
}
if details.SensorType == sensorProbePlus.Temperature ||
details.SensorType == sensorProbePlus.Temperature_dual {
tempSensors, err := akcp.QueryTemperatureTable(params, device_type)
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, device_type)
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
if sensor.Status == 2 {
_ = sc.SetState(check.OK)
sc.Perfdata.Add(&pf)
} else if sensor.Status == 3 || sensor.Status == 5 {
// if sensor.Warning.Present && (float64(sensor.Value) <= sensor.Warning.Val.Lower) {
// sensorString += fmt.Sprintf(" is lower than warning threshold %.1f%s", sensor.Warning.Val.Lower, unit)
// } else if sensor.Warning.Present && (float64(sensor.Value) >= sensor.Warning.Val.Upper) {
// sensorString += fmt.Sprintf(" is higher than warning threshold %.1f%s", sensor.Warning.Val.Upper, unit)
// }
_ = sc.SetState(check.Warning)
sc.Perfdata.Add(&pf)
} else if sensor.Status == 6 || sensor.Status == 4 {
// if sensor.Critical.Present && (float64(sensor.Value) <= sensor.Critical.Val.Lower) {
// sensorString += fmt.Sprintf(" is lower than critical threshold %.1f%s", sensor.Critical.Val.Lower, unit)
// } else if sensor.Critical.Present && (float64(sensor.Value) >= sensor.Critical.Val.Upper) {
// sensorString += fmt.Sprintf(" is higher than critical threshold %.1f%s", sensor.Critical.Val.Upper, unit)
// }
_ = sc.SetState(check.Critical)
sc.Perfdata.Add(&pf)
} else if sensor.Status == 7 {
_ = sc.SetState(check.Critical)
sc.Output = sensor.Name + " ERROR!"
sc.Perfdata.Add(&pf)
}
overall.AddSubcheck(sc)
return nil
}
func querySensorByType(params *gosnmp.GoSNMP, c *Config, overall *result.Overall, device_type int, sensor_type uint64) (err error) {
sensors, err := akcp.QuerySensorList(params, device_type) // Get all sensors
if err != nil {
check.ExitError(err)
}
for _, sensor := range sensors {
details, err := akcp.QuerySensorDetails(params, sensor, device_type)
if err != nil {
check.ExitError(err)
}
if details.SensorType == uint64(sensor_type) {
err = mapSensorStatus(details, overall)
if err != nil {
check.ExitError(err)
}
}
}
return nil
}
func queryTemperatureSensors(params *gosnmp.GoSNMP, c *Config, overall *result.Overall, device_type int) (err error) {
sensors, _ := akcp.QueryTemperatureTable(params, device_type) // 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, c *Config, overall *result.Overall, device_type int) (err error) {
sensors, err := akcp.QueryHumidityTable(params, device_type) // Get all sensors
if err != nil {
check.ExitError(err)
}
for _, sensor := range sensors {
err = mapSensorStatus(sensor, overall)
if err != nil {
return err
}
}
return nil
}