-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (97 loc) · 2.84 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/urfave/cli"
)
type Environment struct {
MonitoredURLs []string `required:"true" envconfig:"MONITORED_URLS"`
}
type Notifier interface {
Notify(message string) bool
}
func main() {
var notifierParameter string
err := godotenv.Load("/go/bin/.env")
if err != nil {
log.Println("No .env file found, falling back to environment variables")
}
var e Environment
err = envconfig.Process("checker", &e)
if err != nil {
log.Fatalf("envconfig.Process: %w", err)
}
app := cli.NewApp()
app.Name = "checker"
app.Usage = "Check if URLs are up"
app.Flags = []cli.Flag {
&cli.StringFlag{
Name: "notifier, n",
Value: "slack",
Usage: "Chose a notifier. Supported values : slack",
Destination: ¬ifierParameter,
},
}
app.Commands = []*cli.Command{
{
Name: "check",
Aliases: []string{"c"},
Usage: "check if URLs are up",
Action: func(c *cli.Context) error {
var statusCode int
notifier := getNotifier(notifierParameter)
for _, monitoredURL := range e.MonitoredURLs {
log.Println(fmt.Sprintf("Visiting %v", monitoredURL))
resp, err := http.Get(monitoredURL)
if err != nil {
log.Println(fmt.Sprintf("Could not connect to url %s : %s", monitoredURL, err))
statusCode = 0
} else {
log.Println(fmt.Sprintf("Response for url %s : %d", monitoredURL, resp.StatusCode))
statusCode = resp.StatusCode
}
switch statusCode {
case 200:
log.Println(fmt.Sprintf("URL %s is up, nothing to do.", monitoredURL))
case 0:
log.Println(fmt.Sprintf("URL %s is not up (connection error), notifying", monitoredURL))
message := fmt.Sprintf(":warning: Alert, site %s looks down (connection error : %s) ! :warning:", monitoredURL, err)
notifier.Notify(message)
default:
log.Println(fmt.Sprintf("URL %s is not up (code %d), notifying", monitoredURL, statusCode))
message := fmt.Sprintf(":warning: Alert, site %s looks down (status code %d) ! :warning:", monitoredURL, statusCode)
notifier.Notify(message)
}
}
return nil
},
},
{
Name: "test",
Aliases: []string{"t"},
Usage: "Test notifier settings",
Action: func(c *cli.Context) error {
log.Println("Sending test message")
notifier := getNotifier(notifierParameter)
notifier.Notify(":warning: Alert, this is a test message for checker !! :warning:")
return nil
},
},
}
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getNotifier(notifierParameter string) (Notifier) {
switch notifierParameter {
case "slack":
return makeSlackNotifier()
}
log.Println("No notifier found, or notifier is not valid. Falling back to slack notifier")
return makeSlackNotifier()
}