diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:08:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:08:18 +0000 |
commit | 5da14042f70711ea5cf66e034699730335462f66 (patch) | |
tree | 0f6354ccac934ed87a2d555f45be4c831cf92f4a /src/go/collectors/go.d.plugin/modules/httpcheck/init.go | |
parent | Releasing debian version 1.44.3-2. (diff) | |
download | netdata-5da14042f70711ea5cf66e034699730335462f66.tar.xz netdata-5da14042f70711ea5cf66e034699730335462f66.zip |
Merging upstream version 1.45.3+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/httpcheck/init.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/httpcheck/init.go b/src/go/collectors/go.d.plugin/modules/httpcheck/init.go new file mode 100644 index 000000000..a7f708191 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/httpcheck/init.go @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package httpcheck + +import ( + "errors" + "fmt" + "net/http" + "regexp" + + "github.com/netdata/netdata/go/go.d.plugin/agent/module" + "github.com/netdata/netdata/go/go.d.plugin/pkg/matcher" + "github.com/netdata/netdata/go/go.d.plugin/pkg/web" +) + +type headerMatch struct { + exclude bool + key string + valMatcher matcher.Matcher +} + +func (hc *HTTPCheck) validateConfig() error { + if hc.URL == "" { + return errors.New("'url' not set") + } + return nil +} + +func (hc *HTTPCheck) initHTTPClient() (*http.Client, error) { + return web.NewHTTPClient(hc.Client) +} + +func (hc *HTTPCheck) initResponseMatchRegexp() (*regexp.Regexp, error) { + if hc.ResponseMatch == "" { + return nil, nil + } + return regexp.Compile(hc.ResponseMatch) +} + +func (hc *HTTPCheck) initHeaderMatch() ([]headerMatch, error) { + if len(hc.HeaderMatch) == 0 { + return nil, nil + } + + var hms []headerMatch + + for _, v := range hc.HeaderMatch { + if v.Key == "" { + continue + } + + hm := headerMatch{ + exclude: v.Exclude, + key: v.Key, + valMatcher: nil, + } + + if v.Value != "" { + m, err := matcher.Parse(v.Value) + if err != nil { + return nil, fmt.Errorf("parse key '%s value '%s': %v", v.Key, v.Value, err) + } + if v.Exclude { + m = matcher.Not(m) + } + hm.valMatcher = m + } + + hms = append(hms, hm) + } + + return hms, nil +} + +func (hc *HTTPCheck) initCharts() *module.Charts { + charts := httpCheckCharts.Copy() + + for _, chart := range *charts { + chart.Labels = []module.Label{ + {Key: "url", Value: hc.URL}, + } + } + + return charts +} |