forked from bzed/whisper-to-graphite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
190 lines (171 loc) · 3.9 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
package main
import (
"errors"
"flag"
"github.com/bzed/go-whisper"
"github.com/marpaia/graphite-golang"
"log"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
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,
) error {
metricName, err := convertFilename(filename, baseDirectory)
if err != nil {
return err
}
whisperData, err := whisper.Open(filename)
if err != nil {
return err
}
archiveDataPoints, err := whisperData.DumpArchives()
if err != nil {
return err
}
metrics := make([]graphite.Metric, 0, 1000)
for _, dataPoint := range archiveDataPoints {
interval, value := dataPoint.Point()
if math.IsNaN(value) || interval == 0 || interval < fromTs || interval > toTs {
continue
}
v := strconv.FormatFloat(value, 'f', 20, 64)
metrics = append(metrics, graphite.NewMetric(metricName, v, int64(interval)))
}
err = graphiteConn.SendMetrics(metrics)
if err != nil {
return err
}
err = nil
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) {
defer wg.Done()
graphiteConn, err := graphite.GraphiteFactory(graphiteProtocol, graphiteHost, graphitePort, "")
if err != nil {
return
}
for {
select {
case path := <-ch:
{
err := sendWhisperData(path, baseDirectory, graphiteConn, fromTs, toTs)
if err != nil {
log.Println("Failed: " + path)
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")
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
wg.Add(*workers)
for i := 0; i < *workers; i++ {
go worker(ch, quit, &wg, *baseDirectory, *graphiteHost, *graphitePort, *graphiteProtocol, *fromTs, *toTs)
}
go findWhisperFiles(ch, quit, *directory)
wg.Wait()
}