-
Notifications
You must be signed in to change notification settings - Fork 0
/
fortigate.go
382 lines (314 loc) · 11 KB
/
fortigate.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
package codecommunicator
import (
"context"
"fmt"
"github.com/RincewindsHat/thola/internal/device"
"github.com/RincewindsHat/thola/internal/network"
"github.com/RincewindsHat/thola/internal/value"
"github.com/pkg/errors"
"regexp"
)
type fortigateCommunicator struct {
codeCommunicator
}
type fortigateSensorData struct {
Name *string
Value value.Value
State *device.HardwareHealthComponentState
}
func (c *fortigateCommunicator) GetHardwareHealthComponentFans(ctx context.Context) ([]device.HardwareHealthComponentFan, error) {
regex, err := regexp.Compile(`Fan\s`)
if err != nil {
return nil, errors.New("invalid regular expression")
}
sensors, err := c.getHardwareHealthComponentReadOutSensors(ctx, regex)
if err != nil {
return nil, errors.Wrap(err, "failed to read out fan sensors")
}
var fans []device.HardwareHealthComponentFan
for _, sensor := range sensors {
var fan device.HardwareHealthComponentFan
if sensor.Name != nil {
fan.Description = sensor.Name
}
if sensor.State != nil {
fan.State = sensor.State
}
fans = append(fans, fan)
}
return fans, nil
}
func (c *fortigateCommunicator) GetHardwareHealthComponentTemperature(ctx context.Context) ([]device.HardwareHealthComponentTemperature, error) {
regex, err := regexp.Compile(`Temp|LM75|((TD|TR)\d+)|(DTS\d+)`)
if err != nil {
return nil, errors.New("invalid regular expression")
}
sensors, err := c.getHardwareHealthComponentReadOutSensors(ctx, regex)
if err != nil {
return nil, errors.Wrap(err, "failed to read out fan sensors")
}
var temps []device.HardwareHealthComponentTemperature
for _, sensor := range sensors {
var temp device.HardwareHealthComponentTemperature
if sensor.Name != nil {
temp.Description = sensor.Name
}
if sensor.State != nil {
temp.State = sensor.State
}
if sensor.Value != nil {
fl, err := sensor.Value.Float64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse temperature as float64")
}
temp.Temperature = &fl
}
temps = append(temps, temp)
}
return temps, nil
}
func (c *fortigateCommunicator) GetHardwareHealthComponentVoltage(ctx context.Context) ([]device.HardwareHealthComponentVoltage, error) {
regex, err := regexp.Compile(`(VOUT)|(VIN)|(VCC)|(P\d+V\d+)|(_\d+V\d+_)|(DDR)|(VCORE)|(DVDD)`)
if err != nil {
return nil, errors.New("invalid regular expression")
}
sensors, err := c.getHardwareHealthComponentReadOutSensors(ctx, regex)
if err != nil {
return nil, errors.Wrap(err, "failed to read out voltage sensors")
}
var voltage []device.HardwareHealthComponentVoltage
for _, sensor := range sensors {
var vol device.HardwareHealthComponentVoltage
if sensor.Name != nil {
vol.Description = sensor.Name
}
if sensor.State != nil {
vol.State = sensor.State
}
if sensor.Value != nil {
fl, err := sensor.Value.Float64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse voltage as float64")
}
vol.Voltage = &fl
}
voltage = append(voltage, vol)
}
return voltage, nil
}
func (c *fortigateCommunicator) GetHardwareHealthComponentPowerSupply(ctx context.Context) ([]device.HardwareHealthComponentPowerSupply, error) {
regex, err := regexp.Compile(`PS.*Status`)
if err != nil {
return nil, errors.New("invalid regular expression")
}
sensors, err := c.getHardwareHealthComponentReadOutSensors(ctx, regex)
if err != nil {
return nil, errors.Wrap(err, "failed to read out power supply sensors")
}
var powerSupply []device.HardwareHealthComponentPowerSupply
for _, sensor := range sensors {
var ps device.HardwareHealthComponentPowerSupply
if sensor.Name != nil {
ps.Description = sensor.Name
}
if sensor.State != nil {
ps.State = sensor.State
}
powerSupply = append(powerSupply, ps)
}
return powerSupply, nil
}
func (c *fortigateCommunicator) getHardwareHealthComponentReadOutSensors(ctx context.Context, regex *regexp.Regexp) ([]fortigateSensorData, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("no device connection available")
}
var sensors []fortigateSensorData
sensorNameOID := network.OID("1.3.6.1.4.1.12356.101.4.3.2.1.2")
valueOID := network.OID("1.3.6.1.4.1.12356.101.4.3.2.1.3")
alarmOID := network.OID("1.3.6.1.4.1.12356.101.4.3.2.1.4")
sensorResults, err := con.SNMP.SnmpClient.SNMPWalk(ctx, sensorNameOID)
if err != nil {
return nil, errors.Wrap(err, "failed to read out sensors oid")
}
for _, sensorResult := range sensorResults {
v, err := sensorResult.GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get value of oid response")
}
sensorName := v.String()
if regex != nil && !regex.MatchString(sensorName) {
continue
}
var sensor fortigateSensorData
sensor.Name = &sensorName
oid := sensorResult.GetOID()
subTree, err := oid.GetIndexAfterOID(sensorNameOID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get subtree of oid %s", sensorResult.GetOID())
}
// get value
resValue, err := con.SNMP.SnmpClient.SNMPGet(ctx, valueOID.AddIndex(subTree))
if err != nil {
return nil, errors.Wrapf(err, "failed to read out value of sensor %s", sensorName)
}
if len(resValue) > 0 {
v, err := resValue[0].GetValue()
if err != nil {
return nil, errors.Wrapf(err, "failed to get value for sensor %s", sensorName)
}
sensor.Value = v
}
// get alarm
resAlarm, err := con.SNMP.SnmpClient.SNMPGet(ctx, alarmOID.AddIndex(subTree))
// alarm is not mandatory and does not exist for every sensor
if err == nil && len(resAlarm) > 0 {
alarm, err := resAlarm[0].GetValue()
if err != nil {
return nil, errors.Wrapf(err, "failed to get alarm for sensor %s", sensorName)
}
switch alarm.String() {
case "0":
state := device.HardwareHealthComponentStateNormal
sensor.State = &state
case "1":
state := device.HardwareHealthComponentStateCritical
sensor.State = &state
default:
state := device.HardwareHealthComponentStateUnknown
sensor.State = &state
}
}
sensors = append(sensors, sensor)
}
return sensors, nil
}
func (c *fortigateCommunicator) GetHighAvailabilityComponentState(ctx context.Context) (device.HighAvailabilityComponentState, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return "", errors.New("no device connection available")
}
// check if ha mode is standalone
modeRes, err := con.SNMP.SnmpClient.SNMPGet(ctx, "1.3.6.1.4.1.12356.101.13.1.1.0") // fgHaSystemMode
if err != nil {
return "", errors.Wrap(err, "failed to read out high-availability mode")
}
if len(modeRes) < 1 {
return "", errors.New("failed to read out high-availability mode")
}
mode, err := modeRes[0].GetValue()
if err != nil {
return "", errors.Wrap(err, "failed to get high-availability mode")
}
if mode.String() == "1" {
return device.HighAvailabilityComponentStateStandalone, nil
}
// if ha mode != standalone, read out sync state
idx, err := c.getHighAvailabilityIndex(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to find device in high-availability table")
}
oid := network.OID("1.3.6.1.4.1.12356.101.13.2.1.1.12") // fgHaStatsSyncStatus
stateRes, err := con.SNMP.SnmpClient.SNMPGet(ctx, oid.AddIndex(idx))
if err != nil {
return "", errors.Wrap(err, "failed to read out high-availability sync state")
}
if len(stateRes) < 1 {
return "", errors.New("failed to read out high-availability mode")
}
state, err := stateRes[0].GetValue()
if err != nil {
return "", errors.Wrap(err, "failed to get high-availability sync state")
}
var res device.HighAvailabilityComponentState
switch s := state.String(); s {
case "0":
res = device.HighAvailabilityComponentStateUnsynchronized
case "1":
res = device.HighAvailabilityComponentStateSynchronized
default:
return "", fmt.Errorf("unknown sync state '%s'", s)
}
return res, nil
}
func (c *fortigateCommunicator) GetHighAvailabilityComponentRole(ctx context.Context) (string, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return "", errors.New("no device connection available")
}
state, err := c.GetHighAvailabilityComponentState(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to read out high-availability state")
}
if state == device.HighAvailabilityComponentStateStandalone {
return "", errors.New("device is not in high-availability mode (state = standalone)")
}
idx, err := c.getHighAvailabilityIndex(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to find device in high-availability table")
}
snmpRes, err := con.SNMP.SnmpClient.SNMPGet(ctx, network.OID("1.3.6.1.4.1.12356.101.13.2.1.1.2").AddIndex(idx), network.OID("1.3.6.1.4.1.12356.101.13.2.1.1.16").AddIndex(idx))
if err != nil {
return "", errors.Wrap(err, "failed to read out high availability serial numbers")
}
if len(snmpRes) != 2 {
return "", errors.Wrap(err, "failed to read out high availability serial numbers")
}
serial, err := snmpRes[0].GetValue()
if err != nil {
return "", errors.Wrap(err, "failed to get high-availability serial number")
}
masterSerial, err := snmpRes[1].GetValue()
if err != nil {
return "", errors.Wrap(err, "failed to get high-availability master serial number")
}
if serial.IsEmpty() && masterSerial.IsEmpty() {
return "", errors.New("cannot map serial number to master serial number because one of them is empty")
}
if serial.String() == masterSerial.String() {
return "master", nil
}
return "slave", nil
}
func (c *fortigateCommunicator) GetHighAvailabilityComponentNodes(ctx context.Context) (int, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return 0, errors.New("no device connection available")
}
state, err := c.GetHighAvailabilityComponentState(ctx)
if err != nil {
return 0, errors.Wrap(err, "failed to read out high-availability state")
}
if state == device.HighAvailabilityComponentStateStandalone {
return 0, errors.New("device is not in high-availability mode (state = standalone)")
}
snmpRes, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.12356.101.13.2.1.1.1")
if err != nil {
return 0, errors.Wrap(err, "failed to read out high availability serial numbers")
}
return len(snmpRes), nil
}
func (c *fortigateCommunicator) getHighAvailabilityIndex(ctx context.Context) (string, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return "", errors.New("no device connection available")
}
serial, err := c.deviceClass.GetSerialNumber(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to read out serial number")
}
res, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.12356.101.13.2.1.1.2")
if err != nil {
return "", errors.Wrap(err, "failed to read out high availability serial numbers")
}
for _, r := range res {
val, err := r.GetValue()
if err != nil {
return "", errors.Wrap(err, "failed to get high availability serial number")
}
if val.String() == serial {
return r.GetOID().GetIndex(), nil
}
}
return "", errors.New("failed to get ha index")
}