forked from IceWhaleTech/CasaOS-LocalStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
160 lines (124 loc) · 4.04 KB
/
misc.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
package main
import (
"context"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS-LocalStorage/common"
"github.com/IceWhaleTech/CasaOS-LocalStorage/model"
"github.com/IceWhaleTech/CasaOS-LocalStorage/service"
"github.com/pilebones/go-udev/netlink"
"go.uber.org/zap"
)
func sendDiskBySocket() {
blkList := service.MyService.Disk().LSBLK(true)
status := model.DiskStatus{}
healthy := true
var systemDisk *model.LSBLKModel
for _, currentDisk := range blkList {
if systemDisk == nil {
// go 5 level deep to look for system block device by mount point being "/"
systemDisk = service.WalkDisk(currentDisk, 5, func(blk model.LSBLKModel) bool { return blk.MountPoint == "/" })
if systemDisk != nil {
s, _ := strconv.ParseUint(systemDisk.FSSize.String(), 10, 64)
a, _ := strconv.ParseUint(systemDisk.FSAvail.String(), 10, 64)
u, _ := strconv.ParseUint(systemDisk.FSUsed.String(), 10, 64)
status.Size += s
status.Avail += a
status.Used += u
continue
}
}
if !service.IsDiskSupported(currentDisk) {
continue
}
temp := service.MyService.Disk().SmartCTL(currentDisk.Path)
if reflect.DeepEqual(temp, model.SmartctlA{}) {
healthy = true
} else {
healthy = temp.SmartStatus.Passed
}
for _, v := range currentDisk.Children {
s, _ := strconv.ParseUint(v.FSSize.String(), 10, 64)
a, _ := strconv.ParseUint(v.FSAvail.String(), 10, 64)
u, _ := strconv.ParseUint(v.FSUsed.String(), 10, 64)
status.Size += s
status.Avail += a
status.Used += u
}
}
status.Health = healthy
message := make(map[string]interface{})
message["sys_disk"] = status
if err := service.MyService.Notify().SendSystemStatusNotify(message); err != nil {
logger.Error("failed to send notify", zap.Any("message", message), zap.Error(err))
}
}
func sendUSBBySocket() {
message := map[string]interface{}{
"sys_usb": service.MyService.Disk().GetUSBDriveStatusList(),
}
if err := service.MyService.Notify().SendSystemStatusNotify(message); err != nil {
logger.Error("failed to send notify", zap.Any("message", message), zap.Error(err))
}
}
func monitorUEvent(ctx context.Context) {
var matcher netlink.Matcher
conn := new(netlink.UEventConn)
if err := conn.Connect(netlink.UdevEvent); err != nil {
logger.Error("udev err", zap.Any("Unable to connect to Netlink Kobject UEvent socket", err))
}
defer conn.Close()
queue := make(chan netlink.UEvent)
defer close(queue)
errors := make(chan error)
defer close(errors)
quit := conn.Monitor(queue, errors, matcher)
defer close(quit)
for {
select {
case <-ctx.Done():
return
case uevent := <-queue:
if event := common.EventAdapter(uevent); event != nil {
// add UI properties to applicable events so that CasaOS UI can render it
event := common.EventAdapterWithUIProperties(event)
if v, ok := event.Properties["local-storage:path"]; ok && strings.Contains(event.Name, "disk") {
diskModel := service.MyService.Disk().GetDiskInfo(v)
if !reflect.DeepEqual(diskModel, model.LSBLKModel{}) {
properties := common.AdditionalProperties(diskModel)
for k, v := range properties {
event.Properties[k] = v
}
}
}
logger.Info("disk model", zap.Any("diskModel", event.Name))
response, err := service.MyService.MessageBus().PublishEventWithResponse(ctx, event.SourceID, event.Name, event.Properties)
if err != nil {
logger.Error("failed to publish event to message bus", zap.Error(err), zap.Any("event", event))
}
if response.StatusCode() != http.StatusOK {
logger.Error("failed to publish event to message bus", zap.String("status", response.Status()), zap.Any("response", response))
}
}
switch uevent.Env["DEVTYPE"] {
case "partition":
switch uevent.Env["ID_BUS"] {
case "usb":
time.Sleep(1 * time.Second)
sendUSBBySocket()
continue
}
}
case err := <-errors:
logger.Error("udev err", zap.Error(err))
}
}
}
func sendStorageStats() {
sendDiskBySocket()
sendUSBBySocket()
}