From 768054fab86b9a217b0df543c1fc70b510b894f2 Mon Sep 17 00:00:00 2001 From: Ruben Cervilla <47088081+rbcervilla@users.noreply.github.com> Date: Mon, 18 Nov 2019 20:48:41 +0100 Subject: [PATCH] Implementation and doc --- .gitignore | 5 +++++ Dockerfile | 8 +++++++ README.md | 24 +++++++++++++++++++- go.mod | 5 +++++ go.sum | 16 ++++++++++++++ main.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15bdab2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa6b915 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.12.10 as builder +WORKDIR /build +COPY . . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o keydb-health + +FROM scratch +COPY --from=builder /build/keydb-health . +ENTRYPOINT ["/keydb-health"] \ No newline at end of file diff --git a/README.md b/README.md index ca0225e..3fbdeb9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ # keydb-health -Health check for KeyDB +HTTP health check. KeyDB instance is ready to server requests if: +- Is not loading from persistence +- Is not synchronizing with master. + +### Usage + +``` +Usage: + -h string + KeyDB host (default "127.0.0.1") + -p string + KeyDB port (default "6379") + -sp string + Server listener port (default "8080") +``` + +### Docker + +You can run it with Docker + +```sh +docker run --name keydb-health rbcervilla/keydb-health -h keydbhost -p 6379 +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..39e48ce --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/rbcervilla/keydb-health + +go 1.12 + +require github.com/go-redis/redis/v7 v7.0.0-beta.4 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cf07f8c --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-redis/redis/v7 v7.0.0-beta.4 h1:p6z7Pde69EGRWvlC++y8aFcaWegyrKHzOBGo0zUACTQ= +github.com/go-redis/redis/v7 v7.0.0-beta.4/go.mod h1:xhhSbUMTsleRPur+Vgx9sUHtyN33bdjxY+9/0n9Ig8s= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c2c28cb --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "flag" + "fmt" + "github.com/go-redis/redis/v7" + "net/http" + "strings" + "time" +) + +func main() { + + h := flag.String("h", "127.0.0.1", "KeyDB host") + p := flag.String("p", "6379", "KeyDB port") + sp := flag.String("sp", "8080", "Server listener port") + flag.Parse() + + client := redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%s", *h, *p), + DialTimeout: time.Second, + ReadTimeout: time.Second, + }) + + http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + + result, err := client.Info().Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + info := parseInfo(result) + if info["loading"] != "0" || info["master_sync_in_progress"] != "0" { + w.WriteHeader(http.StatusInternalServerError) + return + } + }) + + if err := http.ListenAndServe(":"+*sp, nil); err != nil { + panic(err) + } +} + +func parseInfo(str string) map[string]string { + + info := make(map[string]string) + lines := strings.Split(str, "\r\n") + + for _, line := range lines { + + if strings.HasPrefix(line, "#") { + continue + } + + pair := strings.Split(line, ":") + if len(pair) != 2 { + continue + } + + info[pair[0]] = pair[1] + } + + return info +}