-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (65 loc) · 1.56 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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/slack-go/slack"
"gopkg.in/yaml.v2"
)
type conf struct {
Endpoints []struct {
Url string `yaml:"url"`
Timeout float64 `yaml:"timeout"`
} `yaml:"endpoints"`
}
func getConf(c *conf) {
yamlFile, err := ioutil.ReadFile("conf.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
}
func SendSlackMessage(message string, failReason string) {
slackWebHook := os.Getenv("SLACKWEBHOOK")
attachment := slack.Attachment{
Color: "danger",
Text: message,
Footer: failReason,
}
msg := slack.WebhookMessage{
Attachments: []slack.Attachment{attachment},
}
err := slack.PostWebhook(slackWebHook, &msg)
if err != nil {
log.Println(err)
}
}
func main() {
var configs conf
getConf(&configs)
for _, config := range configs.Endpoints{
start := time.Now()
resp, err := http.Get(config.Url)
elapsed := time.Since(start).Seconds()
defer resp.Body.Close()
if err != nil {
log.Println(err)
SendSlackMessage(config.Url + " failed", "Didn't get any response")
} else {
log.Println(config.Url, resp.StatusCode)
if resp.StatusCode != 200 {
SendSlackMessage(config.Url + " failed", "StatusCode:" + strconv.Itoa(resp.StatusCode))
}
if elapsed > config.Timeout {
SendSlackMessage(config.Url + " failed", "Reply toke " + strconv.FormatFloat(elapsed,'E', -1, 32))
}
log.Println(elapsed)
}
}
}