|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "github.com/gorilla/websocket" |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + websocketSuccessful = prometheus.NewGauge( |
| 17 | + prometheus.GaugeOpts{ |
| 18 | + Name: "websocket_successful", |
| 19 | + Help: "( 0 = false , 1 = true )", |
| 20 | + }) |
| 21 | + websocketResponseTime = prometheus.NewGauge( |
| 22 | + prometheus.GaugeOpts{ |
| 23 | + Name: "websocket_response_time", |
| 24 | + Help: "( Time until we get EOSE in ms; 0 for failed )", |
| 25 | + }) |
| 26 | + websocketStatusCode = prometheus.NewGauge( |
| 27 | + prometheus.GaugeOpts{ |
| 28 | + Name: "websocket_status_code", |
| 29 | + Help: "( 101 is normal status code for ws )", |
| 30 | + }) |
| 31 | + |
| 32 | + respCode float64 |
| 33 | +) |
| 34 | + |
| 35 | +func probeHandler(w http.ResponseWriter, r *http.Request) { |
| 36 | + |
| 37 | + messages, msOk := r.URL.Query()["message"] |
| 38 | + if !msOk || len(messages) != 1 { |
| 39 | + http.Error(w, "Specify one 'message' parameter", http.StatusBadRequest) |
| 40 | + return |
| 41 | + } |
| 42 | + sendMessage := []byte(messages[0]) |
| 43 | + |
| 44 | + contains, coOk := r.URL.Query()["contains"] |
| 45 | + if !coOk || len(contains) != 1 { |
| 46 | + http.Error(w, "Specify one 'contains' parameter", http.StatusBadRequest) |
| 47 | + return |
| 48 | + } |
| 49 | + contain := contains[0] |
| 50 | + |
| 51 | + targets, trOk := r.URL.Query()["target"] |
| 52 | + if !trOk || len(targets) != 1 { |
| 53 | + http.Error(w, "Specify one 'target' parameter", http.StatusBadRequest) |
| 54 | + return |
| 55 | + } |
| 56 | + target := targets[0] |
| 57 | + |
| 58 | + ur, _ := url.Parse(target) |
| 59 | + |
| 60 | + u := url.URL{Scheme: ur.Scheme, Host: ur.Host, Path: ur.Path, RawQuery: ur.RawQuery} |
| 61 | + |
| 62 | + fmt.Printf("Probing %s with message %s and checkign if response contains %s\n", target, sendMessage, contain) |
| 63 | + |
| 64 | + c, resp, errCon := websocket.DefaultDialer.Dial(u.String(), nil) |
| 65 | + |
| 66 | + start := time.Now() |
| 67 | + timeout := 5 * time.Second |
| 68 | + c.SetReadDeadline(start.Add(timeout)) |
| 69 | + c.SetWriteDeadline(start.Add(timeout)) |
| 70 | + |
| 71 | + if resp != nil { |
| 72 | + respCode = float64(resp.StatusCode) |
| 73 | + } else { |
| 74 | + respCode = 0 |
| 75 | + } |
| 76 | + |
| 77 | + if (errCon != nil) || (float64(resp.StatusCode) != 101) { |
| 78 | + websocketSuccessful.Set(0) |
| 79 | + websocketStatusCode.Set(respCode) |
| 80 | + websocketResponseTime.Set(0) |
| 81 | + } else { |
| 82 | + // send ws message |
| 83 | + err := c.WriteMessage(websocket.TextMessage, sendMessage) |
| 84 | + if err != nil { |
| 85 | + websocketSuccessful.Set(0) |
| 86 | + websocketStatusCode.Set(respCode) |
| 87 | + websocketResponseTime.Set(0) |
| 88 | + } |
| 89 | + OuterLoop: |
| 90 | + for { |
| 91 | + select { |
| 92 | + case <-time.After(timeout): |
| 93 | + websocketSuccessful.Set(0) |
| 94 | + websocketStatusCode.Set(respCode) |
| 95 | + websocketResponseTime.Set(0) |
| 96 | + break OuterLoop |
| 97 | + default: |
| 98 | + _, message, err := c.ReadMessage() |
| 99 | + if err != nil { |
| 100 | + websocketSuccessful.Set(0) |
| 101 | + websocketStatusCode.Set(respCode) |
| 102 | + websocketResponseTime.Set(0) |
| 103 | + break OuterLoop |
| 104 | + } |
| 105 | + if strings.Contains(string(message), contain) { |
| 106 | + websocketSuccessful.Set(1) |
| 107 | + websocketStatusCode.Set(respCode) |
| 108 | + websocketResponseTime.Set(float64(time.Since(start).Milliseconds())) |
| 109 | + break OuterLoop |
| 110 | + } else { |
| 111 | + websocketSuccessful.Set(0) |
| 112 | + websocketStatusCode.Set(respCode) |
| 113 | + websocketResponseTime.Set(0) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + c.Close() |
| 118 | + } |
| 119 | + |
| 120 | + reg := prometheus.NewRegistry() |
| 121 | + reg.MustRegister(websocketSuccessful) |
| 122 | + reg.MustRegister(websocketStatusCode) |
| 123 | + reg.MustRegister(websocketResponseTime) |
| 124 | + |
| 125 | + h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) |
| 126 | + h.ServeHTTP(w, r) |
| 127 | +} |
| 128 | + |
| 129 | +func main() { |
| 130 | + |
| 131 | + port := flag.Int("port", 9143, "Port Number to listen") |
| 132 | + host := flag.String("host", "127.0.0.1", "Host to listen") |
| 133 | + |
| 134 | + flag.Parse() |
| 135 | + |
| 136 | + addr := fmt.Sprint(*host, ":", *port) |
| 137 | + http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) { |
| 138 | + probeHandler(w, r) |
| 139 | + }) |
| 140 | + |
| 141 | + fmt.Println("Starting exporter on addr: ", addr) |
| 142 | + err := http.ListenAndServe(addr, nil) |
| 143 | + if err != nil { |
| 144 | + fmt.Println("Error staring server: ", err) |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments