-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
148 lines (137 loc) · 3.52 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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Name = "styx"
app.Usage = "Export metrics from prometheus"
app.Action = exportAction
app.Flags = []cli.Flag{
&cli.DurationFlag{
Name: "duration,d",
Usage: "The duration to get timeseries from",
Value: time.Hour,
},
&cli.TimestampFlag{
Name: "start,s",
Usage: "The start time to get timeseries from",
Layout: "2006-01-02T15:04:05",
Value: cli.NewTimestamp(time.Now().Truncate(time.Hour)),
Timezone: time.Local,
},
&cli.DurationFlag{
Name: "resolution,r",
Usage: "The requested resolution of the timeseries in seconds (default 1s)",
Value: time.Second,
},
&cli.BoolFlag{
Name: "header",
Usage: "Include a header into the csv file",
},
&cli.StringFlag{
Name: "prometheus",
Value: "http://localhost:9090",
},
}
app.Commands = []*cli.Command{{
Name: "gnuplot",
Usage: "Directly plot a graph with gnuplot",
Action: gnuplotAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "prometheus",
Value: "http://localhost:9090",
},
&cli.DurationFlag{
Name: "duration,d",
Usage: "The duration to get timeseries from",
Value: time.Hour,
},
&cli.TimestampFlag{
Name: "start,s",
Usage: "The start time to get timeseries from",
Layout: "2006-01-02T15:04:05",
Value: cli.NewTimestamp(time.Now().Truncate(time.Hour)),
Timezone: time.Local,
},
&cli.DurationFlag{
Name: "resolution,r",
Usage: "The requested resolution of the timeseries in seconds (default 1s)",
Value: time.Second,
},
&cli.StringFlag{
Name: "title",
Usage: "Give the gnuplot graph a title",
},
},
}, {
Name: "matplotlib",
Usage: "Generate a file that uses matplotlib",
Action: matplotlibAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "prometheus",
Value: "http://localhost:9090",
},
&cli.DurationFlag{
Name: "duration,d",
Usage: "The duration to get timeseries from",
Value: time.Hour,
},
&cli.TimestampFlag{
Name: "start,s",
Usage: "The start time to get timeseries from",
Layout: "2006-01-02T15:04:05",
Value: cli.NewTimestamp(time.Now().Truncate(time.Hour)),
Timezone: time.Local,
},
&cli.DurationFlag{
Name: "resolution,r",
Usage: "The requested resolution of the timeseries in seconds (default 1s)",
Value: time.Second,
},
&cli.StringFlag{
Name: "title",
Usage: "Give the gnuplot graph a title",
},
},
}}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func exportAction(c *cli.Context) error {
if !c.Args().Present() {
return fmt.Errorf(color.RedString("need a query to run"))
}
start := c.Timestamp("start")
end := start.Add(c.Duration("duration"))
prometheus := c.String("prometheus")
// if resolution is not set, use 1s as default
resolution := c.Duration("resolution")
if resolution == 0 || resolution == time.Duration(0) {
resolution = time.Second
}
if resolution < time.Second {
return fmt.Errorf(color.RedString("resolution must be >= 1s"))
}
// fmt.Printf("Querying %s from %s to %s\n", c.Args().First(), *start, end)
results, err := Query(prometheus, *start, end, resolution, c.Args().First())
if err != nil {
return err
}
// Only add a line as header when the flag is true
header := c.Bool("header")
if header {
if err := csvHeaderWriter(os.Stdout, results); err != nil {
return err
}
}
return csvWriter(os.Stdout, results)
}