-
Notifications
You must be signed in to change notification settings - Fork 30
/
disk.go
278 lines (254 loc) · 6.34 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
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
package system
import (
"fmt"
"strconv"
"strings"
)
type FileSystem struct {
FsName string //文件系统
Total uint64 //总空间(kb)
Used uint64 //已用空间(kb)
Free uint64 //可用空间(kb)
UsedRate float64 //已用(百分比)
Mount string //挂载点
}
type Disk struct {
UsedRateSet []string //磁盘所有分区使用率集合
FsMap map[string]FileSystem //挂载点=>使用情况
Total uint64 //所有挂载分区总空间(kb)
Used uint64 //所有挂载分区总已用空间(kb)
Free uint64 //所有挂载分区总可用空间(kb)
UsedRate float64 //所有挂载分区总使用率
MaxUseRate float64 //所有挂载分区最大使用率
MaxUseRateFs string //使用率最大的分区
}
var mountedSet = []string{
"/disk",
"/",
"/home",
"/tmp",
"/var",
"/dev/shm",
"/boot",
"/usr",
"/data",
"/data0",
"/data1",
"/data2",
"/data3",
"/data4",
"/data5",
"/data6",
"/data7",
"/data8",
"/data9",
"/data10",
"/data11",
"/data12",
"/data00",
"/data01",
"/data02",
"/data03",
"/data04",
"/data05",
"/data06",
"/data07",
"/data08",
"/data09",
}
func (this *Disk) Collect() error {
//注意,这里一定要加P, 否则当Filesystem 太长时,无法显示为一行, 采集数据会不全
cmd := "df -lP"
output, err := Exec(cmd)
if err != nil {
return err
}
lines := strings.Split(output, "\n")
this.FsMap = map[string]FileSystem{}
this.UsedRateSet = []string{}
var (
maxUseRate float64
maxUseRateFs string
)
for row, line := range lines {
if row == 0 {
continue
}
fileds := strings.Fields(line)
if len(fileds) != 6 {
continue
}
fsName := fileds[0]
total, _ := strconv.ParseUint(fileds[1], 10, 64)
used, _ := strconv.ParseUint(fileds[2], 10, 64)
free, _ := strconv.ParseUint(fileds[3], 10, 64)
strUsedRate := strings.Replace(fileds[4], "%", "", -1)
usedRate, _ := strconv.ParseFloat(strUsedRate, 64)
mount := fileds[5]
//挂载分区使用情况
fs := FileSystem{}
fs.FsName = fsName
fs.Total = total
fs.Used = used
fs.Free = free
fs.UsedRate = usedRate
fs.Mount = mount
this.FsMap[mount] = fs
//更新所有挂载分区使用情况
this.Total += total
this.Used += used
this.Free += free
//磁盘所有分区使用率集合
this.UsedRateSet = append(this.UsedRateSet, fsName+"="+mount+"="+strUsedRate)
if fs.FsName != "devfs" && fs.UsedRate > maxUseRate {
maxUseRate = fs.UsedRate
maxUseRateFs = fs.Mount
}
}
//所有挂载分区总使用率
if this.Used+this.Free > 0 {
this.UsedRate = float64(this.Used) / float64(this.Used+this.Free) * 100
}
this.MaxUseRate = maxUseRate
this.MaxUseRateFs = maxUseRateFs
return nil
}
func (this *Disk) Dump() {
for _, fs := range this.FsMap {
fmt.Printf("FsName:%s, Total:%d, Used:%d, Free:%d, UsedRate:%f, Mount:%s\n",
fs.FsName,
fs.Total,
fs.Used,
fs.Free,
fs.UsedRate,
fs.Mount)
}
}
//分区使用率
func (this *Disk) MountUsedRate(mount string) string {
fs, exists := this.FsMap[mount]
if !exists {
return ""
}
return FloatToString(fs.UsedRate)
}
//所有挂载分区总使用率
func (this *Disk) DiskUsedRate(args string) string {
return FloatToString(this.UsedRate)
}
//机器物理磁盘信息
func DiskModel(args string) string {
cmd := "fdisk -l 2>/dev/null | grep 'Disk /dev/'"
output, err := Exec(cmd)
if err != nil {
return ""
}
lines := strings.Split(output, "\n")
models := []string{}
for _, line := range lines {
//去掉字节大小,只保留一个单位(GB或M)
sizes := strings.Split(line, ",")
if len(sizes) != 2 {
continue
}
fields := strings.Fields(sizes[0])
if len(fields) != 4 {
continue
}
model := strings.Replace(fields[1], ":", "", -1)
capacity := fields[2]
models = append(models, model+"|"+capacity)
}
ret := strings.Join(models, "$") + "$"
return ret
}
//磁盘所有分区使用率集合
func (this *Disk) DiskUsedRateSet(args string) string {
return strings.Join(this.UsedRateSet, "$") + "$"
}
// /home分区剩余空间(MB)
func (this *Disk) HomeFree(args string) string {
fs, exists := this.FsMap["/home"]
if !exists {
return ""
}
//MB
free := float64(fs.Free) / float64(1024)
return FloatToString(free)
}
// /home分区总空间(MB)
func (this *Disk) HomeTotal(args string) string {
fs, exists := this.FsMap["/home"]
if !exists {
return ""
}
//MB
total := float64(fs.Total) / float64(1024)
return FloatToString(total)
}
// /home分区空间使用率
func (this *Disk) HomeUsedRate(args string) string {
fs, exists := this.FsMap["/home"]
if !exists {
return ""
}
return FloatToString(fs.UsedRate)
}
//根分区剩余空间(MB)
func (this *Disk) RootFree(args string) string {
fs, exists := this.FsMap["/"]
if !exists {
return ""
}
free := float64(fs.Free) / float64(1024)
return FloatToString(free)
}
//根分区总空间(MB)
func (this *Disk) RootTotal(args string) string {
fs, exists := this.FsMap["/"]
if !exists {
return ""
}
total := float64(fs.Total) / float64(1024)
return FloatToString(total)
}
//根分区空间使用率
func (this *Disk) RootUsedRate(args string) string {
fs, exists := this.FsMap["/"]
if !exists {
return ""
}
return FloatToString(fs.UsedRate)
}
// /tmp分区使用率
func (this *Disk) TmpUsedRate(args string) string {
fs, exists := this.FsMap["/tmp"]
if !exists {
return ""
}
return FloatToString(fs.UsedRate)
}
// /usr分区使用率
func (this *Disk) UsrUsedRate(args string) string {
fs, exists := this.FsMap["/usr"]
if !exists {
return ""
}
return FloatToString(fs.UsedRate)
}
// /var分区使用率
func (this *Disk) VarUsedRate(args string) string {
fs, exists := this.FsMap["/var"]
if !exists {
return ""
}
return FloatToString(fs.UsedRate)
}
//磁盘所有分区最大使用率
func (this *Disk) MaxUsedRateFsFunc(args string) string {
return FloatToString(this.MaxUseRate) + "," + this.MaxUseRateFs
}
//返回某个目录的大小(MB)
func DiskUsedByDir(dir string) string {
return ExecOutput("du -sm " + dir + "|awk '{print $1}'")
}