-
Notifications
You must be signed in to change notification settings - Fork 491
/
main.go
149 lines (138 loc) · 3.55 KB
/
main.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
// A simple application to bulk denormalize historic data.
// For ongoing denormalization use the functionality in tsdbrelay.
package main
import (
"flag"
"fmt"
"log"
"strconv"
"time"
"bosun.org/cmd/tsdbrelay/denormalize"
"bosun.org/collect"
"bosun.org/opentsdb"
)
var (
start = flag.String("start", "2013/01/01", "Start date to backfill.")
end = flag.String("end", "", "End date to backfill. Will go to now if not specified.")
ruleFlag = flag.String("rule", "", "A denormalization rule. ex `os.cpu__host`")
tsdbHost = flag.String("host", "", "opentsdb host")
batchSize = flag.Int("batch", 500, "batch size to send points to openTsdb")
)
func main() {
flag.Parse()
if *tsdbHost == "" {
flag.PrintDefaults()
log.Fatal("host must be supplied")
}
if *ruleFlag == "" {
flag.PrintDefaults()
log.Fatal("rule must be supplied")
}
rules, err := denormalize.ParseDenormalizationRules(*ruleFlag)
if err != nil {
log.Fatal(err)
}
if len(rules) > 1 {
log.Fatal("Please specify only one rule")
}
var rule *denormalize.DenormalizationRule
var metric string
for k, v := range rules {
metric = k
rule = v
}
query := &opentsdb.Query{Metric: metric, Aggregator: "avg"}
query.Tags = queryForAggregateTags(query)
fmt.Println(query)
startDate, err := opentsdb.ParseTime(*start)
if err != nil {
log.Fatal(err)
}
endDate := time.Now().UTC()
if *end != "" {
endDate, err = opentsdb.ParseTime(*end)
if err != nil {
log.Fatal(err)
}
}
backfill := func(batchStart, batchEnd time.Time) (err error) {
startTimeString := batchStart.Format(opentsdb.TSDBTimeFormat)
endTimeString := batchEnd.Format(opentsdb.TSDBTimeFormat)
defer func() {
if err != nil {
log.Fatalf("Error on batch %s - %s. %v \n", startTimeString, endTimeString, err)
}
}()
req := opentsdb.Request{Start: startTimeString, End: endTimeString, Queries: []*opentsdb.Query{query}}
resp, err := req.Query(*tsdbHost)
if err != nil {
return err
}
dps := []*opentsdb.DataPoint{}
for _, r := range resp {
for t, p := range r.DPS {
timeStamp, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return err
}
dp := &opentsdb.DataPoint{
Timestamp: timeStamp,
Metric: r.Metric,
Tags: r.Tags,
Value: p,
}
err = rule.Translate(dp)
if err != nil {
return err
}
dps = append(dps, dp)
}
}
fmt.Printf("%s - %s: %d dps\n", startTimeString, endTimeString, len(dps))
total := 0
for len(dps) > 0 {
count := len(dps)
if len(dps) > *batchSize {
count = *batchSize
}
putResp, err := collect.SendDataPoints(dps[:count], "http://"+*tsdbHost)
if err != nil {
return err
}
if putResp.StatusCode != 200 {
return fmt.Errorf("Non 200 status code from opentsdb: %d", putResp.StatusCode)
}
dps = dps[count:]
total += count
}
fmt.Printf("Relayed %d data points.\n", total)
return nil
}
// walk backwards a day at a time
curEnd := endDate
for curEnd.After(startDate) {
curStart := curEnd.Add(-24 * time.Hour)
if curStart.Before(startDate) {
curStart = startDate
}
backfill(curStart, curEnd)
curEnd = curEnd.Add(-24 * time.Hour)
}
}
func queryForAggregateTags(query *opentsdb.Query) opentsdb.TagSet {
req := opentsdb.Request{}
req.Queries = []*opentsdb.Query{query}
req.Start = "1h-ago"
resp, err := req.Query(*tsdbHost)
if err != nil {
log.Fatal(err)
}
if len(resp) < 1 {
log.Fatal("No points in last hour to learn aggregate tags")
}
tagset := make(opentsdb.TagSet)
for _, t := range resp[0].AggregateTags {
tagset[t] = "*"
}
return tagset
}