Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 7583dff

Browse files
committed
add tool: mt-gen-key
fix #1289
1 parent e215a7c commit 7583dff

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

cmd/mt-gen-key/main.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"os/exec"
10+
"runtime"
11+
12+
"github.com/grafana/metrictank/logger"
13+
"github.com/grafana/metrictank/schema"
14+
log "github.com/sirupsen/logrus"
15+
)
16+
17+
var (
18+
version string
19+
showVersion = flag.Bool("version", false, "print version string")
20+
)
21+
22+
func init() {
23+
formatter := &logger.TextFormatter{}
24+
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
25+
log.SetFormatter(formatter)
26+
log.SetLevel(log.InfoLevel)
27+
}
28+
29+
func main() {
30+
31+
flag.Usage = func() {
32+
fmt.Println("mt-get-key")
33+
fmt.Println()
34+
fmt.Println("mt-gen-key gives you the MKey for a specific MetricDefinition")
35+
fmt.Println("It fills a temp file with a template MetricDefinition")
36+
fmt.Println("It launches vim")
37+
fmt.Println("You fill in the important details - name / interval / tags /...")
38+
fmt.Println("It prints the MKey")
39+
fmt.Println()
40+
flag.PrintDefaults()
41+
}
42+
flag.Parse()
43+
44+
if *showVersion {
45+
fmt.Printf("mt-view-boundaries (version: %s - runtime: %s)\n", version, runtime.Version())
46+
return
47+
}
48+
d := schema.MetricDefinition{
49+
Unit: "unknown",
50+
Mtype: "gauge",
51+
}
52+
53+
var data []byte
54+
var err error
55+
var f *os.File
56+
57+
if data, err = json.MarshalIndent(d, "", " "); err != nil {
58+
log.Fatal(err)
59+
}
60+
if f, err = ioutil.TempFile("", "mt-gen-metricid"); err != nil {
61+
log.Fatal(err)
62+
}
63+
fmt.Println("using file", f.Name())
64+
defer os.Remove(f.Name())
65+
66+
if _, err = f.Write(data); err != nil {
67+
log.Fatal(err)
68+
}
69+
if err := f.Close(); err != nil {
70+
log.Fatal(err)
71+
}
72+
cmd := exec.Command("vim", f.Name())
73+
cmd.Stdin = os.Stdin
74+
cmd.Stdout = os.Stdout
75+
cmd.Stderr = os.Stderr
76+
if err = cmd.Start(); err != nil {
77+
log.Fatal(err)
78+
}
79+
if err = cmd.Wait(); err != nil {
80+
log.Printf("Error while editing. Error: %v\n", err)
81+
} else {
82+
log.Printf("Successfully edited.")
83+
}
84+
85+
data, err = ioutil.ReadFile(f.Name())
86+
if err != nil {
87+
log.Fatal(err)
88+
}
89+
json.Unmarshal(data, &d)
90+
d.SetId()
91+
fmt.Println(d.Id.String())
92+
}

docs/tools.md

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ Example:
4444
```
4545

4646

47+
## mt-gen-key
48+
49+
```
50+
mt-get-key
51+
52+
mt-gen-key gives you the MKey for a specific MetricDefinition
53+
It fills a temp file with a template MetricDefinition
54+
It launches vim
55+
You fill in the important details - name / interval / tags /...
56+
It prints the MKey
57+
58+
-version
59+
print version string
60+
```
61+
62+
4763
## mt-index-cat
4864

4965
```

0 commit comments

Comments
 (0)