This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathmain.go
237 lines (212 loc) · 7.07 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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/grafana/metrictank/cmd/mt-index-cat/out"
"github.com/grafana/metrictank/idx/cassandra"
"github.com/raintank/dur"
"github.com/raintank/schema"
)
func perror(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
var addr string
var prefix string
var substr string
var suffix string
var tags string
var from string
var maxAge string
var verbose bool
var limit int
var partitionStr string
globalFlags := flag.NewFlagSet("global config flags", flag.ExitOnError)
globalFlags.StringVar(&addr, "addr", "http://localhost:6060", "graphite/metrictank address")
globalFlags.StringVar(&prefix, "prefix", "", "only show metrics that have this prefix")
globalFlags.StringVar(&substr, "substr", "", "only show metrics that have this substring")
globalFlags.StringVar(&suffix, "suffix", "", "only show metrics that have this suffix")
globalFlags.StringVar(&partitionStr, "partitions", "*", "only show metrics from the comma separated list of partitions or * for all")
globalFlags.StringVar(&tags, "tags", "", "tag filter. empty (default), 'some', 'none', 'valid', or 'invalid'")
globalFlags.StringVar(&from, "from", "30min", "for vegeta outputs, will generate requests for data starting from now minus... eg '30min', '5h', '14d', etc. or a unix timestamp")
globalFlags.StringVar(&maxAge, "max-age", "6h30min", "max age (last update diff with now) of metricdefs. use 0 to disable")
globalFlags.IntVar(&limit, "limit", 0, "only show this many metrics. use 0 to disable")
globalFlags.BoolVar(&verbose, "verbose", false, "print stats to stderr")
cassFlags := cassandra.ConfigSetup()
outputs := []string{"dump", "list", "vegeta-render", "vegeta-render-patterns"}
flag.Usage = func() {
fmt.Println("mt-index-cat")
fmt.Println()
fmt.Println("Retrieves a metrictank index and dumps it in the requested format")
fmt.Println("In particular, the vegeta outputs are handy to pipe requests for given series into the vegeta http benchmark tool")
fmt.Println()
fmt.Printf("Usage:\n\n")
fmt.Printf(" mt-index-cat [global config flags] <idxtype> [idx config flags] output \n\n")
fmt.Printf("global config flags:\n\n")
globalFlags.PrintDefaults()
fmt.Println()
fmt.Println("tags filter:")
fmt.Println(" '' no filtering based on tags")
fmt.Println(" 'none' only show metrics that have no tags")
fmt.Println(" 'some' only show metrics that have one or more tags")
fmt.Println(" 'valid' only show metrics whose tags (if any) are valid")
fmt.Println(" 'invalid' only show metrics that have one or more invalid tags")
fmt.Println()
fmt.Printf("idxtype: only 'cass' supported for now\n\n")
fmt.Printf("cass config flags:\n\n")
cassFlags.PrintDefaults()
fmt.Println()
fmt.Printf("output: either presets like %v\n", strings.Join(outputs, "|"))
fmt.Printf("output: or custom templates like '{{.Id}} {{.OrgId}} {{.Name}} {{.Metric}} {{.Interval}} {{.Unit}} {{.Mtype}} {{.Tags}} {{.LastUpdate}} {{.Partition}}'\n\n\n")
fmt.Println("You may also use processing functions in templates:")
fmt.Println("pattern: transforms a graphite.style.metric.name into a pattern with wildcards inserted")
fmt.Println("age: subtracts the passed integer (typically .LastUpdate) from the query time")
fmt.Println("roundDuration: formats an integer-seconds duration using aggressive rounding. for the purpose of getting an idea of overal metrics age")
fmt.Println("EXAMPLES:")
fmt.Println("mt-index-cat -from 60min cass -hosts cassandra:9042 list")
fmt.Println("mt-index-cat -from 60min cass -hosts cassandra:9042 'sumSeries({{.Name | pattern}})'")
fmt.Println("mt-index-cat -from 60min cass -hosts cassandra:9042 'GET http://localhost:6060/render?target=sumSeries({{.Name | pattern}})&from=-6h\\nX-Org-Id: 1\\n\\n'")
fmt.Println("mt-index-cat cass -hosts cassandra:9042 -timeout 60s '{{.LastUpdate | age | roundDuration}}\\n' | sort | uniq -c")
}
if len(os.Args) == 2 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
flag.Usage()
os.Exit(0)
}
if len(os.Args) < 3 {
flag.Usage()
os.Exit(-1)
}
var partitions []int32
if partitionStr != "*" {
for _, p := range strings.Split(partitionStr, ",") {
p = strings.TrimSpace(p)
// handle trailing "," on the list of partitions.
if p == "" {
continue
}
id, err := strconv.ParseInt(p, 10, 32)
if err != nil {
log.Printf("invalid partition id %q. must be a int32", p)
flag.Usage()
os.Exit(-1)
}
partitions = append(partitions, int32(id))
}
}
format := os.Args[len(os.Args)-1]
var found bool
if strings.Contains(format, "{{") {
found = true
} else {
for _, output := range outputs {
if format == output {
found = true
break
}
}
}
if !found {
log.Printf("invalid output %q", format)
flag.Usage()
os.Exit(-1)
}
var cassI int
for i, v := range os.Args {
if v == "cass" {
cassI = i
}
}
if cassI == 0 {
log.Println("only indextype 'cass' supported")
flag.Usage()
os.Exit(1)
}
if tags != "" && tags != "valid" && tags != "invalid" && tags != "some" && tags != "none" {
log.Println("invalid tags filter")
flag.Usage()
os.Exit(1)
}
globalFlags.Parse(os.Args[1:cassI])
cassFlags.Parse(os.Args[cassI+1 : len(os.Args)-1])
cassandra.Enabled = true
var show func(d schema.MetricDefinition)
switch format {
case "dump":
show = out.Dump
case "list":
show = out.List
case "vegeta-render":
show = out.GetVegetaRender(addr, from)
case "vegeta-render-patterns":
show = out.GetVegetaRenderPattern(addr, from)
default:
show = out.Template(format)
}
idx := cassandra.New()
err := idx.InitBare()
perror(err)
// from should either be a unix timestamp, or a specification that graphite/metrictank will recognize.
_, err = strconv.Atoi(from)
if err != nil {
_, err = dur.ParseNDuration(from)
perror(err)
}
var cutoff uint32
if maxAge != "0" {
maxAgeInt, err := dur.ParseNDuration(maxAge)
perror(err)
cutoff = uint32(time.Now().Unix() - int64(maxAgeInt))
}
var defs []schema.MetricDefinition
if len(partitions) == 0 {
defs = idx.Load(nil, cutoff)
} else {
defs = idx.LoadPartitions(partitions, nil, cutoff)
}
// set this after doing the query, to assure age can't possibly be negative
out.QueryTime = time.Now().Unix()
total := len(defs)
shown := 0
for _, d := range defs {
// note that prefix and substr can be "", meaning filter disabled.
// the conditions handle this fine as well.
if !strings.HasPrefix(d.Name, prefix) {
continue
}
if !strings.HasSuffix(d.Name, suffix) {
continue
}
if !strings.Contains(d.Name, substr) {
continue
}
if tags == "none" && len(d.Tags) != 0 {
continue
}
if tags == "some" && len(d.Tags) == 0 {
continue
}
if tags == "valid" || tags == "invalid" {
valid := schema.ValidateTags(d.Tags)
// skip the metric if the validation result is not what we want
if valid != (tags == "valid") {
continue
}
}
show(d)
shown += 1
if shown == limit {
break
}
}
if verbose {
fmt.Fprintf(os.Stderr, "total: %d\n", total)
fmt.Fprintf(os.Stderr, "shown: %d\n", shown)
}
}