-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskinfo.go
70 lines (60 loc) · 1.44 KB
/
diskinfo.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
package main
import (
"context"
"syscall"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
const (
// B is Byte
B = 1
// KB is Kilobyte
KB = 1024 * B
// MB is Metabyte
MB = 1024 * KB
// GB is Gigabyte
GB = 1024 * MB
)
// DiskStatus 는 디스크용량 정보를 담는 자료구조이다.
type DiskStatus struct {
All uint64 `json:"all"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
}
// DiskCheck 함수는 rootPath의 디스크용량을 확인하는 함수이다.
func DiskCheck() (DiskStatus, error) {
var ds DiskStatus
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
return ds, err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
return ds, err
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
return ds, err
}
// admin settin에서 rootpath를 가져와서 경로를 생성한다.
rootpath, err := GetRootPath(client)
if err != nil {
return ds, err
}
// rootpath경로의 디스크 용량을 확인한다.
fs := syscall.Statfs_t{}
err = syscall.Statfs(rootpath, &fs)
if err != nil {
return ds, err
}
ds.All = fs.Blocks * uint64(fs.Bsize)
ds.Free = fs.Bfree * uint64(fs.Bsize)
ds.Used = ds.All - ds.Free
return ds, nil
}