forked from prometheus/blackbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
72 lines (64 loc) · 1.61 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"time"
)
func getEarliestCertExpiry(state *tls.ConnectionState) time.Time {
earliest := time.Time{}
for _, cert := range state.PeerCertificates {
if (earliest.IsZero() || cert.NotAfter.Before(earliest)) && !cert.NotAfter.IsZero() {
earliest = cert.NotAfter
}
}
return earliest
}
func probeHTTP(target string, w http.ResponseWriter, module Module) (success bool) {
var isSSL, redirects int
config := module.HTTP
client := &http.Client{
Timeout: module.Timeout,
}
client.CheckRedirect = func(_ *http.Request, via []*http.Request) error {
redirects = len(via)
if redirects > 10 || config.NoFollowRedirects {
return errors.New("Don't follow redirects")
} else {
return nil
}
}
resp, err := client.Get(target)
if err == nil {
defer resp.Body.Close()
if len(config.ValidStatusCodes) != 0 {
for _, code := range config.ValidStatusCodes {
if resp.StatusCode == code {
success = true
break
}
}
} else if 200 >= resp.StatusCode && resp.StatusCode < 300 {
success = true
}
}
if resp == nil {
resp = &http.Response{}
}
if resp.TLS != nil {
isSSL = 1
fmt.Fprintf(w, "probe_ssl_earliest_cert_expiry %f\n",
float64(getEarliestCertExpiry(resp.TLS).UnixNano())/1e9)
if config.FailIfSSL {
success = false
}
} else if config.FailIfNotSSL {
success = false
}
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_redirects %d\n", redirects)
fmt.Fprintf(w, "probe_http_ssl %d\n", isSSL)
return
}