generated from annihilatorrrr/gotemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
361 lines (339 loc) · 9.11 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"sort"
"strings"
"sync"
"time"
)
type Group struct {
Title string `yaml:"title"`
Checks []Check `yaml:"checks"`
}
type Check struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Host string `yaml:"host"`
Address string `yaml:"address"`
Port int `yaml:"port"`
ExpectedCode int `yaml:"expected_code"`
}
type HistoryEntry struct {
Timestamp string `json:"timestamp"`
Status bool `json:"status"`
}
type GroupCheckResult struct {
Title string
CheckResults []CheckResult
}
type CheckResult struct {
Name string
Status bool
}
func checkHTTP(url string, expectedCode int) bool {
client := &http.Client{Timeout: time.Second * 5}
resp, err := client.Get(url)
if err != nil {
return false
}
_ = resp.Body.Close()
return resp.StatusCode == expectedCode
}
func pingIPv6(address string) bool {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("ping", "-n", "1", "-w", "5000", address)
case "darwin":
cmd = exec.Command("ping", "-c", "1", "-W", "5", address)
default:
cmd = exec.Command("ping", "-6", "-c", "1", "-W", "5", address)
}
return cmd.Run() == nil
}
func checkPing(host string) bool {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("ping", "-n", "1", "-w", "5000", host)
default:
cmd = exec.Command("ping", "-c", "1", "-W", "5", host)
}
return cmd.Run() == nil
}
func checkPort(host string, port int) bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), 5*time.Second)
if err != nil {
return false
}
_ = conn.Close()
return true
}
func runChecks(groups []Group) []GroupCheckResult {
numGroups := len(groups)
results := make([]GroupCheckResult, numGroups)
wg := sync.WaitGroup{}
for idx, group := range groups {
wg.Add(1)
go func(g Group) {
defer wg.Done()
results[idx] = checkGroup(g)
}(group)
}
wg.Wait()
return results
}
func checkGroup(g Group) GroupCheckResult {
numResults := len(g.Checks)
checkResults := make([]CheckResult, numResults)
wg := sync.WaitGroup{}
for idx, check := range g.Checks {
wg.Add(1)
go func(c Check) {
defer wg.Done()
var status bool
switch c.Type {
case "http":
status = checkHTTP(c.Host, c.ExpectedCode)
case "ping":
status = checkPing(c.Host)
case "Port":
status = checkPort(c.Host, c.Port)
case "ipv6":
status = pingIPv6(c.Address)
}
checkResults[idx] = CheckResult{c.Name, status}
}(check)
}
wg.Wait()
return GroupCheckResult{g.Title, checkResults}
}
func (c *Config) loadHistory() map[string][]HistoryEntry {
file, err := os.Open(c.HistoryFile)
if err != nil {
return map[string][]HistoryEntry{}
}
defer func(file *os.File) {
_ = file.Close()
}(file)
var history map[string][]HistoryEntry
_ = json.NewDecoder(file).Decode(&history)
if history == nil {
history = make(map[string][]HistoryEntry)
}
return history
}
func (c *Config) saveHistory(history map[string][]HistoryEntry) {
file, err := os.Create(c.HistoryFile)
if err != nil {
log.Println("Failed to save history:", err)
return
}
defer func(file *os.File) {
_ = file.Close()
}(file)
_ = json.NewEncoder(file).Encode(history)
}
func (c *Config) updateHistory(results []GroupCheckResult) {
history := c.loadHistory()
currentTime := time.Now().Format(time.RFC3339)
for _, group := range results {
for _, result := range group.CheckResults {
name := result.Name
if _, exists := history[name]; !exists {
history[name] = []HistoryEntry{}
}
history[name] = append(history[name], HistoryEntry{currentTime, result.Status})
sort.Slice(history[name], func(i, j int) bool {
timeI, _ := time.Parse(time.RFC3339, history[name][i].Timestamp)
timeJ, _ := time.Parse(time.RFC3339, history[name][j].Timestamp)
return timeI.After(timeJ)
})
if len(history[name]) > c.MaxHistoryEntries {
history[name] = history[name][:c.MaxHistoryEntries]
}
}
}
c.saveHistory(history)
}
func renderTemplate(data map[string]interface{}) string {
tmpl, err := template.New("status").Parse(templateFile)
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
if err = tmpl.Execute(&buf, data); err != nil {
log.Fatal(err)
}
return buf.String()
}
func (c *Config) generateHistoryPage() {
history := c.loadHistory()
tmpl, err := template.New("history").Funcs(template.FuncMap{
"split": func(s, sep string) []string {
return strings.Split(s, sep)
},
}).Parse(historyTemplateFile)
if err != nil {
log.Fatal("Failed to parse history template:", err)
}
data := map[string]interface{}{
"history": history,
"last_updated": time.Now().Format("2006-01-02 15:04:05"),
}
var buf bytes.Buffer
if err = tmpl.Execute(&buf, data); err != nil {
log.Fatal("Failed to execute history template:", err)
}
if err = os.WriteFile(c.HistoryHtmlFile(), buf.Bytes(), 0644); err != nil {
log.Fatal("Failed to write history page:", err)
}
}
func (c *Config) monitorServices() {
for {
groups := c.ReadChecks()
// log.Printf("Groups: %+v", groups)
results := runChecks(groups)
c.updateHistory(results)
data := map[string]interface{}{
"groups": results,
"incidents": template.HTML(c.ReadIncidentHtml()),
"last_updated": time.Now().Format("2006-01-02 15:04:05"),
}
html := renderTemplate(data)
if err := os.WriteFile(c.IndexHtmlFile(), []byte(html), 0644); err != nil {
log.Fatal("Failed to write index.html:", err)
}
c.generateHistoryPage()
log.Println("Status pages updated!")
if c.Token != "" && c.Chatid != "" {
log.Println("Notifying on telegram ...")
for key, hdata := range c.loadHistory() {
if total := len(hdata); total >= 2 {
latestdata := hdata[:2]
if latestdata[0].Status == latestdata[1].Status {
continue
}
lastst := latestdata[1].Status
newinterval := c.CheckInterval
for x, y := range hdata {
if x > 1 {
if y.Status == lastst {
newinterval += 60
} else {
break
}
}
}
tosend := fmt.Sprintf("<b>✅ %s is now Up!</b>\nSeen Down from last %ds!", key, newinterval)
if !latestdata[0].Status {
tosend = fmt.Sprintf("<b> 🛑 %s is now Down!</b>\nWas seen Up from last %ds!", key, newinterval)
}
_ = checkHTTP(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&parse_mode=html&text=%s", c.Token, c.Chatid, url.QueryEscape(tosend)), 200)
} else {
tosend := fmt.Sprintf("<b> 🛑 %s is now Down!</b>", key)
if hdata[0].Status {
tosend = fmt.Sprintf("<b>✅ %s is now Up!</b>", key)
}
_ = checkHTTP(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&parse_mode=html&text=%s", c.Token, c.Chatid, url.QueryEscape(tosend)), 200)
}
}
log.Println("Notified on telegram!")
}
time.Sleep(time.Duration(c.CheckInterval) * time.Second)
}
}
func serveFile(w http.ResponseWriter, r *http.Request, filePath string) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
http.NotFound(w, r)
return
}
http.ServeFile(w, r, filePath)
}
func handleHome(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed!", http.StatusMethodNotAllowed)
return
}
_, _ = fmt.Fprintf(w, `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Service Status</title>
<style>
body {
background-color: #121212;
color: #e0e0e0;
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
}
h1 {
color: #bb86fc;
}
pre {
background-color: #1e1e1e;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
}
</style>
</head>
<body>
<h1>I'm alive!</h1>
<p>Go Version: %s</p>
<p>Go Routines: %d</p>
<p>Source Code: <a href="https://github.com/annihilatorrrr/gotinystatus" style="color: #bb86fc;">Gotinystatus</a></p>
</body>
</html>`, runtime.Version(), runtime.NumGoroutine())
}
func main() {
c := readEnv()
log.Println("Monitoring services ...")
// c.PrintEnv()
if c.Port != 0 {
log.Printf("Listening on host: %s\n", c.ListenHost())
go c.monitorServices()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
serveFile(w, r, "./"+c.IndexHtmlFile())
} else {
http.NotFound(w, r)
}
})
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/status") {
handleHome(w, r)
} else {
http.NotFound(w, r)
}
})
http.HandleFunc("/history.html", func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/history") {
serveFile(w, r, "./"+c.HistoryHtmlFile())
} else {
http.NotFound(w, r)
}
})
log.Println("Server started!")
if err := http.ListenAndServe(c.ListenHost(), nil); err != nil {
log.Println(err.Error())
}
} else {
c.monitorServices()
}
log.Println("Bye!")
}