-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
63 lines (55 loc) · 1.5 KB
/
metrics.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
package agent
import (
"errors"
"fmt"
"net/http"
"github.com/mistifyio/mistify-agent/rpc"
)
func getMetrics(w http.ResponseWriter, r *http.Request, mtype string) {
hr := &HTTPResponse{w}
ctx := getContext(r)
guest := getRequestGuest(r)
runner := getRequestRunner(r)
action, err := ctx.GetAction(fmt.Sprintf("%sMetrics", mtype))
if err != nil {
hr.JSONError(http.StatusNotFound, err)
return
}
// Metric requests are special in that they have Args that can vary by stage
// Create a unique request for each stage with the args
// TODO: Fix to use Pre/PostStage functions
response := &rpc.GuestMetricsResponse{}
pipeline := action.GeneratePipeline(nil, response, hr, nil)
hr.Header().Set("X-Guest-Job-ID", pipeline.ID)
for _, stage := range pipeline.Stages {
stage.Request = &rpc.GuestMetricsRequest{
Guest: guest,
Args: stage.Args,
Type: mtype,
}
}
err = runner.Process(pipeline)
if err != nil {
hr.JSONError(http.StatusInternalServerError, err)
return
}
switch mtype {
case "cpu":
hr.JSON(http.StatusOK, response.CPU)
case "nic":
hr.JSON(http.StatusOK, response.Nic)
case "disk":
hr.JSON(http.StatusOK, response.Disk)
default:
hr.JSONError(http.StatusInternalServerError, errors.New("Unknown metric"))
}
}
func getCPUMetrics(w http.ResponseWriter, r *http.Request) {
getMetrics(w, r, "cpu")
}
func getNicMetrics(w http.ResponseWriter, r *http.Request) {
getMetrics(w, r, "nic")
}
func getDiskMetrics(w http.ResponseWriter, r *http.Request) {
getMetrics(w, r, "disk")
}