-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurrent_fails.go
148 lines (128 loc) · 3.57 KB
/
current_fails.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
package main
import (
"encoding/json"
"os"
"path"
"sync"
"time"
uuid "github.com/satori/go.uuid"
)
// CurrentFail type hold informations about a failure currently detected
// and not resolved yet
type CurrentFail struct {
FailStart time.Time
FailCount int
OkCount int
UniqueID string
// optional "payload"
RelatedTask *Task // for Checks (!!)
RelatedHost *Host // for Runs
RelatedTTask *Task // for Tasks
}
var (
currentFails map[string]*CurrentFail
currentFailsMutex sync.Mutex
)
const statusFile string = "nosee-fails.json"
// CurrentFailsCreate initialize the global currentFails variable
func CurrentFailsCreate() {
currentFails = make(map[string]*CurrentFail)
}
// CurrentFailsSave dumps current alerts to disk
func CurrentFailsSave() {
// doing this in a go routine allows this function to be called
// by functions that are already locking the mutex
go func() {
currentFailsMutex.Lock()
defer currentFailsMutex.Unlock()
path := path.Clean(GlobalConfig.SavePath + "/" + statusFile)
f, err := os.Create(path)
if err != nil {
Error.Printf("can't save fails in '%s': %s (see save_path param?)", path, err)
return
}
defer f.Close()
enc := json.NewEncoder(f)
err = enc.Encode(¤tFails)
if err != nil {
Error.Printf("fails json encode: %s", err)
return
}
Info.Printf("current fails successfully saved to '%s'", path)
}()
}
// CurrentFailsLoad will load from disk previous "fails"
func CurrentFailsLoad() {
currentFailsMutex.Lock()
defer currentFailsMutex.Unlock()
path := path.Clean(GlobalConfig.SavePath + "/" + statusFile)
f, err := os.Open(path)
if err != nil {
Warning.Printf("can't read previous status: %s, no fails loaded", err)
return
}
defer f.Close()
dec := json.NewDecoder(f)
err = dec.Decode(¤tFails)
if err != nil {
Error.Printf("'%s' json decode: %s", path, err)
}
Info.Printf("'%s' loaded: %d fail(s)", path, len(currentFails))
}
// CurrentFailDelete deleted the CurrentFail with the given hash of the global currentFails
func CurrentFailDelete(hash string) {
currentFailsMutex.Lock()
defer currentFailsMutex.Unlock()
delete(currentFails, hash)
CurrentFailsSave()
}
// CurrentFailAdd adds a CurrentFail to the global currentFails using given hash
func CurrentFailAdd(hash string, failedCheck *CurrentFail) {
currentFailsMutex.Lock()
defer currentFailsMutex.Unlock()
currentFails[hash] = failedCheck
CurrentFailsSave()
}
// CurrentFailInc increments FailCount of the CurrentFail with the given hash
func CurrentFailInc(hash string) {
currentFailsMutex.Lock()
defer currentFailsMutex.Unlock()
currentFails[hash].FailCount++
currentFails[hash].OkCount = 0
CurrentFailsSave()
}
// CurrentFailDec increments OkCount of the CurrentFail with the given hash
func CurrentFailDec(hash string) {
currentFailsMutex.Lock()
defer currentFailsMutex.Unlock()
currentFails[hash].OkCount++
CurrentFailsSave()
}
// CurrentFailGetAndInc returns the CurrentFail with the given hash and
// increments its FailCount. The CurrentFail is created if it does not
// already exists.
func CurrentFailGetAndInc(hash string) *CurrentFail {
cf, ok := currentFails[hash]
if !ok {
var cf CurrentFail
uuid := uuid.NewV4()
cf.FailCount = 1
cf.OkCount = 0
cf.FailStart = time.Now()
cf.UniqueID = uuid.String()
CurrentFailAdd(hash, &cf)
return &cf
}
CurrentFailInc(hash)
return cf
}
// CurrentFailGetAndDec returns the CurrentFail with the given hash and
// increments its OkCount
func CurrentFailGetAndDec(hash string) *CurrentFail {
cf, ok := currentFails[hash]
if !ok {
return nil
}
CurrentFailDec(hash)
return cf
}