-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
276 lines (234 loc) · 6.83 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
package main
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"strconv"
"time"
"github.com/r3labs/sse/v2"
"gopkg.in/yaml.v2"
)
type Service struct {
Name string `yaml:"name" json:"name"`
Endpoint string `yaml:"endpoint"`
Frequency time.Duration `yaml:"frequency"`
ExpectedCode int `yaml:"expectedCode"`
ContainsString string `yaml:"containsString"`
HttpMethod string `yaml:"httpMethod"`
DisableAlerts bool `yaml:"disableAlerts"`
UserAgent string `yaml:"userAgent"`
up *bool
ack bool
}
var webhookSlackURL string = os.Getenv("SLACK_WEBHOOK_URL")
func checkService(s Service) (bool, error) {
var req *http.Request
var err error
// Create an HTTP request based on the specified method
switch s.HttpMethod {
case "GET":
req, err = http.NewRequest("GET", s.Endpoint, nil)
case "POST":
req, err = http.NewRequest("POST", s.Endpoint, nil)
default:
// Default to GET if the method is not specified or invalid
req, err = http.NewRequest("GET", s.Endpoint, nil)
}
if err != nil {
return false, err
}
// Set the User-Agent header if specified
if s.UserAgent != "" {
req.Header.Set("User-Agent", s.UserAgent)
}
// Send the HTTP request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
// If there is no ExpectedCode, default 200
if s.ExpectedCode == 0 {
s.ExpectedCode = 200
}
// Check the HTTP status code
if resp.StatusCode != s.ExpectedCode {
return false, fmt.Errorf(resp.Status)
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
// Check if the specified string exists in the response content
if s.ContainsString != "" {
if !bytes.Contains(body, []byte(s.ContainsString)) {
return false, fmt.Errorf("response does not contain: %s", s.ContainsString)
}
}
return true, nil
}
func sendStream(server *sse.Server, s Service, err error) {
serviceData := map[string]any{
"name": s.Name,
"endpoint": s.Endpoint,
"frequency": s.Frequency.Seconds(),
"expectedCode": s.ExpectedCode,
"up": s.up,
"ack": s.ack,
"error": "",
}
// If there's an error, set the error field in the map
if err != nil {
serviceData["error"] = err.Error()
}
// Serialize the map to JSON
jsonData, err := json.Marshal(serviceData)
if err != nil {
// Handle the error
slog.Error("Error marshaling JSON: %v\n", err)
return
}
// Publish the JSON data as an SSE event
server.Publish("messages", &sse.Event{
Data: jsonData,
})
}
func handleNotification(s *Service, up bool, err error) {
if s.DisableAlerts {
s.up = &up
return
}
// Recovering Alert
if up && s.up != nil && !*s.up {
sendSlackNotification(fmt.Sprintf("🟩 *<%s|%s>* returning *%v*", s.Endpoint, s.Name, s.ExpectedCode))
s.ack = false
}
s.up = &up // update s.up so its used for the recovering alert on next run in case is false
// Down Alert
if err != nil && !s.ack {
sendSlackNotification(fmt.Sprintf("🟥 *<%s|%s>* returning *%s*", s.Endpoint, s.Name, err.Error()))
}
}
func sendSlackNotification(message string) {
if webhookSlackURL == "" {
return
}
// disable notifications while developing with an early return TODO
// return
message = strconv.Quote(message)
data := fmt.Sprintf(`{"text":%s}`, message)
// Create a POST request with the JSON data
req, err := http.NewRequest("POST", webhookSlackURL, bytes.NewBuffer([]byte(data)))
if err != nil {
slog.Error("Error creating HTTP request:", err)
return
}
// Set the Content-Type header to application/json
req.Header.Set("Content-type", "application/json")
// Perform the HTTP request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
slog.Error("Error sending Slack notification: %v", err)
return
}
defer resp.Body.Close()
// Check the response status
if resp.Status == "200 OK" {
slog.Info("Slack notification sent successfully")
}
}
//go:embed templates/* static/*
var templatesFS embed.FS
func updateAckStatus(services []*Service, serviceName string, ack bool) {
for _, service := range services {
if service.Name == serviceName {
service.ack = ack
fmt.Println("ack status updated for service:", serviceName)
break
}
}
}
var services []*Service
func main() {
// Read the service.yaml file
yamlFile, err := os.ReadFile("config.yaml")
if err != nil {
log.Fatalf("Error reading YAML file: %v", err)
}
// Unmarshal the YAML data into the services slice
if err := yaml.Unmarshal(yamlFile, &services); err != nil {
sendSlackNotification("❌ Error reading the config.yaml file, inxidents will exit and no services will be monitored. Please correct config.yaml and restart the app.")
log.Fatalf("Error unmarshaling YAML: %v", err)
}
server := sse.New()
server.AutoReplay = false
server.CreateStream("messages")
// Create a new Mux and set the handler
http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
server.ServeHTTP(w, r)
})
http.HandleFunc("/ack", func(w http.ResponseWriter, r *http.Request) {
// Check if the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Decode the JSON payload
var requestBody Service
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&requestBody)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
updateAckStatus(services, requestBody.Name, true)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"message": "Request received successfully"}`)
})
http.HandleFunc("/up", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// add an /unstable endpoint that returns 200 and 503 - for testing
http.HandleFunc("/unstable", func(w http.ResponseWriter, r *http.Request) {
// Return with 200 OK if the current time seconds are odd
if time.Now().Unix()%2 == 1 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
// Serve the index.html file from the embedded file system
content, err := templatesFS.ReadFile("templates/index.html")
if err != nil {
http.Error(w, "Unable to read index.html", http.StatusInternalServerError)
return
}
w.Write(content)
})
for _, service := range services {
go func(s *Service) {
for {
up, err := checkService(*s)
handleNotification(s, up, err)
sendStream(server, *s, err)
time.Sleep(s.Frequency)
}
}(service)
}
http.Handle("/static/", http.FileServer(http.FS(templatesFS)))
http.ListenAndServe("0.0.0.0:8080", nil)
}