-
Notifications
You must be signed in to change notification settings - Fork 0
/
ironware.go
78 lines (70 loc) · 2.35 KB
/
ironware.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
package codecommunicator
import (
"context"
"github.com/RincewindsHat/thola/internal/device"
"github.com/RincewindsHat/thola/internal/network"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"strings"
)
type ironwareCommunicator struct {
codeCommunicator
}
// GetCPUComponentCPULoad returns the cpu load of ironware devices.
func (c *ironwareCommunicator) GetCPUComponentCPULoad(ctx context.Context) ([]device.CPU, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("no device connection available")
}
// snAgentCpuUtil100thPercent
cpuUtilizationOID := network.OID(".1.3.6.1.4.1.1991.1.1.2.11.1.1.6")
precision := 100.0
cpuUtilization, err := con.SNMP.SnmpClient.SNMPWalk(ctx, cpuUtilizationOID)
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("walking 'snAgentCpuUtil100thPercent' failed")
// snAgentCpuUtilValue
cpuUtilizationOID = ".1.3.6.1.4.1.1991.1.1.2.11.1.1.4"
precision = 1.0
cpuUtilization, err = con.SNMP.SnmpClient.SNMPWalk(ctx, cpuUtilizationOID)
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("walking 'snAgentCpuUtilValue' failed")
return nil, errors.Wrap(err, "getting CPU utilization failed")
}
}
indexSlotNum := make(map[string]string)
// snAgentCpuUtilSlotNum
slotNumOID := network.OID(".1.3.6.1.4.1.1991.1.1.2.11.1.1.1")
slotNum, err := con.SNMP.SnmpClient.SNMPWalk(ctx, slotNumOID)
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msg("walking 'snAgentCpuUtilSlotNum' failed")
} else {
for _, num := range slotNum {
res, err := num.GetValue()
if err != nil {
return nil, errors.Wrap(err, "couldn't get string value")
}
indexSlotNum[strings.TrimPrefix(num.GetOID().String(), slotNumOID.String())] = res.String()
}
}
var cpus []device.CPU
for _, cpuUtil := range cpuUtilization {
if !strings.HasSuffix(cpuUtil.GetOID().String(), "300") {
continue
}
value, err := cpuUtil.GetValue()
if err != nil {
return nil, errors.Wrap(err, "couldn't get string value")
}
valueFloat, err := value.Float64()
if err != nil {
return nil, errors.Wrap(err, "failed to parse snmp response")
}
valueFloat /= precision
cpu := device.CPU{Load: &valueFloat}
if num, ok := indexSlotNum[strings.TrimPrefix(cpuUtil.GetOID().String(), cpuUtilizationOID.String())]; ok {
cpu.Label = &num
}
cpus = append(cpus, cpu)
}
return cpus, nil
}