forked from open-falcon/falcon-plus
/
grafana_controller.go
272 lines (258 loc) · 8.17 KB
/
grafana_controller.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package graph
import (
"fmt"
"regexp"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/emirpasic/gods/maps/hashmap"
"github.com/gin-gonic/gin"
cmodel "github.com/open-falcon/falcon-plus/common/model"
h "github.com/open-falcon/falcon-plus/modules/api/app/helper"
m "github.com/open-falcon/falcon-plus/modules/api/app/model/graph"
u "github.com/open-falcon/falcon-plus/modules/api/app/utils"
)
type APIGrafanaMainQueryInputs struct {
Limit int `json:"limit" form:"limit"`
Query string `json:"query" form:"query"`
}
type APIGrafanaMainQueryOutputs struct {
Expandable bool `json:"expandable"`
Text string `json:"text"`
}
//for return a host list for api test
func repsonseDefault(limit int) (result []APIGrafanaMainQueryOutputs) {
result = []APIGrafanaMainQueryOutputs{}
//for get right table name
enpsHelp := m.Endpoint{}
enps := []m.Endpoint{}
db.Graph.Table(enpsHelp.TableName()).Limit(limit).Scan(&enps)
for _, h := range enps {
result = append(result, APIGrafanaMainQueryOutputs{
Expandable: true,
Text: h.Endpoint,
})
}
return
}
//for find host list & grafana template searching, regexp support
func responseHostsRegexp(limit int, regexpKey string) (result []APIGrafanaMainQueryOutputs) {
result = []APIGrafanaMainQueryOutputs{}
//for get right table name
enpsHelp := m.Endpoint{}
enps := []m.Endpoint{}
db.Graph.Table(enpsHelp.TableName()).Where("endpoint regexp ?", regexpKey).Limit(limit).Scan(&enps)
for _, h := range enps {
result = append(result, APIGrafanaMainQueryOutputs{
Expandable: true,
Text: h.Endpoint,
})
}
return
}
//for resolve mixed query with endpoint & counter of query string
func cutEndpointCounterHelp(regexpKey string) (hosts []string, counter string) {
r, _ := regexp.Compile("^{?([^#}]+)}?#(.+)")
matchedList := r.FindAllStringSubmatch(regexpKey, 1)
if len(matchedList) != 0 {
if len(matchedList[0]) > 1 {
//get hosts
hostsTmp := matchedList[0][1]
counterTmp := matchedList[0][2]
hosts = strings.Split(hostsTmp, ",")
counter = strings.Replace(counterTmp, "#", "\\.", -1)
}
} else {
log.Errorf("grafana query inputs error: %v", regexpKey)
}
return
}
func expandableChecking(counter string, counterSearchKeyWord string) (expsub string, needexp bool) {
re := regexp.MustCompile("(\\.\\+|\\.\\*)\\s*$")
counterSearchKeyWord = re.ReplaceAllString(counterSearchKeyWord, "")
counterSearchKeyWord = strings.Replace(counterSearchKeyWord, "\\.", ".", -1)
expCheck := strings.Replace(counter, counterSearchKeyWord, "", -1)
if expCheck == "" {
needexp = false
expsub = expCheck
} else {
needexp = true
re = regexp.MustCompile("^\\.")
expsubArr := strings.Split(re.ReplaceAllString(expCheck, ""), ".")
switch len(expsubArr) {
case 0:
expsub = ""
case 1:
expsub = expsubArr[0]
needexp = false
default:
expsub = expsubArr[0]
needexp = true
}
}
return
}
/* add additional items (ex. $ & %)
$ means metric is stop on here. no need expand any more.
% means a wirecard string.
also clean defecate metrics
*/
func addAddItionalItems(items []APIGrafanaMainQueryOutputs, regexpKey string) (result []APIGrafanaMainQueryOutputs) {
flag := false
mapset := hashmap.New()
for _, i := range items {
if !i.Expandable {
flag = true
}
if val, exist := mapset.Get(i.Text); exist {
if val != i.Expandable && i.Expandable {
mapset.Put(i.Text, i.Expandable)
}
} else {
mapset.Put(i.Text, i.Expandable)
}
}
result = make([]APIGrafanaMainQueryOutputs, mapset.Size())
for idx, ctmp := range mapset.Keys() {
c := ctmp.(string)
val, _ := mapset.Get(c)
result[idx] = APIGrafanaMainQueryOutputs{
Text: c,
Expandable: val.(bool),
}
}
if flag {
result = append(result, APIGrafanaMainQueryOutputs{
Text: "$",
Expandable: false,
})
}
if len(strings.Split(regexpKey, "\\.")) > 0 {
result = append(result, APIGrafanaMainQueryOutputs{
Text: "%",
Expandable: false,
})
}
return
}
func findEndpointIdByEndpointList(hosts []string) []int64 {
//for get right table name
enpsHelp := m.Endpoint{}
enps := []m.Endpoint{}
hostStr, _ := u.ArrStringsToString(hosts)
log.Debugf("hostStr: %v", hostStr)
log.Debugf("endpoint in (%s)", hostStr)
db.Graph.Table(enpsHelp.TableName()).Where(fmt.Sprintf("endpoint in (%s)", hostStr)).Scan(&enps)
hostIds := make([]int64, len(enps))
for indx, h := range enps {
hostIds[indx] = int64(h.ID)
}
return hostIds
}
//for reture counter list of endpoints
func responseCounterRegexp(regexpKey string) (result []APIGrafanaMainQueryOutputs) {
result = []APIGrafanaMainQueryOutputs{}
hosts, counter := cutEndpointCounterHelp(regexpKey)
if len(hosts) == 0 || counter == "" {
return
}
hostIds := findEndpointIdByEndpointList(hosts)
//if not any endpoint matched
if len(hostIds) == 0 {
return
}
idConcact, _ := u.ArrInt64ToString(hostIds)
//for get right table name
countHelp := m.EndpointCounter{}
counters := []m.EndpointCounter{}
db.Graph.Table(countHelp.TableName()).Where(fmt.Sprintf("endpoint_id IN (%s) AND counter regexp '%s'", idConcact, counter)).Scan(&counters)
//if not any counter matched
if len(counters) == 0 {
return
}
for _, c := range counters {
expsub, needexp := expandableChecking(c.Counter, counter)
result = append(result, APIGrafanaMainQueryOutputs{
Text: expsub,
Expandable: needexp,
})
}
result = addAddItionalItems(result, regexpKey)
return
}
func GrafanaMainQuery(c *gin.Context) {
inputs := APIGrafanaMainQueryInputs{}
inputs.Limit = 1000
inputs.Query = "!N!"
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err.Error())
return
}
log.Debugf("got query string: %s", inputs.Query)
output := []APIGrafanaMainQueryOutputs{}
if inputs.Query == "!N!" {
output = repsonseDefault(inputs.Limit)
} else if !strings.Contains(inputs.Query, "#") {
output = responseHostsRegexp(inputs.Limit, inputs.Query)
} else if strings.Contains(inputs.Query, "#") && !strings.Contains(inputs.Query, "#select metric") {
output = responseCounterRegexp(inputs.Query)
}
c.JSON(200, output)
return
}
type APIGrafanaRenderInput struct {
Target []string `json:"target" form:"target" binding:"required"`
From int64 `json:"from" form:"from" binding:"required"`
Until int64 `json:"until" form:"until" binding:"required"`
Format string `json:"format" form:"format"`
MaxDataPoints int64 `json:"maxDataPoints" form:"maxDataPoints"`
Step int `json:"step" form:"step"`
ConsolFun string `json:"consolFun" form:"consolFun"`
}
func GrafanaRender(c *gin.Context) {
inputs := APIGrafanaRenderInput{}
//set default step is 60
inputs.Step = 60
inputs.ConsolFun = "AVERAGE"
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err.Error())
return
}
respList := []*cmodel.GraphQueryResponse{}
for _, target := range inputs.Target {
hosts, counter := cutEndpointCounterHelp(target)
//clean characters
log.Debug(counter)
re := regexp.MustCompile("\\\\.\\$\\s*$")
flag := re.MatchString(counter)
counter = re.ReplaceAllString(counter, "")
counter = strings.Replace(counter, "\\.%", ".+", -1)
ecHelp := m.EndpointCounter{}
counters := []m.EndpointCounter{}
hostIds := findEndpointIdByEndpointList(hosts)
if flag {
db.Graph.Table(ecHelp.TableName()).Select("distinct counter").Where(fmt.Sprintf("endpoint_id IN (%s) AND counter = '%s'", u.ArrInt64ToStringMust(hostIds), counter)).Scan(&counters)
} else {
db.Graph.Table(ecHelp.TableName()).Select("distinct counter").Where(fmt.Sprintf("endpoint_id IN (%s) AND counter regexp '%s'", u.ArrInt64ToStringMust(hostIds), counter)).Scan(&counters)
}
if len(counters) == 0 {
// 没有匹配到的继续执行,避免当grafana graph有多个查询时,其他正常的查询也无法渲染视图
continue
}
counterArr := make([]string, len(counters))
for indx, c := range counters {
counterArr[indx] = c.Counter
}
for _, host := range hosts {
for _, c := range counterArr {
resp, err := fetchData(host, c, inputs.ConsolFun, inputs.From, inputs.Until, inputs.Step)
if err != nil {
log.Debugf("query graph got error with: %v", inputs)
} else {
respList = append(respList, resp)
}
}
}
}
c.JSON(200, respList)
return
}