forked from slayercat/GoSNMPServer
/
disk.go
116 lines (112 loc) · 3.53 KB
/
disk.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
package ucdMib
import "fmt"
import "github.com/gosnmp/gosnmp"
import "github.com/shinanca/GoSNMPServer"
import "github.com/shirou/gopsutil/disk"
// NameOverride configs what path disk usage will returns.
type NameOverride struct {
// RealPath in this pc.
RealPath string
// ShowName indicate what will it show in oid:
// in currentDskPath 1.3.6.1.4.1.2021.9.1.2.xxx
// currentDskDevice 1.3.6.1.4.1.2021.9.1.3.xxx
ShowName string
}
// DiskUsageOIDs Returns a list of disk usages.
// Args:
// showTheseNameOnly: what path whill this oid returns. empty means all.
// see http://www.net-snmp.org/docs/mibs/ucdavis.html#DisplayString
func DiskUsageOIDs(showTheseNameOnly ...NameOverride) []*GoSNMPServer.PDUValueControlItem {
if len(showTheseNameOnly) == 0 {
partitionStats, err := disk.Partitions(false)
if err != nil {
g_Logger.Errorf("Load partitionStats failed, err=%v", err)
return []*GoSNMPServer.PDUValueControlItem{}
}
for _, val := range partitionStats {
showTheseNameOnly = append(showTheseNameOnly, NameOverride{
RealPath: val.Mountpoint,
ShowName: val.Mountpoint,
})
}
}
toRet := []*GoSNMPServer.PDUValueControlItem{}
for id, each := range showTheseNameOnly {
cid := id + 1
currentDiskItem := each
thisDiskID := []*GoSNMPServer.PDUValueControlItem{
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.1.%d", cid),
Type: gosnmp.Integer,
OnGet: func() (value interface{}, err error) { return GoSNMPServer.Asn1IntegerWrap(cid), nil },
Document: "dskIndex",
},
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.2.%d", cid),
Type: gosnmp.OctetString,
OnGet: func() (value interface{}, err error) {
return GoSNMPServer.Asn1OctetStringWrap(currentDiskItem.ShowName), nil
},
Document: "currentDskPath",
},
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.3.%d", cid),
Type: gosnmp.OctetString,
OnGet: func() (value interface{}, err error) {
return GoSNMPServer.Asn1OctetStringWrap(currentDiskItem.ShowName), nil
},
Document: "currentDskDevice",
},
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.6.%d", cid),
Type: gosnmp.Integer,
OnGet: func() (value interface{}, err error) {
data, err := disk.Usage(currentDiskItem.RealPath)
if err != nil {
return nil, err
}
return GoSNMPServer.Asn1IntegerWrap(int(data.Total / 1024 / 1024)), nil
},
Document: "currentDskTotal",
},
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.7.%d", cid),
Type: gosnmp.Integer,
OnGet: func() (value interface{}, err error) {
data, err := disk.Usage(currentDiskItem.RealPath)
if err != nil {
return nil, err
}
return GoSNMPServer.Asn1IntegerWrap(int(data.Free / 1024 / 1024)), nil
},
Document: "currentDskAvail",
},
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.8.%d", cid),
Type: gosnmp.Integer,
OnGet: func() (value interface{}, err error) {
data, err := disk.Usage(currentDiskItem.RealPath)
if err != nil {
return nil, err
}
return GoSNMPServer.Asn1IntegerWrap(int(data.Used / 1024 / 1024)), nil
},
Document: "currentDskUsed",
},
{
OID: fmt.Sprintf("1.3.6.1.4.1.2021.9.1.9.%d", cid),
Type: gosnmp.Integer,
OnGet: func() (value interface{}, err error) {
data, err := disk.Usage(currentDiskItem.RealPath)
if err != nil {
return nil, err
}
return GoSNMPServer.Asn1IntegerWrap(int(data.UsedPercent)), nil
},
Document: "currentDskPercent",
},
}
toRet = append(toRet, thisDiskID...)
}
return toRet
}