-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
151 lines (127 loc) · 3.66 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
websocketSuccessful = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "websocket_successful",
Help: "( 0 = false , 1 = true )",
})
websocketResponseTime = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "websocket_response_time",
Help: "( Time until we get EOSE in ms; 0 for failed )",
})
websocketStatusCode = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "websocket_status_code",
Help: "( 101 is normal status code for ws )",
})
respCode float64
)
func probeHandler(w http.ResponseWriter, r *http.Request) {
messages, msOk := r.URL.Query()["message"]
if !msOk || len(messages) != 1 {
http.Error(w, "Specify one 'message' parameter", http.StatusBadRequest)
return
}
sendMessage := []byte(messages[0])
contains, coOk := r.URL.Query()["contains"]
if !coOk || len(contains) != 1 {
http.Error(w, "Specify one 'contains' parameter", http.StatusBadRequest)
return
}
contain := contains[0]
targets, trOk := r.URL.Query()["target"]
if !trOk || len(targets) != 1 {
http.Error(w, "Specify one 'target' parameter", http.StatusBadRequest)
return
}
target := targets[0]
ur, _ := url.Parse(target)
u := url.URL{Scheme: ur.Scheme, Host: ur.Host, Path: ur.Path, RawQuery: ur.RawQuery}
fmt.Printf("Probing %s with message %s and checkign if response contains %s\n", target, sendMessage, contain)
headers := http.Header{}
headers.Add("User-Agent", "websocket-exporter/0.1.0")
c, resp, errCon := websocket.DefaultDialer.Dial(u.String(), headers)
start := time.Now()
timeout := 5 * time.Second
c.SetReadDeadline(start.Add(timeout))
c.SetWriteDeadline(start.Add(timeout))
if resp != nil {
respCode = float64(resp.StatusCode)
} else {
respCode = 0
}
if (errCon != nil) || (float64(resp.StatusCode) != 101) {
websocketSuccessful.Set(0)
websocketStatusCode.Set(respCode)
websocketResponseTime.Set(0)
} else {
// send ws message
err := c.WriteMessage(websocket.TextMessage, sendMessage)
if err != nil {
websocketSuccessful.Set(0)
websocketStatusCode.Set(respCode)
websocketResponseTime.Set(0)
}
OuterLoop:
for {
select {
case <-time.After(timeout):
websocketSuccessful.Set(0)
websocketStatusCode.Set(respCode)
websocketResponseTime.Set(0)
break OuterLoop
default:
_, message, err := c.ReadMessage()
if err != nil {
websocketSuccessful.Set(0)
websocketStatusCode.Set(respCode)
websocketResponseTime.Set(0)
break OuterLoop
}
if strings.Contains(string(message), contain) {
websocketSuccessful.Set(1)
websocketStatusCode.Set(respCode)
websocketResponseTime.Set(float64(time.Since(start).Milliseconds()))
break OuterLoop
} else {
websocketSuccessful.Set(0)
websocketStatusCode.Set(respCode)
websocketResponseTime.Set(0)
}
}
}
c.Close()
}
reg := prometheus.NewRegistry()
reg.MustRegister(websocketSuccessful)
reg.MustRegister(websocketStatusCode)
reg.MustRegister(websocketResponseTime)
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func main() {
port := flag.Int("port", 9143, "Port Number to listen")
host := flag.String("host", "127.0.0.1", "Host to listen")
flag.Parse()
addr := fmt.Sprint(*host, ":", *port)
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r)
})
fmt.Println("Starting exporter on addr: ", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
fmt.Println("Error staring server: ", err)
}
}