forked from leoleovich/grafsy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
272 lines (247 loc) · 7.23 KB
/
server.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 grafsy
import (
"bufio"
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
)
// The Server class to receive a data
type Server struct {
// User config.
Conf *Config
// Local config.
Lc *LocalConfig
// Pointer to Monitoring structure.
Mon *Monitoring
}
// Aggregate metrics with prefix.
func (s Server) aggrMetricsWithPrefix() {
for ; ; time.Sleep(time.Duration(s.Conf.AggrInterval) * time.Second) {
// We assume, that aggregation is done for a current point in time
aggrTimestamp := time.Now().Unix()
workingList := make(map[string]*metricData)
chanSize := len(s.Lc.aggrChannel)
for i := 0; i < chanSize; i++ {
split := strings.Fields(<-s.Lc.aggrChannel)
metricName := split[0]
value, err := strconv.ParseFloat(split[1], 64)
if err != nil {
s.Lc.lg.Println("Can not parse value of metric ", metricName, ": ", split[1])
continue
}
_, metricExist := workingList[metricName]
if !metricExist {
workingList[metricName] = &metricData{}
}
if strings.HasPrefix(metricName, s.Conf.SumPrefix) {
workingList[metricName].value += value
} else if strings.HasPrefix(metricName, s.Conf.AvgPrefix) {
workingList[metricName].value += value
workingList[metricName].amount++
} else if strings.HasPrefix(metricName, s.Conf.MinPrefix) {
if !metricExist {
workingList[metricName].value = value
} else if workingList[metricName].value > value {
workingList[metricName].value = value
}
} else if strings.HasPrefix(metricName, s.Conf.MaxPrefix) {
if workingList[metricName].value < value {
workingList[metricName].value = value
}
}
}
/*
We may have a problem, that working_list size will be bigger than main buffer/space in it.
But then go suppose to block appending into buffer and wait until space will be free.
I am not sure if we need to check free space of main buffer here...
*/
dropped := 0
for metricName, metricData := range workingList {
value := metricData.value
var prefix string
if strings.HasPrefix(metricName, s.Conf.SumPrefix) {
prefix = s.Conf.SumPrefix
} else if strings.HasPrefix(metricName, s.Conf.AvgPrefix) {
value = metricData.value / float64(metricData.amount)
prefix = s.Conf.AvgPrefix
} else if strings.HasPrefix(metricName, s.Conf.MinPrefix) {
prefix = s.Conf.MinPrefix
} else if strings.HasPrefix(metricName, s.Conf.MaxPrefix) {
prefix = s.Conf.MaxPrefix
}
select {
case s.Lc.mainChannel <- fmt.Sprintf("%s %.2f %d", strings.Replace(metricName, prefix, "", -1), value, aggrTimestamp):
default:
s.Lc.lg.Printf("Too many metrics in the main queue (%d). I can not append aggregated metrics", len(s.Lc.mainChannel))
dropped++
}
}
if dropped > 0 {
for _, carbonAddr := range s.Conf.CarbonAddrs {
s.Mon.Increase(&s.Mon.clientStat[carbonAddr].dropped, dropped)
}
}
}
}
func (s *Server) overwriteName(metric *string) {
for i, re := range s.Lc.overwriteRegexp {
if re.MatchString(*metric) {
*metric = re.ReplaceAllString(*metric, s.Conf.Overwrite[i].ReplaceWith)
return
}
}
}
// Validate metrics list in order:
// 1) Find proper channel for metric.
// 2) Check overflow of the channel.
// 3) Put metric in a proper channel.
func (s Server) cleanAndUseIncomingData(metrics []string) {
dropped := 0
aggregated := 0
for _, metric := range metrics {
s.overwriteName(&metric)
if s.Lc.allowedMetrics.MatchString(metric) {
if s.Lc.aggrRegexp.MatchString(metric) {
select {
case s.Lc.aggrChannel <- metric:
aggregated++
default:
s.Lc.lg.Println("Too many metrics in aggregating channel, drop metric: ", metric)
dropped++
}
} else {
select {
case s.Lc.mainChannel <- metric:
default:
s.Lc.lg.Println("Too many metrics in main channel, drop metric: ", metric)
dropped++
}
}
} else {
if metric != "" {
s.Mon.Increase(&s.Mon.serverStat.invalid, 1)
s.Lc.lg.Printf("Removing bad metric '%s' from the list", metric)
}
}
}
if dropped > 0 {
for _, carbonAddr := range s.Conf.CarbonAddrs {
s.Mon.Increase(&s.Mon.clientStat[carbonAddr].dropped, dropped)
}
}
if aggregated > 0 {
for _, carbonAddr := range s.Conf.CarbonAddrs {
s.Mon.Increase(&s.Mon.clientStat[carbonAddr].aggregated, aggregated)
}
}
}
// Reading metrics from network
func (s Server) handleRequest(conn net.Conn) {
defer conn.Close()
conBuf := bufio.NewReader(conn)
for {
s.Mon.Increase(&s.Mon.serverStat.net, 1)
metric, err := conBuf.ReadString('\n')
// Even if error occurred we still put "metric" into analysis, cause it can be a valid metric, but without \n
s.cleanAndUseIncomingData([]string{strings.Replace(strings.Replace(metric, "\r", "", -1), "\n", "", -1)})
if err != nil {
return
}
}
}
// Reading metrics from files in folder.
// This is a second way how to send metrics, except network.
func (s Server) handleDirMetrics() {
for ; ; time.Sleep(time.Duration(s.Conf.ClientSendInterval) * time.Second) {
entries, err := os.ReadDir(s.Conf.MetricDir)
if err != nil {
panic(err.Error())
}
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
panic(err.Error())
}
resultsList, _ := readMetricsFromFile(s.Conf.MetricDir + "/" + info.Name())
s.Mon.Increase(&s.Mon.serverStat.dir, len(resultsList))
s.cleanAndUseIncomingData(resultsList)
}
}
}
// handleListener handles incoming connections
func (s *Server) handleListener(addr *net.TCPAddr) {
// Listen for incoming connections.
l, err := net.ListenTCP("tcp", addr)
if err != nil {
s.Lc.lg.Println("Failed to run server:", err.Error())
os.Exit(1)
} else {
s.Lc.lg.Println("Server is running")
}
defer l.Close()
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
s.Lc.lg.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go s.handleRequest(conn)
}
}
// resolveBind takes a TCP bind string and resolves it to all
// ips associated with it in case a hostname is given.
// Named ports can also be used.
// It returns a list of corresponding *TCPAddr objects that can
// directly be used in net.ListenTCP().
//
// Example:
// localhost:ssh -> [127.0.0.1:22, [::1]:22]
func (s *Server) resolveBind() []*net.TCPAddr {
// Resolve hostname to ips
h, p, err := net.SplitHostPort(s.Conf.LocalBind)
if err != nil {
s.Lc.lg.Println("Failed to split bind address:", err.Error())
os.Exit(1)
}
ips, err := net.LookupIP(h)
if err != nil {
s.Lc.lg.Println("Failed to lookup IPs:", err.Error())
os.Exit(1)
}
// Resolve named ports
port, err := net.LookupPort("tcp", p)
if err != nil {
s.Lc.lg.Println("Failed to lookup port:", err.Error())
os.Exit(1)
}
// Create *TCPAddr objects
addrs := make([]*net.TCPAddr, 0, len(ips))
for _, ip := range ips {
addrs = append(addrs, &net.TCPAddr{
IP: ip,
Port: port,
})
}
return addrs
}
// Run server.
// Should be run in separate goroutine.
func (s *Server) Run() {
// Resolve listen endpoints and start listeners
for _, addr := range s.resolveBind() {
go s.handleListener(addr)
}
// Run goroutine for reading metrics from metricDir
go s.handleDirMetrics()
// Run goroutine for aggr metrics with prefix
go s.aggrMetricsWithPrefix()
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
}