-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (137 loc) · 3.8 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
package main
import (
"errors"
"math/rand"
"strings"
"sync"
"time"
"git.iglou.eu/xavierSrv/config"
"git.iglou.eu/xavierSrv/tools"
)
var wg sync.WaitGroup
func main() {
//init:
rand.Seed(42)
config.Global.Init(&config.Check, &config.Error)
tools.InitLogConf(
config.Global.Overall.LogFile,
config.Global.Overall.LogVerbose,
)
//loop:
for index, team := range config.Check.Team {
if team.Enable {
wg.Add(1)
go xTeam(index)
}
}
wg.Wait()
time.Sleep(time.Duration(config.Global.Overall.LoopWait))
//goto loop
}
func xTeam(teamID int) bool {
team := &config.Check.Team[teamID]
var failures [][]string
for _, app := range team.App {
tools.PushToLog(6, errors.New("Checking '"+app.Name+"' ..."))
if _, err := cerebro(app.URL, app.Response); err != nil {
returned := []string{app.Name, err.Error()}
failures = append(failures, returned)
tools.PushToLog(6, err)
}
}
if failures != nil {
xTeamReport(team.Name, failures)
}
defer wg.Done()
return true
}
func xTeamReport(team string, failures [][]string) (bool, error) {
//var message string {"name":"","error":""}
if config.Global.Reporting.Local {
//report.LocalSave(team, report.LocalBuild(failures))
}
// Build message
reportMessage := ""
for i := range failures {
reportMessage += "[" + failures[i][0] + "]" + "\n" +
failures[i][1] + "\n"
}
// for reporting
for _, reportProcess := range config.Error.Team[team].Report {
switch reportProcess.Process {
case "smtp":
subject := strings.ReplaceAll(reportProcess.Subject, "[%TEAM]", team)
message := strings.ReplaceAll(reportProcess.Body, "[%TEAM]", team)
message = strings.ReplaceAll(message, "[%ERRORS]", reportMessage)
tools.SmtpReport(
reportProcess.Host,
reportProcess.Encrypt,
reportProcess.From,
reportProcess.User,
reportProcess.Passwd,
reportProcess.Recipients,
subject, message,
)
case "http":
message := strings.ReplaceAll(reportProcess.Body, "[%TEAM]", team)
if reportProcess.Methods == "POST" {
message = strings.ReplaceAll(message, "[%ERRORS]", tools.JsonEscapeString(reportMessage))
} else {
message = strings.ReplaceAll(message, "[%ERRORS]", reportMessage)
}
tools.HttpReport(reportProcess.Methods, reportProcess.URL, message)
default:
tools.PushToLog(3, errors.New("Unknow '"+reportProcess.Process+"' reporting process"))
}
}
// exec type
/*if config.Global.Reporting.ReportJSON {
jsonFile := "{\"status\":\"error\",\"date\":\""
jsonFile += time.Now().Format(time.RFC3339)
jsonFile += "\",\"listing\":["
i, l := 0, len(failures)
for {
if i%2 == 0 {
jsonFile += "{\"name\":\"" + failures[i] + "\""
} else {
jsonFile += ",\"error\":\"" + failures[i] + "\"},"
}
i++
if i == l {
jsonFile = jsonFile[:len(jsonFile)-1]
break
}
}
jsonFile += "]}"
fmt.Println(jsonFile)
}*/
/*
Si on a une erreur
- on construit le json de la team
- on formate le message de sortie
On boucle sur les reports
- pour les reports smtp on envoi le message
- pour les retapi on export les info format json puis send
Ne pas oublier de replace les balises [%***]
*/
return true, nil
}
func cerebro(url string, need int) (bool, error) {
xPsyAgatha, errAgatha := tools.HttpStatus(url, need, config.Global.HTTP.MaxWait)
xPsyArthur, errArthur := tools.HttpStatus(url, need, config.Global.HTTP.MaxWait)
xPsyDash, errDash := tools.HttpStatus(url, need, config.Global.HTTP.MaxWait)
if (xPsyAgatha && xPsyArthur) ||
(xPsyAgatha && xPsyDash) ||
(xPsyArthur && xPsyDash) {
return true, nil
}
err := errors.New("Inconsistent error returned")
if errAgatha.Error() == errArthur.Error() {
err = errAgatha
} else if errArthur.Error() == errDash.Error() {
err = errArthur
} else if errDash.Error() == errAgatha.Error() {
err = errDash
}
return false, err
}