1package check
2
3import (
4 "fmt"
5 "io"
6 "net/http"
7 "regexp"
8 "time"
9)
10
11type Check struct {
12 Name string `toml:"name"`
13 Type string `toml:"type"`
14 URL string `toml:"url,omitempty"`
15 Expect string `toml:"expect"`
16 Interval int `toml:"interval"`
17}
18
19func CheckHTTP(c Check) (string, string, time.Duration) {
20 start := time.Now()
21 resp, err := http.Get(c.URL)
22 duration := time.Since(start)
23
24 if err != nil {
25 return "fail", err.Error(), duration
26 }
27 defer resp.Body.Close()
28
29 body, _ := io.ReadAll(resp.Body)
30 matched, _ := regexp.Match(c.Expect, body)
31
32 if resp.StatusCode == 200 && matched {
33 return "ok", "matched", duration
34 }
35
36 return "fail", fmt.Sprintf("Status: %d, matched: %v", resp.StatusCode, matched), duration
37}