-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (122 loc) · 3.79 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
package main
import (
"context"
"flag"
"os"
"sync"
"time"
"github.com/rbeuque74/jagozzi/config"
"github.com/rbeuque74/jagozzi/plugins"
_ "github.com/rbeuque74/jagozzi/plugins/command"
_ "github.com/rbeuque74/jagozzi/plugins/http"
_ "github.com/rbeuque74/jagozzi/plugins/marathon"
_ "github.com/rbeuque74/jagozzi/plugins/processes"
_ "github.com/rbeuque74/jagozzi/plugins/ssl"
_ "github.com/rbeuque74/jagozzi/plugins/supervisor"
log "github.com/sirupsen/logrus"
)
// nolint: gochecknoglobals
var (
configFile = flag.String("cfg", "./jagozzi.yml", "path to config file")
logLevel = flag.String("level", "info", "verbosity level for application logs")
guiConsumer = flag.Bool("display", false, "set display to true if GUI need to display plugins results")
oneShot = flag.Bool("oneShot", false, "run jagozzi one time, no periodic checker (useful when used on cron)")
version string
)
func main() {
flag.Parse()
applyLogLevel(logLevel)
log.Infof("jagozzi - %s", version)
ctx, cancel := context.WithCancel(context.Background())
exiting := exitSignalHandling(cancel)
go exitTimeout(ctx)
cfg, err := config.Load(*configFile)
if err != nil {
log.Fatal(err)
} else if ctx.Err() != nil {
log.Info("main context already closed")
os.Exit(0)
}
jag, err := Load(*cfg)
if err != nil {
log.Fatal(err)
} else if ctx.Err() != nil {
log.Info("main context already closed")
os.Exit(0)
}
var wg sync.WaitGroup
jag.runMainLoop(ctx, &wg)
if !*oneShot {
<-exiting
}
log.Debug("jagozzi: waiting for all goroutines")
wg.Wait()
log.Debug("jagozzi: subroutines exited")
jag.Unload()
log.Debug("jagozzi: unloading complete; exit successful")
}
func (jag Jagozzi) runMainLoop(ctx context.Context, wg *sync.WaitGroup) {
checkersPerPeridicity := map[time.Duration][]plugins.Checker{}
for _, checker := range jag.Checkers() {
periodicity := jag.cfg.Periodicity
if p := checker.Periodicity(); p != nil {
periodicity = *p
}
var currentArray []plugins.Checker
if array, ok := checkersPerPeridicity[periodicity]; ok {
currentArray = array
}
currentArray = append(currentArray, checker)
checkersPerPeridicity[periodicity] = currentArray
}
for loopPeriodicity, loopCheckers := range checkersPerPeridicity {
periodicity := loopPeriodicity
checkers := loopCheckers
wg.Add(1)
go func() {
defer wg.Done()
timeout := periodicity * time.Duration(2)
cancellationTimeout := timeout + time.Second
ticker := time.NewTicker(periodicity)
defer ticker.Stop()
log := log.WithField("periodicity", periodicity.String())
log.Debug("loop: starting")
for {
select {
case t := <-ticker.C:
log.Debugf("triggered: %s", t)
// context should never be cancelled as we are canceling the parent
loopCtx, cancelCtx := context.WithTimeout(ctx, timeout)
time.AfterFunc(cancellationTimeout, func() {
cancelCtx()
})
for _, checker := range checkers {
wg.Add(1)
go jag.runChecker(loopCtx, checker, wg)
}
case <-ctx.Done():
log.Debug("loop: main context closed, exiting")
return
}
if *oneShot {
log.Debug("loop: one shot activated, exiting")
return
}
}
}()
}
}
func (jag Jagozzi) runChecker(ctx context.Context, checker plugins.Checker, wg *sync.WaitGroup) {
defer wg.Done()
log := log.WithFields(log.Fields{"name": checker.Name(), "serviceName": checker.ServiceName()})
log.Debugf("perform check")
result := checker.Run(ctx)
if ctx.Err() != nil && ctx.Err() == context.Canceled {
log.Debug("jagozzi: context cancelled while running checker")
return
} else if ctx.Err() != nil && ctx.Err() == context.DeadlineExceeded {
log.Errorf("jagozzi: context timed out while running checker: %s", checker.Name())
}
log.Debugf("checker: result was %q", result.Message)
jag.SendConsumers(result)
}