/
opSystem.go
155 lines (139 loc) · 3.7 KB
/
opSystem.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
package freepsgraph
import (
"context"
"net/http"
"strings"
"github.com/hannesrauhe/freeps/base"
"github.com/hannesrauhe/freeps/utils"
)
type OpSystem struct {
ge *GraphEngine
cancel context.CancelFunc
}
var _ base.FreepsBaseOperator = &OpSystem{}
func NewSytemOp(ge *GraphEngine, cancel context.CancelFunc) *OpSystem {
return &OpSystem{ge: ge, cancel: cancel}
}
// GetName returns the name of the operator
func (o *OpSystem) GetName() string {
return "system"
}
func (o *OpSystem) Execute(ctx *base.Context, fn string, args map[string]string, input *base.OperatorIO) *base.OperatorIO {
switch fn {
case "stop", "shutdown":
o.ge.reloadRequested = false
o.cancel()
return base.MakeEmptyOutput()
case "reload":
o.ge.reloadRequested = true
o.cancel()
return base.MakeEmptyOutput()
case "getGraph", "getGraphDesc", "GetGraphDesc":
return o.ge.ExecuteOperatorByName(ctx, "graphbuilder", "getGraph", map[string]string{"graphName": args["name"]}, base.MakeEmptyOutput())
case "deleteGraph":
return o.ge.ExecuteOperatorByName(ctx, "graphbuilder", "deleteGraph", map[string]string{"graphName": args["name"]}, base.MakeEmptyOutput())
case "toDot":
g, out := o.ge.prepareGraphExecution(ctx, args["name"])
if out.IsError() {
return out
}
return base.MakeByteOutput(g.ToDot(ctx))
case "GetGraphDescByTag":
tags := []string{}
if _, ok := args["tags"]; ok {
tags = strings.Split(args["tags"], ",")
}
if args["tag"] != "" {
tags = append(tags, args["tag"])
}
gim := o.ge.GetGraphDescByTag(tags)
if gim == nil || len(gim) == 0 {
return base.MakeOutputError(http.StatusNotFound, "No graphs with tags %v", strings.Join(tags, ","))
}
return base.MakeObjectOutput(gim)
case "stats":
return o.Stats(ctx, fn, args, input)
case "noop":
return base.MakeEmptyOutput()
case "version":
return base.MakePlainOutput(utils.BuildFullVersion())
}
return base.MakeOutputError(http.StatusBadRequest, "Unknown function: "+fn)
}
func (o *OpSystem) GetFunctions() []string {
return []string{"shutdown", "reload", "stats", "getGraphDesc", "getGraphInfo", "getGraphDescByTag", "getCollectedErrors", "toDot", "contextToDot", "deleteGraph", "version"}
}
func (o *OpSystem) GetPossibleArgs(fn string) []string {
switch fn {
case "stats":
return []string{"statType"}
case "getGraphDesc":
return []string{"name"}
case "GetGraphDesc":
return []string{"name"}
case "GetGraphDescByTag":
return []string{"tags", "tag"}
case "getCollectedErrors":
return []string{"duration"}
case "toDot":
return []string{"name"}
case "contextToDot":
return []string{}
case "deleteGraph":
return []string{"name"}
}
return []string{"name"}
}
func (o *OpSystem) GetArgSuggestions(fn string, arg string, otherArgs map[string]string) map[string]string {
if arg == "name" {
agd := o.ge.GetAllGraphDesc()
graphs := map[string]string{}
for n := range agd {
graphs[n] = n
}
return graphs
}
switch fn {
case "stats":
switch arg {
case "statType":
return map[string]string{
"cpu": "cpu",
"disk": "disk",
"loadavg": "loadavg",
"memory": "memory",
"network": "network",
"uptime": "uptime",
}
}
case "getGraphDesc":
fallthrough
case "GetGraphDesc":
switch arg {
case "name":
agd := o.ge.GetAllGraphDesc()
graphs := make(map[string]string)
for n := range agd {
graphs[n] = n
}
return graphs
}
case "GetGraphDescByTag":
switch arg {
case "tag":
tags := o.ge.GetTags()
return tags
}
}
return map[string]string{}
}
// StartListening (noOp)
func (o *OpSystem) StartListening(ctx *base.Context) {
}
// Shutdown (noOp)
func (o *OpSystem) Shutdown(ctx *base.Context) {
}
// GetHook (noOp)
func (o *OpSystem) GetHook() interface{} {
return nil
}