diff options
| -rw-r--r-- | cmd/main/http.go | 16 | ||||
| -rw-r--r-- | cmd/main/main.go | 10 | ||||
| -rw-r--r-- | http.go | 22 |
3 files changed, 35 insertions, 13 deletions
diff --git a/cmd/main/http.go b/cmd/main/http.go index 8eb4990..ef6f2a7 100644 --- a/cmd/main/http.go +++ b/cmd/main/http.go @@ -4,8 +4,6 @@ import ( "bytes" "fmt" "time" - - "riedstra.dev/go/checkup" ) type httpCheck struct { @@ -13,9 +11,11 @@ type httpCheck struct { Code int } -func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *jobResponse, timeout int) { +type httpCheckFunc func(string, int, int) error + +func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *jobResponse, timeout int, HF httpCheckFunc) { for hc := range jobs { - err := checkup.HttpStatusOK(hc.URL, timeout, hc.Code) + err := HF(hc.URL, timeout, hc.Code) if err != nil { s := fmt.Sprintf("Checking: %s, %v", hc.URL, err) @@ -31,18 +31,18 @@ func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *jobResponse, timeout in } } -func checkStatus(conf *Config) map[string]*jobResponse { +func checkStatus(conf *Config, data map[string]*int, HF httpCheckFunc) map[string]*jobResponse { buf := &bytes.Buffer{} hc := make(chan httpCheck) msgs := make(chan *jobResponse) for i := 1; i <= conf.Workers; i++ { - go httpStatusWorker(hc, msgs, conf.HTTPTimeout) + go httpStatusWorker(hc, msgs, conf.HTTPTimeout, HF) } go func() { - for url, code := range conf.StatusChecks { + for url, code := range data { if code == nil { code = &conf.ExpectedStatusCode } @@ -52,7 +52,7 @@ func checkStatus(conf *Config) map[string]*jobResponse { }() out := make(map[string]*jobResponse) - for i := 0; i < len(conf.StatusChecks); i++ { + for i := 0; i < len(data); i++ { resp := <-msgs if resp != nil { buf.Write([]byte(resp.Message)) diff --git a/cmd/main/main.go b/cmd/main/main.go index dd344d2..9210d44 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -30,6 +30,7 @@ type Config struct { CheckCerts map[string]*string `yaml:"CheckCerts"` ExpectedStatusCode int `yaml:"ExpectedStatusCode"` StatusChecks map[string]*int `yaml:"StatusChecks"` + StatusChecksNoRdr map[string]*int `yaml:"StatusChecksNoRedirect"` Workers int `yaml:"Workers"` Interval int `yaml:"Interval"` RenotifyInterval int `yaml:"RenotifyInterval"` @@ -172,21 +173,26 @@ func main() { if *once { jobNotify(conf, checkCerts(conf)) - jobNotify(conf, checkStatus(conf)) + jobNotify(conf, checkStatus(conf, conf.StatusChecks, checkup.HttpStatusOK)) + jobNotify(conf, checkStatus(conf, conf.StatusChecksNoRdr, checkup.HttpNoRedirectStatusOK)) os.Exit(0) } certsPrev := map[string]*jobResponse{} statusPrev := map[string]*jobResponse{} + statusPrevNoRdr := map[string]*jobResponse{} for { cert := checkCerts(conf) - status := checkStatus(conf) + status := checkStatus(conf, conf.StatusChecks, checkup.HttpStatusOK) + statusNoRdr := checkStatus(conf, conf.StatusChecksNoRdr, checkup.HttpNoRedirectStatusOK) jobNotifyDedup(conf, certsPrev, cert) jobNotifyDedup(conf, statusPrev, status) + jobNotifyDedup(conf, statusPrevNoRdr, statusNoRdr) certsPrev = cert statusPrev = status + statusPrevNoRdr = statusNoRdr time.Sleep(time.Duration(conf.Interval) * time.Second) } @@ -1,14 +1,13 @@ package checkup import ( + "errors" "fmt" "net/http" "time" ) -func HttpStatusOK(url string, timeout, status int) error { - client := &http.Client{Timeout: time.Duration(timeout) * time.Second} - +func httpReq(client *http.Client, url string, timeout, status int) error { resp, err := client.Get(url) if err != nil { @@ -24,3 +23,20 @@ func HttpStatusOK(url string, timeout, status int) error { return nil } + +func HttpStatusOK(url string, timeout, status int) error { + client := &http.Client{Timeout: time.Duration(timeout) * time.Second} + return httpReq(client, url, timeout, status) +} + +func HttpNoRedirectStatusOK(url string, timeout, status int) error { + client := &http.Client{ + Timeout: time.Duration(timeout) * time.Second, + CheckRedirect: httpNoRedirectfunc, + } + return httpReq(client, url, timeout, status) +} + +func httpNoRedirectfunc(req *http.Request, via []*http.Request) error { + return errors.New("Redirected") +} |
