-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (86 loc) · 2.29 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"crypto/tls"
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"time"
gircclient "github.com/goshuirc/irc-go/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
http.Handle("/probe", http.HandlerFunc(probeHandler))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func probeHandler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
w.WriteHeader(400)
w.Write([]byte("no target provided"))
return
}
tgt, err := url.Parse(target)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("invalid target; not valid uri"))
return
}
if tgt.Scheme != "ircs" && tgt.Scheme != "irc" {
w.WriteHeader(400)
w.Write([]byte("target must have ircs or irc scheme"))
return
}
// TODO: prometheus timeout header
registry := prometheus.NewRegistry()
up := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "irc_up",
Help: "target irc server is up",
})
registry.MustRegister(up)
reactor := gircclient.NewReactor()
defer reactor.Shutdown("probe done")
server := reactor.CreateServer("probe")
server.InitialNick = fmt.Sprintf("promirc_%s", rand.Int31())
server.InitialUser = "promirc"
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
err = server.Connect(
tgt.Host,
tgt.Scheme == "ircs",
tlsConfig,
)
if err != nil {
log.Printf("[ERROR] Could not connect to target: %v", err)
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r)
return
}
// TODO timeout; implement this ourselves
server.WaitForConnection()
up.Inc()
if tgt.Scheme == "ircs" {
state := server.RawConnection.(*tls.Conn).ConnectionState()
tlsExpiryGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "irc_ssl_expiry_epoch_seconds",
Help: "ssl expiry in unixtime, or zero for error",
})
registry.MustRegister(tlsExpiryGauge)
earliest := time.Time{}
if len(state.PeerCertificates) != 0 {
earliest = state.PeerCertificates[0].NotAfter
}
for _, cert := range state.PeerCertificates {
if cert.NotAfter.Before(earliest) {
earliest = cert.NotAfter
}
}
tlsExpiryGauge.Set(float64(earliest.Unix()))
}
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}