-
Notifications
You must be signed in to change notification settings - Fork 6
/
handler_dt.go
168 lines (149 loc) · 5.7 KB
/
handler_dt.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
package server
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/gbolo/vsummary/common"
)
func handlerDtVirtualMachine(w http.ResponseWriter, req *http.Request) {
dtResponse, err := getDatatablesResponse(req, "view_vm")
if err != nil {
fmt.Fprintf(w, err.Error())
log.Errorf("error parsing datatables request: %v", err)
}
// loop through data and make modifications
data := dtResponse.Data[:0]
for _, row := range dtResponse.Data {
row["status"] = decorateCell(row["status"])
row["esxi_status"] = decorateCell(row["esxi_status"])
row["power_state"] = decorateCell(row["power_state"])
row["guest_tools_running"] = decorateCell(row["guest_tools_running"])
row["memory_bytes"] = common.BytesHumanReadable(row["memory_bytes"])
row["memory_mb"] = common.MegaBytesHumanReadable(row["memory_mb"])
row["stat_guest_memory_usage"] = common.MegaBytesHumanReadable(row["stat_guest_memory_usage"])
row["stat_host_memory_usage"] = common.MegaBytesHumanReadable(row["stat_host_memory_usage"])
row["stat_cpu_usage"] = row["stat_cpu_usage"] + " MHz"
row["stat_uptime_sec"] = common.SecondsToHuman(row["stat_uptime_sec"])
row["folder"] = common.SetDefaultValue(row["folder"], "None")
row["cluster"] = common.SetDefaultValue(row["cluster"], "None")
row["guest_os"] = common.SetDefaultValue(row["guest_os"], "unknown")
row["guest_hostname"] = common.SetDefaultValue(row["guest_hostname"], "unknown")
row["guest_ip"] = common.SetDefaultValue(row["guest_ip"], "unknown")
row["config_change_version"] = common.SetDefaultValue(row["config_change_version"], "unknown")
data = append(data, row)
}
// write a response
dtResponse.Data = data
b, _ := json.MarshalIndent(dtResponse, "", " ")
fmt.Fprintf(w, string(b))
}
func handlerDtEsxi(w http.ResponseWriter, req *http.Request) {
dtResponse, err := getDatatablesResponse(req, "view_esxi")
if err != nil {
fmt.Fprintf(w, err.Error())
log.Errorf("error parsing datatables request: %v", err)
}
// loop through data and make modifications
data := dtResponse.Data[:0]
for _, row := range dtResponse.Data {
row["memory_bytes"] = common.BytesHumanReadable(row["memory_bytes"])
row["vmemory_mb_powered_on"] = common.MegaBytesHumanReadable(row["vmemory_mb_powered_on"])
row["stat_memory_usage"] = common.MegaBytesHumanReadable(row["stat_memory_usage"])
row["stat_cpu_usage"] = fmt.Sprintf("%s MHz", row["stat_cpu_usage"])
row["status"] = decorateCell(row["status"])
row["in_maintenance_mode"] = decorateCell(row["in_maintenance_mode"])
row["power_state"] = decorateCell(row["power_state"])
row["stat_uptime_sec"] = common.SecondsToHuman(row["stat_uptime_sec"])
row["max_evc"] = common.SetDefaultValue(row["max_evc"], "unknown")
data = append(data, row)
}
// write a response
dtResponse.Data = data
b, _ := json.MarshalIndent(dtResponse, "", " ")
fmt.Fprintf(w, string(b))
}
func handlerDtDatastore(w http.ResponseWriter, req *http.Request) {
dtResponse, err := getDatatablesResponse(req, "view_datastore")
if err != nil {
fmt.Fprintf(w, err.Error())
log.Errorf("error parsing datatables request: %v", err)
}
// loop through data and make modifications
data := dtResponse.Data[:0]
for _, row := range dtResponse.Data {
row["capacity_bytes"] = common.BytesHumanReadable(row["capacity_bytes"])
row["free_bytes"] = common.BytesHumanReadable(row["free_bytes"])
row["status"] = decorateCell(row["status"])
data = append(data, row)
}
// write a response
dtResponse.Data = data
b, _ := json.MarshalIndent(dtResponse, "", " ")
fmt.Fprintf(w, string(b))
}
// generic handler that will write the http response itself
func handlerDatatables(w http.ResponseWriter, req *http.Request, dbTable string) {
// log time on debug
defer common.ExecutionTime(time.Now(), "dt api "+dbTable)
// ensure the request is a proper datatables request
di, err := ParseDatatablesRequest(req)
if err != nil {
fmt.Fprintf(w, err.Error())
log.Errorf("error parsing datatables request: %v", err)
return
}
// get the data for the request
di.SetDbX(backend.GetDB())
response, err := di.fetchDataForResponse(dbTable)
if err != nil {
log.Errorf("error getting datatables response: %v", err)
return
}
// write a response
b, _ := json.MarshalIndent(response, "", " ")
fmt.Fprintf(w, string(b))
return
}
// retrieves the DataTablesResponse based on the request body
func getDatatablesResponse(req *http.Request, dbTable string) (dtResponse DataTablesResponse, err error) {
// log time on debug
defer common.ExecutionTime(time.Now(), "dt api "+dbTable)
// ensure the request is a proper datatables request
di, err := ParseDatatablesRequest(req)
if err != nil {
return
}
// get the data for the request
di.SetDbX(backend.GetDB())
dtResponse, err = di.fetchDataForResponse(dbTable)
if err != nil {
log.Errorf("error getting datatables response: %v", err)
return
}
return
}
// this will add some html styling to string
func decorateCell(value string) string {
switch strings.ToLower(value) {
case "green":
return "<span class=\"label label-pill label-success\">green</span>"
case "yellow":
return "<span class=\"label label-pill label-warning\">yellow</span>"
case "red":
return "<span class=\"label label-pill label-danger\">red</span>"
case "poweredon":
return "<span class=\"label label-pill label-success\">poweredOn</span>"
case "poweredoff":
return "<span class=\"label label-pill label-danger\">poweredOff</span>"
case "yes", "true", "guesttoolsrunning":
return "<span class=\"label label-pill label-success\">Yes</span>"
case "no", "false", "guesttoolsnotrunning":
return "<span class=\"label label-pill label-danger\">No</span>"
case "ok":
return "<span class=\"label label-pill label-success\">OK</span>"
default:
return value
}
}