-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (52 loc) · 1.38 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
package main
import (
"bytes"
"encoding/json"
"flag"
"log"
"net/http"
"time"
)
func main() {
site := flag.String("site", "", "The site to check the health of")
webhook := flag.String("webhook", "", "The webhook url to hit with the health status")
interval := flag.Duration("interval", 30*time.Minute, "The interval at which to check the site. Uses go time strings")
flag.Parse()
start(*site, *webhook, *interval)
log.Println("Closed pingy")
}
func start(site, webhook string, interval time.Duration) {
log.Println("Started pingy")
for {
if !isHealthy(site) {
log.Printf("%s is UN-healthy, sending report", site)
reportUnhealthy(webhook)
} else {
log.Printf("%s is healthy", site)
}
time.Sleep(interval)
}
}
func isHealthy(site string) bool {
resp, err := http.Head(site)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
func reportUnhealthy(webhook string) {
report := map[string]string{"text": "Site cannot be reached"}
json, err := json.Marshal(report)
if err != nil {
log.Println("Could not create json body for report")
return
}
log.Printf("Reporting unhealthy site, sending %s to %s", json, webhook)
resp, err := http.Post(webhook, "application/json", bytes.NewBuffer(json))
if err != nil {
log.Printf("Could not make request to webhook")
}
log.Printf("+%v", resp)
defer resp.Body.Close()
}