Move probers to subdir, so more can be added
authorBrian Brazil <brian.brazil@robustperception.io>
Sat, 5 Sep 2015 14:51:52 +0000 (15:51 +0100)
committerBrian Brazil <brian.brazil@robustperception.io>
Sat, 5 Sep 2015 14:51:52 +0000 (15:51 +0100)
main.go
probers/http.go [new file with mode: 0644]
probers/probers.go [new file with mode: 0644]

diff --git a/main.go b/main.go
index 7257254df213877f97cd984f385e9c3647772d59..bd744ef876c6f6ede159c2c4e0d6ec16711a851f 100644 (file)
--- 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 (file)
index 0000000..aaa00b8
--- /dev/null
@@ -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 (file)
index 0000000..a4f710f
--- /dev/null
@@ -0,0 +1,7 @@
+package probers
+
+import (
+  "net/http"
+)
+
+var Probers = make(map[string]func(string, http.ResponseWriter)(bool))