forked from megaease/easeprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
256 lines (221 loc) · 6.23 KB
/
data.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
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package probe
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/megaease/easeprobe/global"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// MetaData the meta data of data file
type MetaData struct {
Name string `yaml:"name"`
Ver string `yaml:"version"`
ver string `yaml:"-"` // the version in data file
file string `yaml:"-"` // the current file name
backup string `yaml:"-"` // the backup file name
}
var (
resultData = map[string]*Result{}
metaData = MetaData{
Name: global.DefaultProg,
Ver: global.Ver,
}
metaBuf []byte
mutex = &sync.RWMutex{}
)
const split = "---\n"
// GetMetaData get the meta data
func GetMetaData() *MetaData {
return &metaData
}
// SetResultData set the result of probe
// Note: this function would be called by status update goroutine
//
// int saveData() in cmd/easeprobe/report.go
func SetResultData(name string, result *Result) {
r := result.Clone()
mutex.Lock()
resultData[name] = &r
mutex.Unlock()
}
// SetResultsData set the results of probe
func SetResultsData(r []Result) {
for i := 0; i < len(r); i++ {
SetResultData(r[i].Name, &r[i])
}
}
// GetResultData get the result of probe
// Note: the function would be called by Data Saving, SLA Report, Web Server
func GetResultData(name string) *Result {
mutex.RLock()
defer mutex.RUnlock()
if v, ok := resultData[name]; ok {
r := v.Clone()
return &r
}
return nil
}
// CleanData removes the items in resultData not in []Prober
// Note: No need to consider the thread-safe, because this function is only called once during the startup
func CleanData(p []Prober) {
var data = map[string]*Result{}
for i := 0; i < len(p); i++ {
r := p[i].Result()
d := GetResultData(r.Name)
if d != nil {
data[r.Name] = d
} else {
data[r.Name] = r
}
}
resultData = data
}
// SaveDataToFile save the results to file
// Note: No need to consider the thread-safe, because this function and SetResultData in same goroutine
func SaveDataToFile(filename string) error {
metaData.file = filename
if strings.TrimSpace(filename) == "-" {
return nil
}
dataBuf, err := yaml.Marshal(resultData)
if err != nil {
return err
}
genMetaBuf()
buf := append(metaBuf, dataBuf...)
if err := ioutil.WriteFile(filename, []byte(buf), 0644); err != nil {
return err
}
return nil
}
// LoadDataFromFile load the results from file
// Note: No need to consider the thread-safe, because this function is only called once during the startup
func LoadDataFromFile(filename string) error {
// if the data file is disabled, return
if strings.TrimSpace(filename) == "-" {
return nil
}
// if the data file is not exist, return
if _, err := os.Stat(filename); os.IsNotExist(err) {
return err
}
buf, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
// Multiple YAML Documents reading
dec := yaml.NewDecoder(bytes.NewReader(buf))
for {
var value interface{}
err := dec.Decode(&value)
if err == io.EOF {
break
}
if err != nil {
return err
}
if v, ok := value.(map[string]interface{}); ok {
valueBytes, _ := yaml.Marshal(value)
if v["version"] != nil {
if err := yaml.Unmarshal(valueBytes, &metaData); err != nil {
log.Warnf("Load meta data error: %v", err)
} else {
log.Debugf("Load meta data: name[%s], version[%s]", metaData.Name, metaData.Ver)
}
} else {
if err := yaml.Unmarshal(valueBytes, &resultData); err != nil {
return err
}
}
}
}
// set the meta name and version
// - if the Name is found in the data file, use it, otherwise use the default
// - always use the program version for the data file.
metaData.ver = metaData.Ver // save the file's version
SetMetaData(metaData.Name, global.Ver)
// backup the current data file
time := time.Now().UTC().Format(time.RFC3339Nano)
// replace ":" to "_" for windows platform compliance
time = strings.Replace(time, ":", "_", -1)
metaData.backup = filename + "-" + time
if err := os.Rename(filename, metaData.backup); err != nil {
log.Warnf("Backup data file error: %v", err)
}
return nil
}
// CleanDataFile keeps the max backup of data file
// Note: No need to consider the thread-safe, because this function is only called once during the startup
func CleanDataFile(filename string, backups int) {
if strings.TrimSpace(filename) == "-" {
return
}
// if backups is negative value, keep all backup files
if backups < 0 {
return
}
// get all of the backup files
pattern := filename + "-*"
matches, err := filepath.Glob(pattern)
if err != nil {
log.Errorf("Cannot clean data file: %v", err)
return
}
// if backups is not exceed the max number of backup files, return
if len(matches) <= backups {
log.Debugf("No need to clean data file (%d - %d) ", backups, len(matches))
return
}
// remove the oldest backup files
sort.Strings(matches)
for i := 0; i < len(matches)-backups; i++ {
if err := os.Remove(matches[i]); err != nil {
log.Errorf("Cannot clean data file: %v", err)
continue
}
log.Infof("Clean data file: %s", matches[i])
}
}
// SetMetaData set the meta data
// Note: No need to consider the thread-safe, because this function is only called during the startup
func SetMetaData(name string, ver string) {
metaData.Name = name
metaData.Ver = ver
// reconstructure the meta buf
genMetaBuf()
}
func genMetaBuf() {
// if the meta data is not exist in current data file, using the default.
if metaData.Name == "" {
metaData.Name = global.DefaultProg
}
if metaData.Ver == "" {
metaData.Ver = global.Ver
}
metaBuf, _ = yaml.Marshal(metaData)
metaBuf = append([]byte(split), metaBuf...)
metaBuf = append(metaBuf, []byte(split)...)
}