forked from leoleovich/clusterhc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbitmq.go
70 lines (64 loc) · 1.44 KB
/
rabbitmq.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
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"time"
)
type ConfRabbitmq struct {
Enabled bool
Interval int
User string
Pass string
Host string
Port int
Nodes []string
}
type Rabbitmq struct {
Conf ConfRabbitmq
Status Status
lg log.Logger
}
func (rmq *Rabbitmq) check() {
for ; ; time.Sleep(time.Duration(rmq.Conf.Interval) * time.Second) {
client := &http.Client{Timeout: time.Duration(rmq.Conf.Interval/2+1) * time.Second}
url := "http://" + rmq.Conf.Host + ":" + strconv.Itoa(rmq.Conf.Port) + "/api/nodes"
req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth(rmq.Conf.User, rmq.Conf.Pass)
res, err := client.Do(req)
if err != nil {
rmq.lg.Println(err.Error())
rmq.Status.PartOfCluster = false
} else {
decoder := json.NewDecoder(res.Body)
type Node struct {
Name string
}
var n []Node
err = decoder.Decode(&n)
if err != nil {
rmq.lg.Println(err.Error())
rmq.Status.PartOfCluster = false
} else {
var found int
for _, node := range n {
for _, nodeToCheck := range rmq.Conf.Nodes {
if node.Name == "rabbit@"+nodeToCheck {
found++
break
}
}
}
if found == len(rmq.Conf.Nodes) {
rmq.Status.PartOfCluster = true
} else {
rmq.Status.PartOfCluster = false
rmq.lg.Println("Can not find all nodes in cluster")
}
}
res.Body.Close()
}
rmq.Status.Timestamp = time.Now()
}
}