-
Notifications
You must be signed in to change notification settings - Fork 6
/
dump.go
264 lines (241 loc) · 5.77 KB
/
dump.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
package main
import (
"errors"
"flag"
"log"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/bzed/go-whisper"
"github.com/marpaia/graphite-golang"
)
type rateLimiter struct {
pointsPerSecond int64
currentPoints int64
full chan bool
lock *sync.Mutex
enabled bool
}
func newRateLimiter(pointsPerSecond int64) *rateLimiter {
rl := new(rateLimiter)
rl.pointsPerSecond = pointsPerSecond
rl.currentPoints = 0
rl.full = make(chan bool)
rl.lock = new(sync.Mutex)
if pointsPerSecond == 0 {
rl.enabled = false
} else {
rl.enabled = true
go func() {
for {
time.Sleep(1 * time.Second)
select {
case <-rl.full:
default:
}
}
}()
return rl
}
return rl
}
func (rl *rateLimiter) limit(n int64) {
if !rl.enabled {
return
}
rl.lock.Lock()
defer rl.lock.Unlock()
rl.currentPoints += n
if rl.currentPoints >= rl.pointsPerSecond {
rl.full <- true
rl.currentPoints = 0
}
}
func convertFilename(filename string, baseDirectory string) (string, error) {
absFilename, err := filepath.Abs(filename)
if err != nil {
return "", err
}
absBaseDirectory, err := filepath.Abs(baseDirectory)
if err != nil {
return "", err
}
err = nil
if strings.HasPrefix(absFilename, absBaseDirectory) {
metric := strings.Replace(
strings.TrimPrefix(
strings.TrimSuffix(
strings.TrimPrefix(
absFilename,
absBaseDirectory),
".wsp"),
"/"),
"/",
".",
-1)
return metric, err
}
err = errors.New("Path for whisper file does not live in BasePath")
return "", err
}
func sendWhisperData(
filename string,
baseDirectory string,
graphiteConn *graphite.Graphite,
fromTs int,
toTs int,
connectRetries int,
rateLimiter *rateLimiter,
) error {
metricName, err := convertFilename(filename, baseDirectory)
if err != nil {
return err
}
whisperData, err := whisper.Open(filename)
if err != nil {
return err
}
archiveDataPoints, err := whisperData.DumpDataPoints()
if err != nil {
return err
}
metrics := make([]graphite.Metric, 0, 1000)
for _, dataPoint := range archiveDataPoints {
interval, value := dataPoint.Point()
if math.IsNaN(value) || interval < fromTs || interval > toTs {
continue
}
v := strconv.FormatFloat(value, 'f', 20, 64)
metrics = append(metrics, graphite.NewMetric(metricName, v, int64(interval)))
}
rateLimiter.limit(int64(len(metrics)))
for r := 1; r <= connectRetries; r++ {
err = graphiteConn.SendMetrics(metrics)
if err != nil && r != connectRetries {
// Trying to reconnect to graphite with given parameters
sleep := time.Duration(r) * time.Second
log.Printf("Failed to send metric %v to graphite: %v", filename, err.Error())
log.Printf("Trying to reconnect and send metric again %v times", connectRetries-r)
log.Printf("Sleeping for %v", sleep)
time.Sleep(sleep)
graphiteConn.Connect()
} else {
break
}
}
if err != nil {
log.Printf("Failed to send metric %v after %v retries", filename, connectRetries)
return err
}
return err
}
func findWhisperFiles(ch chan string, quit chan int, directory string) {
visit := func(path string, info os.FileInfo, err error) error {
if (info != nil) && !info.IsDir() {
if strings.HasSuffix(path, ".wsp") {
ch <- path
}
}
return nil
}
err := filepath.Walk(directory, visit)
if err != nil {
log.Fatal(err)
}
close(quit)
}
func worker(ch chan string,
quit chan int,
wg *sync.WaitGroup,
baseDirectory string,
graphiteHost string,
graphitePort int,
graphiteProtocol string,
fromTs int,
toTs int,
connectRetries int,
rateLimiter *rateLimiter) {
defer wg.Done()
graphiteConn, err := graphite.GraphiteFactory(graphiteProtocol, graphiteHost, graphitePort, "")
if err != nil {
log.Printf("Failed to connect to graphite host with error: %v", err.Error())
return
}
for {
select {
case path := <-ch:
{
err := sendWhisperData(path, baseDirectory, graphiteConn, fromTs, toTs, connectRetries, rateLimiter)
if err != nil {
log.Println(err)
} else {
log.Println("OK: " + path)
}
}
case <-quit:
return
}
}
}
func main() {
baseDirectory := flag.String(
"basedirectory",
"/var/lib/graphite/whisper",
"Base directory where whisper files are located. Used to retrieve the metric name from the filename.")
directory := flag.String(
"directory",
"/var/lib/graphite/whisper/collectd",
"Directory containing the whisper files you want to send to graphite again")
graphiteHost := flag.String(
"host",
"127.0.0.1",
"Hostname/IP of the graphite server")
graphitePort := flag.Int(
"port",
2003,
"graphite Port")
graphiteProtocol := flag.String(
"protocol",
"tcp",
"Protocol to use to transfer graphite data (tcp/udp/nop)")
workers := flag.Int(
"workers",
5,
"Workers to run in parallel")
fromTs := flag.Int(
"from",
0,
"Starting timestamp to dump data from")
toTs := flag.Int(
"to",
2147483647,
"Ending timestamp to dump data up to")
pointsPerSecond := flag.Int64(
"pps",
0,
"Number of maximum points per second to send (0 means rate limiter is disabled)")
connectRetries := flag.Int(
"retries",
3,
"How many connection retries worker will make before failure. It is progressive and each next pause will be equal to 'retry * 1s'")
flag.Parse()
if !(*graphiteProtocol == "tcp" ||
*graphiteProtocol == "udp" ||
*graphiteProtocol == "nop") {
log.Fatalln("Graphite protocol " + *graphiteProtocol + " not supported, use tcp/udp/nop.")
}
ch := make(chan string)
quit := make(chan int)
var wg sync.WaitGroup
rl := newRateLimiter(*pointsPerSecond)
wg.Add(*workers)
for i := 0; i < *workers; i++ {
go worker(ch, quit, &wg, *baseDirectory, *graphiteHost, *graphitePort, *graphiteProtocol, *fromTs, *toTs, *connectRetries, rl)
}
go findWhisperFiles(ch, quit, *directory)
wg.Wait()
}