/
main.go
418 lines (366 loc) · 10 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package main
import (
"flag"
"github.com/amundi/escheck.v2/config"
"github.com/amundi/escheck.v2/eslog"
"github.com/amundi/escheck.v2/esmail"
"github.com/amundi/escheck.v2/esslack"
"github.com/amundi/escheck.v2/queries"
"github.com/amundi/escheck.v2/worker"
"gopkg.in/olivere/elastic.v3"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
const (
TIMELAYOUT = "Jan 2 15:04:05"
NBWORKERS = 64
)
//manual queries
var g_queryList map[string]queries.Query = map[string]queries.Query{
//put your manual query here
}
type Env struct {
flagsilent *bool
flagcheck *bool
filename *string
queries map[string]config.Query
client *elastic.Client
semaphore chan struct{}
}
func main() {
//get flags and config
env := new(Env)
env.getFlags()
env.getConfig()
//semaphore to queue SendRequest, or else timeout gets wrong
env.semaphore = make(chan struct{}, 1)
//init log, if silent, nothing will be printed on stdout
if *env.flagsilent {
eslog.InitSilent()
} else {
eslog.Init()
}
//init rotating log if necessary
env.initRotatingLog()
//init slack, mail etc.
env.initIntegrations()
//get queries from yaml, if flag -c activated check them and exit
env.parseQueries()
//init the stats for every queries and the dispatcher for workers
initStats()
worker.StartDispatcher(getNbWorkers())
//connect to the elasticsearch cluster via env.client
env.connect()
for name, check := range g_queryList {
go launchQuery(check, name, env)
}
if isServer() {
launchServer()
} else {
//select to wait forever
select {}
}
}
func launchServer() {
path, port := serverPath(), serverPort()
var auth *BasicAuth
eslog.Info("%s : launching server on path %s and port %s", os.Args[0], path, port)
if IsServerAuthentication() {
auth = NewBasicAuth(getServerLogin(), getServerPassword())
auth.setDisplayFunc(collectorDisplay)
http.HandleFunc(path, auth.BasicAuthHandler)
} else {
http.HandleFunc(path, collectorDisplay)
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// function that handles the life of each query
func launchQuery(c queries.Query, name string, env *Env) {
schedule := new(scheduler)
retries := getMaxRetries()
send := new(sender)
stats := queryStats{true, false, retries, 0, "None"}
var query elastic.Query
//query initiation
eslog.Info("%s : getting query configuration", name)
c.SetQueryConfig(config.G_Config.ManualConfig.List)
query, err := c.BuildQuery()
if err != nil {
eslog.Error("%s : failed to build query, %s", name, err.Error())
stats.IsUp = false
if isServer() {
go collectorUpdate(stats, name)
}
return
}
//scheduler and sender initiation
schedInfo, ok := env.queries[strings.ToLower(name)]
if ok {
schedule.initScheduler(&schedInfo)
} else {
schedule.initSchedulerDefault()
}
err = send.initSender(&schedInfo)
if err != nil {
eslog.Error("%s : initSender failed, %s", name, err.Error())
stats.IsUp = false
if isServer() {
go collectorUpdate(stats, name)
}
return
}
eslog.Info("%s : Starting...", name)
//loop forever
for {
//try to send request. If fails, continue while decreasing attempts, or
//die if retries reach 0.
env.semaphore <- struct{}{}
results, err := send.SendRequest(env.client, query)
<-env.semaphore
if err != nil {
eslog.Error(err.Error())
stats.Tries -= 1
if stats.Tries == 0 {
eslog.Error("%s : max attempts reached, stopping query", name)
stats.IsUp = false
if isServer() {
go collectorUpdate(stats, name)
}
return
} else {
//retry after schedule
eslog.Warning("%s : failed to connect, number of attempts left : %d", name, stats.Tries)
if isServer() {
go collectorUpdate(stats, name)
}
schedule.wait()
continue
}
}
// interpet the results, if any
if results != nil && results.Hits != nil && results.Hits.TotalHits > 0 {
//request succeeded, restart attempts
stats.Tries = getMaxRetries()
eslog.Warning("%s : found a total of %d results", name, results.Hits.TotalHits)
yes := c.CheckCondition(results)
if yes {
//condition is verified. Should we enter alert status ?
if !schedule.isAlertOnlyOnce || (schedule.isAlertOnlyOnce && !schedule.alertState) {
eslog.Alert("%s : Action triggered", name)
c.DoAction(results)
schedule.alertState = true
stats.AlertStatus = true
stats.LastAlert = time.Now().Format(TIMELAYOUT)
stats.NbAlerts++
}
} else if !yes {
//condition not verified. Exiting alert status, triggering onAlertEnd() if necessary
if schedule.alertState == true && schedule.isAlertEndMsg == true {
c.OnAlertEnd()
}
schedule.alertState = false
stats.AlertStatus = false
}
} else {
// no results found
eslog.Info("%s : no result found", name)
if schedule.alertState == true && schedule.isAlertEndMsg == true {
c.OnAlertEnd()
}
schedule.alertState = false
stats.AlertStatus = false
}
//update the stats and display them, if necessary
if isServer() {
go collectorUpdate(stats, name)
}
//wait, and do it again
schedule.wait()
}
}
func (e *Env) connect() {
var err error
config := config.G_Config.Config
if config == nil || config.Cluster_addr == "" {
log.Fatal("No config info to start connection ! Check your yml")
}
eslog.Info("%s : connection attempt to %s", os.Args[0], config.Cluster_addr)
if isAuthentication() {
e.client, err = elastic.NewClient(
elastic.SetSniff(false),
elastic.SetURL(config.Cluster_addr),
elastic.SetBasicAuth(getAuthLogin(), getAuthPassword()))
} else {
e.client, err = elastic.NewClient(elastic.SetSniff(false),
elastic.SetURL(config.Cluster_addr))
}
if err != nil {
log.Fatal(err.Error())
} else {
eslog.Info("%s : connection succeeded", os.Args[0])
}
}
func (e *Env) getFlags() {
e.flagcheck = flag.Bool("c", false, "Check the queries from the yml and exit")
e.flagsilent = flag.Bool("q", false, "Silent output")
e.filename = flag.String("f", "config.yml", "Specify config file name")
flag.Parse()
}
// read YAML config and set it in config global variables
func (e *Env) getConfig() {
source, err := ioutil.ReadFile(*e.filename)
if err != nil {
log.Fatal(err)
}
if err = e.setConfig(source); err != nil {
log.Fatal(err)
}
}
func (e *Env) setConfig(source []byte) error {
var ultim config.Config
var manual config.ManualConfig
err := yaml.Unmarshal(source, &ultim)
if err != nil {
return err
}
err = yaml.Unmarshal(source, &manual)
if err != nil {
return err
}
config.G_Config.ManualConfig = &manual
config.G_Config.Config = &ultim
e.queries = ultim.QueryList
return nil
}
func (e *Env) parseQueries() int {
var size int
autoQueryList := getAutoQueryList(e.queries)
//add autoqueries to queryList
if size = len(autoQueryList); size > 0 {
m := initAutoQueries(autoQueryList)
for k, v := range m {
g_queryList[k] = v
if *e.flagcheck {
eslog.Info("%s : added autoquery %s", os.Args[0], k)
}
}
eslog.Info("%s : %d autoqueries added", os.Args[0], size)
}
if *e.flagcheck {
e.checkAndExit()
}
return size
}
// function to check query list, try to init them and build query, and exit with
// displaying relevant errors, if any
func (e *Env) checkAndExit() {
var err bool
var err2 error
var errcount uint32
if len(g_queryList) == 0 {
eslog.Warning("%s : No query added, %d", os.Args[0])
errcount++
}
for k, v := range g_queryList {
eslog.Info("%s : initiating...", k)
err = v.SetQueryConfig(config.G_Config.ManualConfig.List)
if err {
eslog.Error("%s : failed to get config", k)
errcount++
continue
}
_, err2 = v.BuildQuery()
if err2 != nil {
eslog.Error("%s : error while building query, %s", k, err2.Error())
errcount++
}
//test scheduler and sender
schedule := new(scheduler)
send := new(sender)
schedInfo, ok := e.queries[strings.ToLower(k)]
if ok {
if errinit := schedule.initScheduler(&schedInfo); errinit != nil {
eslog.Error("%s : error in schedule info, %s", k, errinit.Error())
errcount++
}
} else {
eslog.Error("%s : error while getting query info in yaml", k)
errcount++
}
if err2 = send.initSender(&schedInfo); err2 != nil {
eslog.Error("%s : error while getting query information, %s", k, err2.Error())
errcount++
}
}
if errcount > 0 {
eslog.Warning("%s : Detected %d errors", os.Args[0], errcount)
} else {
eslog.Info("%s : Everything is ok !", os.Args[0])
}
os.Exit(0)
}
func (e *Env) initIntegrations() {
esmail.Init()
esslack.Init()
}
func (e *Env) initRotatingLog() {
if !*e.flagcheck && isRotatingLog() {
path := config.G_Config.Config.Log_path
if path == "" {
path = "./"
}
err := eslog.InitRotatingLog(
path+"/"+config.G_Config.Config.Log_name,
config.G_Config.Config.Rotate_every,
config.G_Config.Config.Number_of_files,
)
if err != nil {
eslog.Error("%s : "+err.Error(), os.Args[0])
}
} else if !*e.flagcheck && config.G_Config.Config.Log && len(config.G_Config.Config.Log_name) == 0 {
eslog.Error("%s : no filename specified for log", os.Args[0])
}
}
//getters from config
func isServer() bool {
return config.G_Config.Config.Server_mode
}
func serverPath() string {
return config.G_Config.Config.Server_path
}
func serverPort() string {
return config.G_Config.Config.Server_port
}
func getMaxRetries() int {
return config.G_Config.Config.Max_retries
}
func getNbWorkers() int {
return config.G_Config.Config.Workers
}
func isAuthentication() bool {
return len(getAuthLogin()) > 0 && len(getAuthPassword()) > 0
}
func getAuthLogin() string {
return config.G_Config.Config.Auth_login
}
func getAuthPassword() string {
return config.G_Config.Config.Auth_password
}
func isRotatingLog() bool {
return config.G_Config.Config.Log && len(config.G_Config.Config.Log_name) > 0 &&
config.G_Config.Config.Number_of_files > 0 && config.G_Config.Config.Rotate_every > 0
}
func IsServerAuthentication() bool {
return len(config.G_Config.Config.Server_login) > 0 && len(config.G_Config.Config.Server_password) > 0
}
func getServerLogin() string {
return config.G_Config.Config.Server_login
}
func getServerPassword() string {
return config.G_Config.Config.Server_password
}