This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
163 lines (131 loc) · 3.25 KB
/
checker.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
package main
import (
"log"
"net/http"
"strings"
"time"
)
// CheckResult is the result for check instance status.
type CheckResult struct {
Message string
Status bool
}
// checkInstances gets the statuses of instances, and return the unreachable
// instance list.
func checkInstances(config *Config) []string {
ch := make(chan CheckResult)
expect := getInstanceCount(config)
actual := 0
messages := make([]string, 0, expect)
if config.Instances != nil {
for _, instance := range config.Instances {
go checkInstance(instance, config, ch)
}
}
if config.Groups != nil {
for _, group := range config.Groups {
go checkGroup(group, config, ch)
}
}
for res := range ch {
if !res.Status {
messages = append(messages, res.Message)
}
actual++
if expect == actual {
close(ch)
}
}
return messages
}
// getInstanceCount gets the count of instances including groups.
func getInstanceCount(config *Config) int {
count := 0
if config.Instances != nil {
count += len(config.Instances)
}
if config.Groups != nil {
count += len(config.Groups)
}
return count
}
// checkGroup gets the statuses of instances of the group.
func checkGroup(group InstanceGroup, config *Config, ch chan CheckResult) {
instancesChan := make(chan CheckResult)
num := 0
failed := 0
messages := make([]string, 0, len(group.Instances))
var result CheckResult
result.Status = true
for _, instance := range group.Instances {
go checkInstance(instance, config, instancesChan)
}
for res := range instancesChan {
if !res.Status {
messages = append(messages, res.Message)
failed++
}
num++
if num == len(group.Instances) {
close(instancesChan)
}
}
if group.Type != "all" && group.Type != "any" {
// The default type is 'all'
group.Type = "all"
}
switch group.Type {
case "any":
if failed > 0 {
// The status of the group will be false when some instances were failed.
result.Status = false
}
case "all":
if failed == len(group.Instances) {
// The status of the group will be false when all instances were failed.
result.Status = false
}
}
if !result.Status {
result.Message = "Check group " + group.Name + " failed:\n\t" + strings.Join(messages, "\n\t")
}
ch <- result
}
// checkInstance sends request to specifc server and creates report if it has failed.
func checkInstance(
instance Instance,
config *Config,
ch chan CheckResult,
) {
var result CheckResult
// Set to global uri if no specific uri in instance config.
url := getRequestURL(instance, config)
client := http.Client{
Timeout: time.Duration(config.Timeout) * time.Second,
}
result.Status = true
_, err := client.Get(url)
if err != nil {
log.Printf("Failed to open url %s: %v", url, err)
result.Status = false
result.Message = "Check instance " + instance.Addr + " failed (error: " + err.Error() + ")"
}
ch <- result
}
// getRequestURL make destination url by instance or global config.
func getRequestURL(instance Instance, config *Config) string {
url := ""
if instance.Scheme != "" {
url = instance.Scheme + "://"
} else if config.Scheme != "" {
url = config.Scheme + "://"
} else {
url = "http://"
}
if instance.URI != "" {
url += instance.Addr + instance.URI
} else {
url += instance.Addr + config.URI
}
return url
}