-
Notifications
You must be signed in to change notification settings - Fork 5
/
worker.go
171 lines (146 loc) · 3.54 KB
/
worker.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
package main
import (
"fmt"
"time"
"io/ioutil"
"encoding/json"
"encoding/base64"
"net/http"
"net/smtp"
"os"
"github.com/facebookgo/httpcontrol"
)
type Config struct {
From string
Wait int
Timeout int
Recipient string
Sites []string
}
type Site struct {
Url string
Status int
}
func check(e error) {
if e != nil {
panic(e)
}
}
var sites = make([]Site, 0)
var config Config
/*
* sends email when site status changes
*/
func notify(status string, site *Site) {
api_key := os.Getenv("POSTMARK_API_KEY")
mail_server := os.Getenv("POSTMARK_SMTP_SERVER") + ":25"
auth := smtp.CRAMMD5Auth(
api_key,
api_key,
)
from := config.From
stat := http.StatusText(site.Status)
time := time.Now().Format("Mon Jan 02 15:04:05 2006")
body := "<center><h3>" + site.Url + " is " + status + "</h3>"
body += fmt.Sprintf("<p><strong>%s (%d)</strong> @ %s</p>", stat, site.Status, time)
body += "<br><br></center>"
header := make(map[string]string)
header["From"] = from
header["To"] = config.Recipient
header["Subject"] = "Monitor - " + site.Url + " - " + status
header["Content-Type"] = "text/html; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
err := smtp.SendMail(
mail_server,
auth,
from,
[]string{config.Recipient},
[]byte(message),
)
if err != nil {
fmt.Println("Unable to notify via email")
fmt.Println(err)
}
}
/*
* recursive func that sends websites to the jobs queue.
* runs every config.Wait seconds.
*/
func perform(jobs chan<- *Site, results <-chan int) {
for j := 0; j < len(sites); j++ {
jobs <- &sites[j]
}
for a := 0; a < len(sites); a++ {
<-results
}
select {
case <-time.After(time.Duration(config.Wait) * time.Second):
perform(jobs, results)
}
}
/*
* goroutine that processes a website and reports its status.
*/
func worker(id int, jobs <-chan *Site, results chan<- int) {
for j := range jobs {
status := check_http_status(j.Url)
if status != j.Status {
// status changed, lets notify
state := "UP"
if status <= 399 && j.Status > 399 { state = "BACK UP" }
if status > 399 { state = "DOWN" }
fmt.Println("[", id, "] - ", j.Url, "is ", state, " -", status)
j.Status = status
if state == "BACK UP" || state == "DOWN" {
notify(state, j)
}
}
j.Status = status
results <- j.Status
}
}
/*
* send GET request to url.
* Returns status code.
*/
func check_http_status(url string) (int) {
status := 408 // Something went wrong, default to 'ClientRequestTimeout'
client := &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: time.Duration(config.Timeout) * time.Second,
MaxTries: 3,
},
}
res, err := client.Head(url)
if err != nil {
fmt.Println("Unable to check")
fmt.Println(err)
}
if res != nil {
defer res.Body.Close()
status = res.StatusCode
}
return status
}
func main() {
jobs := make(chan *Site, 100)
results := make(chan int, 100)
done := make(chan bool, 1)
f, err := ioutil.ReadFile("./config.json")
check(err)
json_err := json.Unmarshal(f, &config)
check(json_err)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for s := 0; s < len(config.Sites); s++ {
sites = append(sites, Site{Url: config.Sites[s], Status: 0})
}
perform(jobs, results)
<- done
}