forked from CrowdStrike/gotel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
279 lines (240 loc) · 8.68 KB
/
store.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package gotel
import (
"database/sql"
"errors"
"fmt"
"time"
)
// Endpoint holds the reference to our DB connection
type Endpoint struct {
Db *sql.DB
}
// reservation is when an app first registers that it will be checking into our gotel
type reservation struct {
JobID int `json:"job_id"`
Owner string `json:"owner"`
Notify string `json:"notify"`
App string `json:"app"`
Component string `json:"component"`
Frequency int `json:"frequency"`
TimeUnits string `json:"time_units"`
LastCheckin int64 `json:"last_checkin"`
LastCheckinStr string `json:"last_checkin_str"` // human readable time
TimeSinceLastCheckin string `json:"time_sine_last_checkin"`
FailingSLA bool `json:"failing_sla"`
NumCheckins int `json:"number_of_checkins"`
}
// checkin holds a struct that is populated when an app checks in as still alive
type checkin struct {
App string `json:"app"`
Component string `json:"component"`
Notes string `json:"notes"`
}
// checkOut is for removing reservations
type checkOut struct {
App string `json:"app"`
Component string `json:"component"`
}
// snooze holds a struct for when users want to pause an alert for maintenance
type snooze struct {
App string `json:"app"`
Component string `json:"component"`
Duration int `json:"duration"`
TimeUnits string `json:"time_units"`
}
// InitDb initialzes and then bootstraps the database
func InitDb(host, user, pass string, conf config) *sql.DB {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/gotel", user, pass, host))
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(fmt.Sprintf("Unable to ping the DB at host [%s] user [%s]", host, user))
}
bootstrapDb(db, conf)
return db
}
func storeReservation(db *sql.DB, r *reservation) (bool, error) {
// get current unix time one day into the future as the initial insert data, give it one day to bake
tomorrow := time.Now().Add(24 * time.Hour).UTC().Unix()
now := time.Now().UTC().Unix()
seconds := getSecondsFromUnits(r.Frequency, r.TimeUnits)
if seconds < 10 {
return false, errors.New("Unable to store reservations for less than 10 seconds at this time, for no real reason.")
}
stmt, err := db.Prepare(`INSERT INTO reservations(app, component, owner, notify, frequency, time_units, inserted_timestamp, last_checkin_timestamp)
VALUES(?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE notify=?, frequency=?, time_units=?
`)
if err != nil {
l.warn("unable to prepare statement %s", err)
return false, errors.New("Unable to save record")
}
defer stmt.Close()
res, err := stmt.Exec(r.App, r.Component, r.Owner, r.Notify, r.Frequency, r.TimeUnits, now, tomorrow, r.Notify, r.Frequency, r.TimeUnits)
if err != nil {
l.warn("Unable to insert record %s", err)
return false, errors.New("Unable to save record")
}
rowCnt, err := res.RowsAffected()
if err != nil {
return false, errors.New("Unable to save record")
}
l.info("Insertedaffected = %d\n", rowCnt)
return true, nil
}
func logHouseKeeping(db *sql.DB, c checkin, now int64) (bool, error) {
//Insert
stmt, err := db.Prepare("insert into housekeeping(app, component, notes, last_checkin_timestamp) values(?, ?, ?, ?)")
if err != nil {
l.warn("Unable to prepare record %s", err)
return false, errors.New("Unable to store checkin")
}
defer stmt.Close()
_, err = stmt.Exec(c.App, c.Component, c.Notes, now)
if err != nil {
l.warn("Unable to insert record %s", err)
return false, errors.New("Unable to store checkin")
}
return true, nil
}
func storeCheckin(db *sql.DB, c checkin, now int64) (bool, error) {
stmt, err := db.Prepare("UPDATE reservations SET last_checkin_timestamp = ?, num_checkins = num_checkins + 1 WHERE app=? AND component=?")
if err != nil {
l.warn("Unable to prepare record %s", err)
return false, errors.New("Unable to prepare checkin")
}
defer stmt.Close()
_, err = stmt.Exec(now, c.App, c.Component)
if err != nil {
l.warn("Unable to update reservation %s", err)
return false, errors.New("Unable to store checkin")
}
return true, nil
}
func storeCheckOut(db *sql.DB, c *checkOut) (bool, error) {
stmt, err := db.Prepare("DELETE FROM reservations WHERE app=? AND component=?")
if err != nil {
l.warn("Unable to prepare record %s", err)
return false, errors.New("Unable to prepare checkin")
}
defer stmt.Close()
_, err = stmt.Exec(c.App, c.Component)
if err != nil {
l.warn("Unable to update reservation %s", err)
return false, errors.New("Unable to store checkin")
}
return true, nil
}
func storeSnooze(db *sql.DB, p *snooze) (bool, error) {
futureSeconds := getSecondsFromUnits(p.Duration, p.TimeUnits)
pausedTime := time.Now().Add(time.Duration(futureSeconds) * time.Second).UTC().Unix()
stmt, err := db.Prepare("UPDATE reservations SET last_checkin_timestamp = ? WHERE app=? AND component=?")
if err != nil {
l.warn("Unable to prepare record %s", err)
return false, errors.New("Unable to prepare snooze")
}
defer stmt.Close()
_, err = stmt.Exec(pausedTime, p.App, p.Component)
if err != nil {
l.warn("Unable to update snooze %s", err)
return false, errors.New("Unable to store snooze")
}
return true, nil
}
func getSecondsFromUnits(freq int, units string) int {
var seconds int
if units == "seconds" {
seconds = freq
} else if units == "minutes" {
seconds = 60 * freq
} else if units == "hours" {
seconds = 60 * 60 * freq
} else {
seconds = 60 * 60 * 24 * freq
}
return seconds
}
func bootstrapDb(db *sql.DB, conf config) {
l.info("Bootstrapping GoTel DB tables")
alertSQL := `
CREATE TABLE IF NOT EXISTS alerts (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
app varchar(30) DEFAULT NULL,
component varchar(30) DEFAULT NULL,
alert_time int(11) DEFAULT NULL,
alerters text DEFAULT NULL,
insert_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`
_, err := db.Exec(alertSQL)
if err != nil {
panic(err)
}
sql := `
CREATE TABLE IF NOT EXISTS reservations (
id int(11) NOT NULL AUTO_INCREMENT,
app varchar(150) DEFAULT NULL,
component varchar(150) DEFAULT NULL,
owner text DEFAULT NULL,
notify text DEFAULT NULL,
frequency int(11) DEFAULT NULL,
time_units varchar(30) DEFAULT NULL,
inserted_timestamp int(11) DEFAULT NULL,
num_checkins int(11) DEFAULT '0',
last_alert_timestamp int(11) DEFAULT NULL,
last_checkin_timestamp int(11) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY uniq_app (app,component)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
`
_, err = db.Exec(sql)
if err != nil {
panic(err)
}
housekeeping := `CREATE TABLE IF NOT EXISTS housekeeping (
id int(11) NOT NULL AUTO_INCREMENT,
app varchar(30) DEFAULT NULL,
component varchar(30) DEFAULT NULL,
notes text,
last_checkin_timestamp int(11) DEFAULT NULL,
insert_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`
_, err = db.Exec(housekeeping)
if err != nil {
panic(err)
}
nodes := `CREATE TABLE IF NOT EXISTS nodes (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
ip_address varchar(20) DEFAULT NULL,
node_id int(30) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY uniq_ip (ip_address)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;`
_, err = db.Exec(nodes)
if err != nil {
panic(err)
}
// store gotel as the initial application to monitor
l.info("Starting to bootstrap worker/coordinator reservations...")
tomorrow := time.Now().Add(24 * time.Hour).UTC().Unix()
now := time.Now().UTC().Unix()
gotelApp := `INSERT INTO reservations (app, component, owner, notify, frequency, time_units, inserted_timestamp, last_checkin_timestamp)
VALUES ('gotel', 'coordinator', ?, ?, 5, 'minutes', ?, ?) ON DUPLICATE KEY UPDATE owner=?`
_, err = db.Exec(gotelApp, conf.Main.GotelOwnerEmail, conf.Main.GotelOwnerEmail, now, tomorrow, conf.Main.GotelOwnerEmail)
if err != nil {
l.warn("storing gotel/coordinator as initial app [%q]", err)
} else {
l.info("Inserted gotel/coordinator as first app to monitor")
}
gotelAppWorker := `INSERT INTO reservations (app, component, owner, notify, frequency, time_units, inserted_timestamp, last_checkin_timestamp)
VALUES ('gotel', 'worker', ?, ?, 5, 'minutes', ?, ?) ON DUPLICATE KEY UPDATE owner=?`
_, err = db.Exec(gotelAppWorker, conf.Main.GotelOwnerEmail, conf.Main.GotelOwnerEmail, now, tomorrow, conf.Main.GotelOwnerEmail)
if err != nil {
l.warn("storing gotel/worker as initial app [%v]", err)
} else {
l.info("Inserted gotel/worker as first app to monitor")
}
}