github.com/netdata/go.d.plugin@v0.58.1/modules/httpcheck/init.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package httpcheck 4 5 import ( 6 "errors" 7 "fmt" 8 "net/http" 9 "regexp" 10 11 "github.com/netdata/go.d.plugin/agent/module" 12 "github.com/netdata/go.d.plugin/pkg/matcher" 13 "github.com/netdata/go.d.plugin/pkg/web" 14 ) 15 16 type headerMatch struct { 17 exclude bool 18 key string 19 valMatcher matcher.Matcher 20 } 21 22 func (hc *HTTPCheck) validateConfig() error { 23 if hc.URL == "" { 24 return errors.New("'url' not set") 25 } 26 return nil 27 } 28 29 func (hc *HTTPCheck) initHTTPClient() (*http.Client, error) { 30 return web.NewHTTPClient(hc.Client) 31 } 32 33 func (hc *HTTPCheck) initResponseMatchRegexp() (*regexp.Regexp, error) { 34 if hc.ResponseMatch == "" { 35 return nil, nil 36 } 37 return regexp.Compile(hc.ResponseMatch) 38 } 39 40 func (hc *HTTPCheck) initHeaderMatch() ([]headerMatch, error) { 41 if len(hc.HeaderMatch) == 0 { 42 return nil, nil 43 } 44 45 var hms []headerMatch 46 47 for _, v := range hc.HeaderMatch { 48 if v.Key == "" { 49 continue 50 } 51 52 hm := headerMatch{ 53 exclude: v.Exclude, 54 key: v.Key, 55 valMatcher: nil, 56 } 57 58 if v.Value != "" { 59 m, err := matcher.Parse(v.Value) 60 if err != nil { 61 return nil, fmt.Errorf("parse key '%s value '%s': %v", v.Key, v.Value, err) 62 } 63 if v.Exclude { 64 m = matcher.Not(m) 65 } 66 hm.valMatcher = m 67 } 68 69 hms = append(hms, hm) 70 } 71 72 return hms, nil 73 } 74 75 func (hc *HTTPCheck) initCharts() *module.Charts { 76 charts := httpCheckCharts.Copy() 77 78 for _, chart := range *charts { 79 chart.Labels = []module.Label{ 80 {Key: "url", Value: hc.URL}, 81 } 82 } 83 84 return charts 85 }