-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (89 loc) · 2.3 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/dbschenker/patsch/dns"
"github.com/dbschenker/patsch/http"
"github.com/dbschenker/patsch/kube"
"github.com/dbschenker/patsch/util"
)
var (
quiet bool
insecure bool
fail bool
auto bool
once bool
interval int
urls []string
kubeconfig *string
)
func main() {
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Gets http status of URL(s) and prints an error, if the status is not okay\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTION]... URL [URL]...\n", os.Args[0])
flag.PrintDefaults()
}
flag.BoolVar(&quiet, "q", false, "quiet mode, does not print successful requests")
flag.BoolVar(&insecure, "k", false, "ignore ssl verification errors")
flag.BoolVar(&fail, "f", false, "fail mode, exit with an error, if any request fails")
flag.BoolVar(&auto, "a", false, "(EXPERIMENTAL!) auto mode, finds and checks all ingress rules in current kubernetes cluster")
flag.BoolVar(&once, "o", false, "single mode, only check once")
flag.IntVar(&interval, "n", 2, "interval <secs>")
kubeconfig = flag.String("kubeconfig", filepath.Join(util.HomeDir(), ".kube", "config"), "(optional) absolute path to the kubeconfig file")
flag.Parse()
if auto {
urls = kube.FindIngresses(*kubeconfig)
} else {
urls = flag.Args()
}
if len(urls) == 0 {
if auto {
fmt.Println("Could not find ingress routes in current cluster")
} else {
flag.Usage()
}
os.Exit(1)
}
start := time.Now()
ch := make(chan http.Result)
client := http.CreateHTTPClient(insecure)
ticker := time.NewTicker(time.Duration(interval) * time.Second)
for ; true; <-ticker.C {
for _, site := range urls {
go http.Fetch(client, site, ch) // start a goroutine
}
for range urls {
ret := <-ch
if ret.Error != nil {
failure(ret.Error)
} else {
success(ret.Message, ret.Site)
}
}
if once {
ticker.Stop()
break
}
}
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}
func failure(err error) {
_, _ = fmt.Println("❌ ", err)
if fail {
os.Exit(1)
}
}
func success(msg string, site string) {
if quiet {
return
}
addresses, err := dns.Lookup(site)
if err != nil {
failure(err)
return
}
fmt.Printf("✅ %s %s\n", msg, dns.Print(addresses, site))
}