forked from fwtpe/owl-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
185 lines (155 loc) · 4.37 KB
/
run.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
package cron
import (
"fmt"
"github.com/fwtpe/owl-backend/modules/aggregator/g"
"github.com/fwtpe/owl-backend/sdk/portal"
"github.com/fwtpe/owl-backend/sdk/sender"
log "github.com/sirupsen/logrus"
"strconv"
"strings"
"time"
)
func WorkerRun(item *g.Cluster) {
debug := g.Config().Debug
numeratorStr := cleanParam(item.Numerator)
denominatorStr := cleanParam(item.Denominator)
if !expressionValid(numeratorStr) || !expressionValid(denominatorStr) {
log.Println("[W] invalid numerator or denominator", item)
return
}
needComputeNumerator := needCompute(numeratorStr)
needComputeDenominator := needCompute(denominatorStr)
if !needComputeNumerator && !needComputeDenominator {
log.Println("[W] no need compute", item)
return
}
numeratorOperands, numeratorOperators := parse(numeratorStr, needComputeNumerator)
denominatorOperands, denominatorOperators := parse(denominatorStr, needComputeDenominator)
if !operatorsValid(numeratorOperators) || !operatorsValid(denominatorOperators) {
log.Println("[W] operators invalid", item)
return
}
hostnames, err := portal.Hostnames(fmt.Sprintf("%d", item.GroupId))
if err != nil || len(hostnames) == 0 {
return
}
now := time.Now().Unix()
valueMap, err := queryCounterLast(numeratorOperands, denominatorOperands, hostnames, now-int64(item.Step*2), now)
if err != nil {
log.Println("[E]", err, item)
return
}
var numerator, denominator float64
var validCount int
for _, hostname := range hostnames {
var numeratorVal, denominatorVal float64
var (
numeratorValid = true
denominatorValid = true
)
if needComputeNumerator {
numeratorVal, numeratorValid = compute(numeratorOperands, numeratorOperators, hostname, valueMap)
if !numeratorValid && debug {
log.Printf("[W] [hostname:%s] [numerator:%s] invalid or not found", hostname, item.Numerator)
}
}
if needComputeDenominator {
denominatorVal, denominatorValid = compute(denominatorOperands, denominatorOperators, hostname, valueMap)
if !denominatorValid && debug {
log.Printf("[W] [hostname:%s] [denominator:%s] invalid or not found", hostname, item.Denominator)
}
}
if numeratorValid && denominatorValid {
numerator += numeratorVal
denominator += denominatorVal
validCount += 1
}
}
if !needComputeNumerator {
if numeratorStr == "$#" {
numerator = float64(validCount)
} else {
numerator, err = strconv.ParseFloat(numeratorStr, 64)
if err != nil {
log.Printf("[E] strconv.ParseFloat(%s) fail %v", numeratorStr, item)
return
}
}
}
if !needComputeDenominator {
if denominatorStr == "$#" {
denominator = float64(validCount)
} else {
denominator, err = strconv.ParseFloat(denominatorStr, 64)
if err != nil {
log.Printf("[E] strconv.ParseFloat(%s) fail %v", denominatorStr, item)
return
}
}
}
if denominator == 0 {
log.Println("[W] denominator == 0", item)
return
}
sender.Push(item.Endpoint, item.Metric, item.Tags, numerator/denominator, item.DsType, int64(item.Step))
}
func parse(expression string, needCompute bool) (operands []string, operators []uint8) {
if !needCompute {
return
}
// e.g. $(cpu.busy)+$(cpu.idle)-$(cpu.nice)-$(cpu.guest)
// xx -- -- -- x
newExpression := expression[2 : len(expression)-1]
arr := strings.Split(newExpression, "$(")
count := len(arr)
if count == 1 {
operands = append(operands, arr[0])
return
}
if count > 1 {
for i := 0; i < count; i++ {
item := arr[i]
length := len(item)
if i == count-1 {
operands = append(operands, item)
continue
}
operators = append(operators, item[length-1])
operands = append(operands, item[0:length-2])
}
}
return
}
func cleanParam(val string) string {
val = strings.TrimSpace(val)
val = strings.Replace(val, " ", "", -1)
val = strings.Replace(val, "\r", "", -1)
val = strings.Replace(val, "\n", "", -1)
val = strings.Replace(val, "\t", "", -1)
return val
}
// $#
// 200
// $(cpu.busy) + $(cpu.idle)
func needCompute(val string) bool {
if strings.Contains(val, "$(") {
return true
}
return false
}
func expressionValid(val string) bool {
// use chinese character?
if strings.Contains(val, "(") || strings.Contains(val, ")") {
return false
}
return true
}
func operatorsValid(ops []uint8) bool {
count := len(ops)
for i := 0; i < count; i++ {
if ops[i] != '+' && ops[i] != '-' {
return false
}
}
return true
}