forked from e-XpertSolutions/f5-rest-client
/
health_check.go
61 lines (57 loc) · 1.63 KB
/
health_check.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
package f5
import (
"fmt"
"strings"
)
// Cluster Management REST paths.
const (
PathDeviceInfo = "/mgmt/tm/cm/device"
)
// IsActive returns true whether the BigIP is active and the iControl REST are
// accessible. In case of error, false is returned.
func (c *Client) IsActive(host string) bool {
var respData struct {
FailoverState string `json:"failoverState"`
}
if err := c.ReadQuery(PathDeviceInfo+"/"+host, &respData); err != nil {
return false
}
return respData.FailoverState == "active"
}
// FailoverState returns the status of the BigIP (active, standby,
// forced-offline, ...).
func (c *Client) FailoverState(host, ip string) (string, error) {
var respData struct {
Items []struct {
FailoverState string `json:"failoverState"`
Name string `json:"name"`
Hostname string `json:"hostname"`
IP string `json:"managementIp"`
} `json:"items"`
}
if err := c.ReadQuery(PathDeviceInfo, &respData); err != nil {
return "unreachable", fmt.Errorf("cannot retrieve failover state: %v", err)
}
for _, item := range respData.Items {
if strings.HasPrefix(item.Name, host) || strings.HasPrefix(item.Hostname, host) {
if item.FailoverState == "" {
return "invalid", nil
}
return item.FailoverState, nil
}
}
if ip != "" {
if pos := strings.Index(ip, "/"); pos != -1 {
ip = ip[:pos]
}
for _, item := range respData.Items {
if strings.HasPrefix(item.IP, ip) {
if item.FailoverState == "" {
return "invalid", nil
}
return item.FailoverState, nil
}
}
}
return "invalid hostname", fmt.Errorf("hostname %q and management ip %q mismatch the ones retrieved", host, ip)
}