-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.go
102 lines (90 loc) · 3.26 KB
/
linux.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
package codecommunicator
import (
"context"
"github.com/RincewindsHat/thola/internal/device"
"github.com/RincewindsHat/thola/internal/mapping"
"github.com/RincewindsHat/thola/internal/network"
"github.com/pkg/errors"
)
type linuxCommunicator struct {
codeCommunicator
}
// GetDiskComponentStorages returns the cpu load of ios devices.
func (c *linuxCommunicator) GetDiskComponentStorages(ctx context.Context) ([]device.DiskComponentStorage, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return nil, errors.New("snmp client is empty")
}
typeResponses, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.2.1.25.2.3.1.2")
if err != nil {
return nil, errors.Wrap(err, "failed to read storage types")
}
descriptionResponses, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.2.1.25.2.3.1.3")
if err != nil {
return nil, errors.Wrap(err, "failed to read storage description")
}
availableResponses, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.2.1.25.2.3.1.5")
if err != nil {
return nil, errors.Wrap(err, "failed to read storage available")
}
usedResponses, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.2.1.25.2.3.1.6")
if err != nil {
return nil, errors.Wrap(err, "failed to read storage used")
}
storageUnitResponses, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.2.1.25.2.3.1.4")
if err != nil {
return nil, errors.Wrap(err, "failed to read storage unit")
}
var res []device.DiskComponentStorage
for i := range typeResponses {
var storage device.DiskComponentStorage
storageTypeValue, err := typeResponses[i].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get value from snmp response")
}
storageType, err := mapping.GetMappedValue("hrStorageType.yaml", storageTypeValue.String())
if err != nil {
return nil, errors.Wrap(err, "failed to get mapped storage type")
}
if storageType == "Other" || storageType == "RAM" || storageType == "Virtual Memory" {
continue
}
storage.Type = &storageType
descriptionValue, err := descriptionResponses[i].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get value from snmp response")
}
description := descriptionValue.String()
storage.Description = &description
storageUnitValue, err := storageUnitResponses[i].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get value from snmp response")
}
storageUnit, err := storageUnitValue.UInt64()
if err != nil {
return nil, errors.Wrap(err, "failed to convert value to int")
}
availableValue, err := availableResponses[i].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get value from snmp response")
}
available, err := availableValue.UInt64()
if err != nil {
return nil, errors.Wrap(err, "failed to convert value to int")
}
availableComputed := available * storageUnit
storage.Available = &availableComputed
usedValue, err := usedResponses[i].GetValue()
if err != nil {
return nil, errors.Wrap(err, "failed to get value from snmp response")
}
used, err := usedValue.UInt64()
if err != nil {
return nil, errors.Wrap(err, "failed to convert value to int")
}
usedComputed := used * storageUnit
storage.Used = &usedComputed
res = append(res, storage)
}
return res, nil
}