-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
212 lines (168 loc) · 4.84 KB
/
main.go
File metadata and controls
212 lines (168 loc) · 4.84 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/fatih/color"
)
const jStartUrl string = "www.ibm.com/jstart"
var green (func(string, ...interface{}) string) = color.New(color.FgGreen, color.Bold).SprintfFunc()
var yellow (func(string, ...interface{}) string) = color.New(color.FgYellow, color.Bold).SprintfFunc()
var red (func(string, ...interface{}) string) = color.New(color.FgRed, color.Bold).SprintfFunc()
var cyan (func(string, ...interface{}) string) = color.New(color.FgCyan).SprintfFunc()
type retryClient struct {
maxRetries int
userAgent string
http.Client
}
var client = &retryClient{}
func Client(timeout, maxRetries int) {
client.Timeout = time.Duration(timeout) * time.Second
client.maxRetries = maxRetries
client.userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
}
func (c *retryClient) newRequest(url string) (*http.Request, error) {
request, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return nil, fmt.Errorf("Error creating request: %s", err)
}
request.Header.Set("User-Agent", c.userAgent)
request.Close = true
return request, nil
}
func readLocalSource(filepath string) ([]byte, error) {
source, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
return source, nil
}
func readWebSource(sourceUrl string) ([]byte, error) {
response, err := http.Get(sourceUrl)
if err != nil {
return nil, err
}
if !strings.HasPrefix(response.Status, "2") {
return nil, fmt.Errorf("%s at %s", response.Status, sourceUrl)
}
source, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return source, nil
}
func getData(locations []string) ([][]byte, error) {
var tempData []byte
var dataList [][]byte
var err error
for _, location := range locations {
if strings.HasPrefix(location, "http") {
tempData, err = readWebSource(location)
} else {
tempData, err = readLocalSource(location)
}
if err != nil {
return nil, err
}
dataList = append(dataList, tempData)
}
return dataList, nil
}
func getAvailability(status string) bool {
if strings.HasPrefix(status, "2") {
return true
}
return false
}
func formatUrl(url string) (string, error) {
if strings.Contains(url, "*.") {
url = strings.Replace(url, "*.", "", -1)
}
if !strings.HasPrefix(url, "http") {
isMatch, err := regexp.MatchString(".*:[0-9]+$", url)
if err != nil {
return "", fmt.Errorf("Failed to build regex: %s", err)
}
if !isMatch {
url += ":80"
}
}
return url, nil
}
func formatStatus(url, status string) string {
formattedStatus := ""
if strings.HasPrefix(status, "2") || status == "TCP Dial OK" {
formattedStatus += fmt.Sprintf("%s", green(status))
} else if strings.HasPrefix(status, "3") {
formattedStatus += fmt.Sprintf("%s", yellow(status))
} else if strings.HasPrefix(status, "4") || strings.HasPrefix(status, "5") {
formattedStatus += fmt.Sprintf("%s", red(status))
} else {
formattedStatus += fmt.Sprintf("%s %s", red("FAILED:"), status)
}
if strings.Contains(url, "*.") {
formattedStatus += fmt.Sprintf("\n\t %s Wildcards unsupported, reporting for %s ",
yellow("WARNING:"), yellow(strings.Replace(url, "*.", "", -1)))
}
return formattedStatus
}
func parseArgs() ([][]byte, bool, error) {
timeout := flag.Int("t", 60, "http request timeout (in seconds)")
retries := flag.Int("r", 0, "number of http request retries")
quiet := flag.Bool("q", false, "only display status for failed requests")
noColor := flag.Bool("c", false, "disable color output")
flag.Parse()
if *noColor {
color.NoColor = true
}
Client(*timeout, *retries)
locations := flag.Args()
if len(locations) == 0 {
return nil, false, fmt.Errorf("No YAML file(s) provided")
}
sourceData, err := getData(locations)
if err != nil {
return nil, false, err
}
return sourceData, *quiet, nil
}
func printUsage(err error) {
usage := red("Invalid arguments: ") + err.Error() + "\n\n" +
cyan("USAGE:") + " ./bx-availability [-t seconds] [-r retries] [-q] [-c] " +
"YAML_file_location [YAML_file_location...]\n\n" +
cyan("OPTIONS:") + " t - http request timeout (in seconds) (default 60)\n" +
" r - number of http request retries (default 0)\n" +
" q - only display status for failed requests\n" +
" c - disable color output\n"
fmt.Print(usage)
os.Exit(1)
}
func main() {
sourceData, quiet, err := parseArgs()
if err != nil {
printUsage(err)
}
services, err := ServiceList(sourceData, quiet)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
done := make(chan int)
go services.write(done)
for i, _ := range services.Services {
services.testService(i)
_ = <-done
}
close(services.output)
close(done)
if !quiet {
services.displayResults()
}
fmt.Printf("\nCourtesy of %s - %s\n", cyan("IBM jStart"), jStartUrl)
}