Skip to content

Commit

Permalink
Implementation and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
rbcervilla authored and rcfisic committed Nov 18, 2019
1 parent e6da2d2 commit 768054f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
65 changes: 65 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 768054f

Please sign in to comment.