forked from akwirick/go-health
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra-health.go
64 lines (50 loc) · 1.39 KB
/
cassandra-health.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/gocql/gocql"
"github.com/gorilla/mux"
)
var (
// Session is the global cassandra session
Session *gocql.Session
)
func fail(w http.ResponseWriter) {
http.Error(w, "failed", http.StatusInternalServerError)
}
// Health provides a basic endpoint for determining health
// fails if we can neither start a session or execute a query
func healthHandler(hostport string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cluster := gocql.NewCluster(hostport)
cluster.Keyspace = "system"
cluster.Consistency = gocql.One
session, sesErr := cluster.CreateSession()
if sesErr != nil {
fail(w)
return
}
defer session.Close()
// Execute test query
err := session.Query(`SELECT now() FROM system.local`).Exec()
if err != nil {
fail(w)
return
}
fmt.Fprintln(w, "success")
})
}
func main() {
host := flag.String("host", "127.0.0.1", "Cassandra Host")
port := flag.Uint("port", 9042, "CQL Port")
listen_port := flag.Uint("listen_port", 19042, "HTTP listen port")
flag.Parse()
hostport := fmt.Sprintf("%v:%v", *host, *port)
listenport := fmt.Sprintf(":%v", *listen_port)
r := mux.NewRouter()
r.Handle("/health", healthHandler(hostport))
fmt.Println("Starting health check on port", listenport, "press ctrl-c to exit")
log.Fatal(http.ListenAndServe(listenport, r))
}