-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectivity.go
175 lines (155 loc) · 3.74 KB
/
connectivity.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
package main
import (
"log"
"os"
"sync"
)
/*
This module is responsible for parsing CLI arguments and return codes. It
handles the top-level subcommands and main loops for each (check, wait, and
monitor). All goroutines are managed here.
*/
func main() {
if len(os.Args) == 1 {
PrintUsage()
os.Exit(0)
}
command := os.Args[1]
if command == "validate-config" {
var configPath string
var err error
if len(os.Args) == 3 {
configPath = os.Args[2]
} else {
configPath, err = FindConfig()
if err != nil {
log.Fatal(err)
}
}
config := LoadConfig(configPath)
destinations := ParseDestinations(config.URLs)
ShowDestinations(destinations)
} else if command == "check" {
configPath, _ := FindConfig()
config := LoadConfig(configPath)
go StatsdSender(config)
urls := GetURLs(config)
destinations := ParseDestinations(urls)
log.Print("Checking all connectivity...")
ShowDestinations(destinations)
if CheckLoop(destinations) {
os.Exit(0)
} else {
os.Exit(1)
}
} else if command == "wait" || command == "waitfor" {
configPath, _ := FindConfig()
config := LoadConfig(configPath)
go StatsdSender(config)
urls := GetURLs(config)
destinations := ParseDestinations(urls)
ShowDestinations(destinations)
log.Print("Waiting until all connectivity is validated...")
WaitLoop(destinations)
} else if command == "monitor" {
configPath, _ := FindConfig()
config := LoadConfig(configPath)
go StatsdSender(config)
urls := GetURLs(config)
destinations := ParseDestinations(urls)
ShowDestinations(destinations)
log.Print("Monitoring connectivity...")
MonitorLoop(destinations)
} else if command == "version" {
PrintVersion()
} else if command == "help" {
if len(os.Args) == 3 {
// connectivity help <subcommand>
if PrintCommandUsage(os.Args[2]) {
// Command usage for this argument was found
os.Exit(0)
} else {
// Invalid subcommand
os.Exit(2)
}
} else {
// connectivity help
PrintUsage()
}
} else {
PrintUsage()
// Invalid subcommand
os.Exit(2)
}
}
func GetURLs(config *Config) []Url {
if len(os.Args) > 2 {
// Ignore URLs in the config file and use the ones from the CLI instead
config.URLs = []Url{}
for _, url := range os.Args[1:len(os.Args)] {
config.URLs = append(config.URLs, Url{Url: url})
}
}
return config.URLs
}
func ParseDestinations(urls []Url) []*Destination {
// Validate all destinations before beginning any monitoring
errEncountered := false
var destinations []*Destination
for idx, url := range urls {
if idx != 0 {
dest, err := NewDestination(url)
if err != nil {
log.Printf("%s", err)
errEncountered = true
} else {
destinations = append(destinations, dest)
}
}
}
if errEncountered {
os.Exit(2)
}
return destinations
}
func ShowDestinations(destinations []*Destination) {
if len(destinations) == 0 {
log.Print("Failed to parse any destinations")
os.Exit(1)
}
log.Print("Parsed the following destinations:")
for _, dest := range destinations {
LogDestination(dest, dest.UrlString())
}
}
func CheckLoop(destinations []*Destination) bool {
// Assume all destinations are reachable until proven otherwise
reachable := true
// Check destinations sequentially, which is slow, but fixes issue #2
for _, dest := range destinations {
if dest.Check() {
LogDestination(dest, "Connected")
} else {
reachable = false
}
}
return reachable
}
func WaitLoop(destinations []*Destination) {
var wg sync.WaitGroup
for _, dest := range destinations {
wg.Add(1)
go func(dest *Destination) {
defer wg.Done()
dest.WaitFor()
}(dest)
}
wg.Wait()
}
func MonitorLoop(destinations []*Destination) {
for _, dest := range destinations {
go dest.Monitor()
}
// Sleep forever
select {}
}