-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmonitor.go
88 lines (73 loc) · 2.1 KB
/
monitor.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
87
88
//author SkyRocknRoll
package main
import (
"flag"
"fmt"
"github.com/gorilla/mux"
"gopkg.in/redis.v3"
"io"
"log"
"net/http"
"strings"
)
func main() {
ListenEndpoint := flag.String("listen-enpoint", "0.0.0.0:9736", "Enpoint to listen ex: 0.0.0.0:9736")
r := mux.NewRouter()
r.HandleFunc("/redis/{endpoint}", RedisStatus)
r.HandleFunc("/sentinel/{endpoint}/{master}", RedisSentinelStatus)
fmt.Printf("Server listening on %s", *ListenEndpoint)
err := http.ListenAndServe(*ListenEndpoint, r)
if err != nil {
fmt.Println(err.Error())
log.Fatal("Unable to start the server!!!")
}
}
// Creates New Redis Client
func GetNewClient(Endpoint string) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: Endpoint,
Password: "", // no password set
DB: 0, // use default DB
})
return client
}
//Provides new redis client using sentinel
func GetNewFailoverClient(MasterName string, SentinelAddrs []string) *redis.Client {
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: MasterName,
SentinelAddrs: SentinelAddrs,
Password: "", // no password set
DB: 0, // use default DB
})
return client
}
func RedisStatus(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
client := GetNewClient(vars["endpoint"])
defer client.Close() // Close the client.
data, err := client.Set("__||__", "Are You Up?", -1).Result()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
fmt.Println(err.Error())
} else {
w.WriteHeader(http.StatusOK)
io.WriteString(w, data)
}
}
func RedisSentinelStatus(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
SentinelAddrs := strings.Split(vars["endpoint"], ",")
MasterName := vars["master"]
client := GetNewFailoverClient(MasterName, SentinelAddrs)
defer client.Close() // Close the client.
data, err := client.Set("__||__", "Are You Up?", -1).Result()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
//fmt.Println(err.Error())
} else {
w.WriteHeader(http.StatusOK)
io.WriteString(w, data)
}
}