This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
main.go
294 lines (263 loc) · 9.03 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"flag"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/grafana/metrictank/cmd/mt-index-cat/out"
"github.com/grafana/metrictank/conf"
"github.com/grafana/metrictank/idx/cassandra"
"github.com/grafana/metrictank/idx/memory"
"github.com/grafana/metrictank/logger"
"github.com/grafana/metrictank/schema"
"github.com/raintank/dur"
log "github.com/sirupsen/logrus"
)
func init() {
formatter := &logger.TextFormatter{}
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
log.SetFormatter(formatter)
log.SetLevel(log.InfoLevel)
}
func perror(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
func main() {
var addr string
var prefix string
var substr string
var suffix string
var regexStr string
var regex *regexp.Regexp
var tags string
var from string
var maxStale string
var minStale 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(®exStr, "regex", "", "only show metrics that match this regex")
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(&maxStale, "max-stale", "6h30min", "exclude series that have not been seen for this much time (compared against LastUpdate). use 0 to disable")
globalFlags.StringVar(&minStale, "min-stale", "0", "exclude series that have been seen in this much time (compared against LastUpdate). 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.Println("output:")
fmt.Println()
fmt.Printf(" * presets: %v\n", strings.Join(outputs, "|"))
fmt.Println(" * templates, which may contain:")
fmt.Println(" - fields, e.g. '{{.Id}} {{.OrgId}} {{.Name}} {{.Interval}} {{.Unit}} {{.Mtype}} {{.Tags}} {{.LastUpdate}} {{.Partition}}'")
fmt.Println(" - methods, e.g. '{{.NameWithTags}}' (works basically the same as a field)")
fmt.Println(" - processing functions:")
fmt.Println(" pattern: transforms a graphite.style.metric.name into a pattern with wildcards inserted")
fmt.Println(" an operation is randomly selected between: replacing a node with a wildcard, replacing a character with a wildcard, and passthrough")
out.PatternCustomUsage(" ")
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()
fmt.Println()
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")
fmt.Println("mt-index-cat cass -hosts localhost:9042 -schema-file ../../scripts/config/schema-idx-cassandra.toml '{{.Name | patternCustom 15 \"pass\" 40 \"1rcnw\" 15 \"2rcnw\" 10 \"3rcnw\" 10 \"3rccw\" 10 \"2rccw\"}}\\n'")
}
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)
}
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.CliConfig.Enabled = true
if regexStr != "" {
var err error
regex, err = regexp.Compile(regexStr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
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(cassandra.CliConfig)
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)
}
memory.IndexRules = conf.IndexRules{
Rules: nil,
Default: conf.IndexRule{
Name: "default",
Pattern: regexp.MustCompile(""),
MaxStale: 0,
},
}
if maxStale != "0" {
maxStaleInt, err := dur.ParseNDuration(maxStale)
perror(err)
memory.IndexRules.Default.MaxStale = time.Duration(maxStaleInt) * time.Second
}
var cutoffMin int64
if minStale != "0" {
minStaleInt, err := dur.ParseNDuration(minStale)
perror(err)
cutoffMin = time.Now().Unix() - int64(minStaleInt)
}
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))
}
}
var defs []schema.MetricDefinition
if len(partitions) == 0 {
defs = idx.Load(nil, time.Now())
} else {
defs = idx.LoadPartitions(partitions, nil, time.Now())
}
// set this after doing the query, to assure age can't possibly be negative unless if clocks are misconfigured.
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 regex != nil && !regex.MatchString(d.Name) {
continue
}
if tags == "valid" || tags == "invalid" {
err := schema.ValidateTags(d.Tags)
// skip the metric if the validation result is not what we want
if (err == nil) != (tags == "valid") {
continue
}
}
if cutoffMin != 0 && d.LastUpdate >= cutoffMin {
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)
}
}