-
Notifications
You must be signed in to change notification settings - Fork 6
/
listHypervisors.go
178 lines (166 loc) · 4.57 KB
/
listHypervisors.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package hypervisors
import (
"bufio"
"fmt"
"net/http"
"sort"
"github.com/Cloud-Foundations/Dominator/lib/constants"
"github.com/Cloud-Foundations/Dominator/lib/html"
"github.com/Cloud-Foundations/Dominator/lib/json"
"github.com/Cloud-Foundations/Dominator/lib/url"
proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager"
)
const (
showOK = iota
showConnected
showAll
showOff
)
type hypervisorList []*hypervisorType
func (h *hypervisorType) getHealthStatus() string {
healthStatus := h.probeStatus.String()
if h.probeStatus == probeStatusConnected {
if h.healthStatus != "" {
healthStatus = h.healthStatus
}
}
return healthStatus
}
func (h *hypervisorType) getNumVMs() uint {
h.mutex.RLock()
defer h.mutex.RUnlock()
return uint(len(h.vms))
}
func (m *Manager) listHypervisors(topologyDir string, showFilter int,
subnetId string) (hypervisorList, error) {
m.mutex.RLock()
defer m.mutex.RUnlock()
machines, err := m.topology.ListMachines(topologyDir)
if err != nil {
return nil, err
}
hypervisors := make([]*hypervisorType, 0, len(machines))
for _, machine := range machines {
if subnetId != "" {
hasSubnet, _ := m.topology.CheckIfMachineHasSubnet(
machine.Hostname, subnetId)
if !hasSubnet {
continue
}
}
hypervisor := m.hypervisors[machine.Hostname]
switch showFilter {
case showOK:
if hypervisor.probeStatus == probeStatusConnected &&
(hypervisor.healthStatus == "" ||
hypervisor.healthStatus == "healthy") {
hypervisors = append(hypervisors, hypervisor)
}
case showConnected:
if hypervisor.probeStatus == probeStatusConnected {
hypervisors = append(hypervisors, hypervisor)
}
case showAll:
hypervisors = append(hypervisors, hypervisor)
case showOff:
if hypervisor.probeStatus == probeStatusOff {
hypervisors = append(hypervisors, hypervisor)
}
}
}
return hypervisors, nil
}
func (m *Manager) listHypervisorsHandler(w http.ResponseWriter,
req *http.Request) {
writer := bufio.NewWriter(w)
defer writer.Flush()
_, err := m.getTopology()
if err != nil {
fmt.Fprintln(writer, err)
return
}
parsedQuery := url.ParseQuery(req.URL)
showFilter := showAll
switch parsedQuery.Table["state"] {
case "connected":
showFilter = showConnected
case "OK":
showFilter = showOK
case "off":
showFilter = showOff
}
hypervisors, err := m.listHypervisors("", showFilter, "")
if err != nil {
fmt.Fprintln(writer, err)
return
}
sort.Sort(hypervisors)
if parsedQuery.OutputType() == url.OutputTypeText {
for _, hypervisor := range hypervisors {
fmt.Fprintln(writer, hypervisor.machine.Hostname)
}
return
}
if parsedQuery.OutputType() == url.OutputTypeJson {
json.WriteWithIndent(writer, " ", hypervisors)
return
}
fmt.Fprintf(writer, "<title>List of hypervisors</title>\n")
writer.WriteString(commonStyleSheet)
fmt.Fprintln(writer, "<body>")
fmt.Fprintln(writer, `<table border="1" style="width:100%">`)
tw, _ := html.NewTableWriter(writer, true,
"Name", "Status", "IP Addr", "Serial Number", "Location", "NumVMs")
for _, hypervisor := range hypervisors {
machine := hypervisor.machine
tw.WriteRow("", "",
fmt.Sprintf("<a href=\"showHypervisor?%s\">%s</a>",
machine.Hostname, machine.Hostname),
fmt.Sprintf("<a href=\"http://%s:%d/\">%s</a>",
machine.Hostname, constants.HypervisorPortNumber,
hypervisor.getHealthStatus()),
machine.HostIpAddress.String(),
hypervisor.serialNumber,
hypervisor.location,
fmt.Sprintf("<a href=\"http://%s:%d/listVMs\">%d</a>",
machine.Hostname, constants.HypervisorPortNumber,
hypervisor.getNumVMs()),
)
}
fmt.Fprintln(writer, "</table>")
fmt.Fprintln(writer, "</body>")
}
func (m *Manager) listHypervisorsInLocation(
request proto.ListHypervisorsInLocationRequest) ([]string, error) {
showFilter := showOK
if request.IncludeUnhealthy {
showFilter = showConnected
}
hypervisors, err := m.listHypervisors(request.Location, showFilter,
request.SubnetId)
if err != nil {
return nil, err
}
addresses := make([]string, 0, len(hypervisors))
for _, hypervisor := range hypervisors {
addresses = append(addresses,
fmt.Sprintf("%s:%d",
hypervisor.machine.Hostname, constants.HypervisorPortNumber))
}
return addresses, nil
}
func (list hypervisorList) Len() int {
return len(list)
}
func (list hypervisorList) Less(i, j int) bool {
if list[i].location < list[j].location {
return true
} else if list[i].location > list[j].location {
return false
} else {
return list[i].machine.Hostname < list[j].machine.Hostname
}
}
func (list hypervisorList) Swap(i, j int) {
list[i], list[j] = list[j], list[i]
}