-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathwatcher.go
205 lines (184 loc) · 5.34 KB
/
watcher.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
// Copyright (c) 2021 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"github.com/lf-edge/eve/pkg/pillar/base"
uuid "github.com/satori/go.uuid"
)
// UsageZone :
type UsageZone int
const (
// GreenZone :
GreenZone UsageZone = 0
// YellowZone :
YellowZone UsageZone = 1
// OrangeZone :
OrangeZone UsageZone = 2
// RedZone :
RedZone UsageZone = 3
)
// MemoryNotification :
type MemoryNotification struct {
Total uint64 // Total memory in Bytes
Used uint64 // Used memory in Bytes
Zone UsageZone
PrevUsage uint64 // Previous (last) memory usage in Bytes
// Last 5 Usage percentage values
// Most recent usage is at index 0, the next at index 1 and so on.
LastFive []uint64
UsageSlab uint64
PrevSlab uint64
}
func zonetoString(zone UsageZone) string {
switch zone {
case GreenZone:
return "GREEN"
case YellowZone:
return "YELLOW"
case OrangeZone:
return "ORANGE"
case RedZone:
return "RED"
}
return "UNKNOWN"
}
// LogCreate :
func (mem MemoryNotification) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.MemoryNotificationType, "MemoryNotification",
uuid.UUID{}, mem.LogKey())
if logObject == nil {
return
}
loggable := logObject.CloneAndAddField("Total", mem.Total).
CloneAndAddField("Used", mem.Used).
CloneAndAddField("Zone", zonetoString(mem.Zone)).
CloneAndAddField("PrevUsage", mem.PrevUsage).
CloneAndAddField("LastFive", mem.LastFive).
CloneAndAddField("UsageSlab", mem.UsageSlab).
CloneAndAddField("PrevSlab", mem.PrevSlab)
switch mem.Zone {
case GreenZone, YellowZone:
loggable.Functionf("MemoryNotification create")
case OrangeZone:
loggable.Warnf("MemoryNotification create")
case RedZone:
loggable.Errorf("MemoryNotification create")
default:
loggable.Noticef("MemoryNotification create")
}
}
// LogModify :
func (mem MemoryNotification) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.NewLogObject(logBase, base.MemoryNotificationType, "MemoryNotification",
uuid.UUID{}, mem.LogKey())
if logObject == nil {
return
}
oldMem, ok := old.(MemoryNotification)
if !ok {
return
}
if mem.UsageSlab == oldMem.UsageSlab && mem.Zone == oldMem.Zone {
return
}
loggable := logObject.CloneAndAddField("Total", mem.Total).
CloneAndAddField("Used", mem.Used).
CloneAndAddField("Zone", zonetoString(mem.Zone)).
CloneAndAddField("PrevUsage", mem.PrevUsage).
CloneAndAddField("LastFive", mem.LastFive).
CloneAndAddField("UsageSlab", mem.UsageSlab).
CloneAndAddField("PrevSlab", mem.PrevSlab)
switch mem.Zone {
case GreenZone, YellowZone:
loggable.Functionf("MemoryNotification create")
case OrangeZone:
loggable.Warnf("MemoryNotification create")
case RedZone:
loggable.Errorf("MemoryNotification create")
default:
loggable.Noticef("MemoryNotification create")
}
}
// LogDelete :
func (mem MemoryNotification) LogDelete(logBase *base.LogObject) {
}
// LogKey :
func (mem MemoryNotification) LogKey() string {
return "MemoryNotification"
}
// DiskNotification :
type DiskNotification struct {
Total uint64 // Total Disk space in Bytes
Used uint64 // Used Disk space in Bytes
Zone UsageZone
PrevUsage uint64 // Previous (last) disk usage in Bytes
// Last 5 Usage percentage values
// Most recent usage is at index 0, the next at index 1 and so on.
LastFive []uint64
UsageSlab uint64
PrevSlab uint64
}
// LogCreate :
func (disk DiskNotification) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.DiskNotificationType, "DiskNotification",
uuid.UUID{}, disk.LogKey())
if logObject == nil {
return
}
loggable := logObject.CloneAndAddField("Total", disk.Total).
CloneAndAddField("Used", disk.Used).
CloneAndAddField("Zone", zonetoString(disk.Zone)).
CloneAndAddField("PrevUsage", disk.PrevUsage).
CloneAndAddField("LastFive", disk.LastFive).
CloneAndAddField("UsageSlab", disk.UsageSlab).
CloneAndAddField("PrevSlab", disk.PrevSlab)
switch disk.Zone {
case GreenZone, YellowZone:
loggable.Functionf("DiskNotification create")
case OrangeZone:
loggable.Warnf("DiskNotification create")
case RedZone:
loggable.Errorf("DiskNotification create")
default:
loggable.Noticef("DiskNotification create")
}
}
// LogModify :
func (disk DiskNotification) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.NewLogObject(logBase, base.DiskNotificationType, "DiskNotification",
uuid.UUID{}, disk.LogKey())
if logObject == nil {
return
}
oldDisk, ok := old.(DiskNotification)
if !ok {
return
}
if disk.UsageSlab == oldDisk.UsageSlab && disk.Zone == oldDisk.Zone {
return
}
loggable := logObject.CloneAndAddField("Total", disk.Total).
CloneAndAddField("Used", disk.Used).
CloneAndAddField("Zone", zonetoString(disk.Zone)).
CloneAndAddField("PrevUsage", disk.PrevUsage).
CloneAndAddField("LastFive", disk.LastFive).
CloneAndAddField("UsageSlab", disk.UsageSlab).
CloneAndAddField("PrevSlab", disk.PrevSlab)
switch disk.Zone {
case GreenZone, YellowZone:
loggable.Functionf("DiskNotification create")
case OrangeZone:
loggable.Warnf("DiskNotification create")
case RedZone:
loggable.Errorf("DiskNotification create")
default:
loggable.Noticef("DiskNotification create")
}
}
// LogDelete :
func (disk DiskNotification) LogDelete(logBase *base.LogObject) {
}
// LogKey :
func (disk DiskNotification) LogKey() string {
return "DiskNotification"
}