From 78dc256570164e341793182c793e7433ac7fa924 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sat, 5 Sep 2015 15:51:52 +0100 Subject: [PATCH] Move probers to subdir, so more can be added --- main.go | 30 +++--------------------------- probers/http.go | 36 ++++++++++++++++++++++++++++++++++++ probers/probers.go | 7 +++++++ 3 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 probers/http.go create mode 100644 probers/probers.go diff --git a/main.go b/main.go index 7257254..bd744ef 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + + "github.com/brian-brazil/blackbox_exporter/probers" ) var addr = flag.String("web.listen-address", ":8080", "The address to listen on for HTTP requests.") @@ -21,32 +23,6 @@ type Module struct { Config interface{} `yaml:"config"` } -type HTTPProbe struct { - // Defaults to 2xx. - ValidStatusCodes []int `yaml:"valid_status_codes"` - FollowRedirects bool `yaml:"follow_redirects"` - FailIfSSL bool `yaml:"fail_if_ssl"` - FailIfNotSSL bool `yaml:"fail_if_not_ssl"` -} - -func probeHTTP(target string, w http.ResponseWriter) (success bool) { - var isSSL int - resp, err := http.Get(target) - if err == nil { - if 200 >= resp.StatusCode && resp.StatusCode < 300 { - success = true - } - defer resp.Body.Close() - } - if resp.TLS != nil { - isSSL = 1 - } - fmt.Fprintf(w, "probe_http_status_code %d\n", resp.StatusCode) - fmt.Fprintf(w, "probe_http_content_length %d\n", resp.ContentLength) - fmt.Fprintf(w, "probe_http_ssl %d\n", isSSL) - return -} - func probeHandler(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() target := params.Get("target") @@ -59,7 +35,7 @@ func probeHandler(w http.ResponseWriter, r *http.Request) { module = "http2xx" } start := time.Now() - success := probeHTTP(target, w) + success := probers.Probers["http"](target, w) fmt.Fprintf(w, "probe_duration_seconds %f\n", float64(time.Now().Sub(start))/1e9) if success { fmt.Fprintf(w, "probe_success %d\n", 1) diff --git a/probers/http.go b/probers/http.go new file mode 100644 index 0000000..aaa00b8 --- /dev/null +++ b/probers/http.go @@ -0,0 +1,36 @@ +package probers + +import ( + "net/http" + "fmt" +) + +func init() { + Probers["http"] = probeHTTP +} + +type HTTPProbe struct { + // Defaults to 2xx. + ValidStatusCodes []int `yaml:"valid_status_codes"` + FollowRedirects bool `yaml:"follow_redirects"` + FailIfSSL bool `yaml:"fail_if_ssl"` + FailIfNotSSL bool `yaml:"fail_if_not_ssl"` +} + +func probeHTTP(target string, w http.ResponseWriter) (success bool) { + var isSSL int + resp, err := http.Get(target) + if err == nil { + if 200 >= resp.StatusCode && resp.StatusCode < 300 { + success = true + } + defer resp.Body.Close() + } + if resp.TLS != nil { + isSSL = 1 + } + fmt.Fprintf(w, "probe_http_status_code %d\n", resp.StatusCode) + fmt.Fprintf(w, "probe_http_content_length %d\n", resp.ContentLength) + fmt.Fprintf(w, "probe_http_ssl %d\n", isSSL) + return +} diff --git a/probers/probers.go b/probers/probers.go new file mode 100644 index 0000000..a4f710f --- /dev/null +++ b/probers/probers.go @@ -0,0 +1,7 @@ +package probers + +import ( + "net/http" +) + +var Probers = make(map[string]func(string, http.ResponseWriter)(bool)) -- 2.25.1