-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
86 lines (68 loc) · 1.78 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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/go-redis/redis"
)
func main() {
// create client
client := redisClient()
// check if its alive
if err := client.Ping().Err(); err != nil {
critical("Could not ping redis: %s", err)
}
// get the client info
raw, err := client.Info().Result()
if err != nil {
critical("Could not get client info: %s", err)
}
// parse out
info := parseKeyValue(raw)
// check if redis is loading data from disk
if loading, ok := info["loading"]; ok && loading == "1" {
warning("Redis is currently loading data from disk")
}
// check if redis is syncing data from master
if masterSyncInProgress, ok := info["master_sync_in_progress"]; ok && masterSyncInProgress == "1" {
warning("Redis slave is currently syncing data from its master instance")
}
// check if the master link is up
if masterLinkUpString, ok := info["master_link_status"]; ok && masterLinkUpString != "up" {
warning("Redis slave do not have active connection to its master instance")
}
fmt.Printf("OK!")
}
func redisClient() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_ADDR"),
DialTimeout: 1 * time.Second,
Password: os.Getenv("REDIS_PASS"),
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
})
}
func critical(str string, args ...interface{}) {
fmt.Printf(str, args...)
os.Exit(2)
}
func warning(str string, args ...interface{}) {
fmt.Printf(str, args...)
os.Exit(1)
}
func parseKeyValue(str string) map[string]string {
res := 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
}
res[pair[0]] = pair[1]
}
return res
}