-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
156 lines (131 loc) · 5.2 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
/*
Prometheus ASCII Client
Copyright (C) 2020 Jacob Colvin (MacroPower)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package main
import (
"context"
"fmt"
"math"
"os"
"time"
prometheus "github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/MacroPower/asciigraph"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
name = "prometheus_ascii"
layout = "2006-01-02T15:04:05Z"
)
func queryPrometheus(promQuery string, server string, start time.Time, end time.Time, step time.Duration, logger log.Logger, options ...asciigraph.Option) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := prometheus.NewClient(prometheus.Config{Address: server})
if err != nil {
level.Error(logger).Log("msg", "Client error", "err", err)
os.Exit(1)
}
q := v1.NewAPI(client)
value, warn, err := q.QueryRange(ctx, promQuery, v1.Range{
Start: start,
End: end,
Step: step,
})
if len(warn) > 0 {
level.Error(logger).Log("msg", "Too many warnings for query", "query", promQuery, "warn", warn)
os.Exit(1)
}
if err != nil {
level.Error(logger).Log("msg", "Query error", "err", err)
os.Exit(1)
}
queryType := value.Type()
level.Info(logger).Log("msg", "Retrieved query result", "type", queryType.String())
switch {
case queryType == model.ValMatrix:
matrixVal := value.(model.Matrix)
for _, series := range matrixVal {
data := []float64{}
for _, elem := range series.Values {
data = append(data, float64(elem.Value))
}
graph := asciigraph.Plot(data, options...)
fmt.Println(graph)
}
default:
level.Error(logger).Log("msg", "Query error", "err", "ValueType of Query Result is unknown")
os.Exit(1)
}
}
func main() {
var (
promServer = kingpin.Flag("server.endpoint", "Prometheus server.").Envar("PROMETHEUS_ASCII_SERVER_ENDPOINT").Required().String()
promQuery = kingpin.Flag("query.selector", "Prometheus query to submit.").Envar("PROMETHEUS_ASCII_QUERY_SELECTOR").Required().String()
qDur = kingpin.Flag("query.duration", "Duration of query. Overwritten by start.").Envar("PROMETHEUS_ASCII_QUERY_DURATION").Default("24h").Duration()
qStart = kingpin.Flag("query.start", "Start time for query. Layout: "+layout).Envar("PROMETHEUS_ASCII_QUERY_START").String()
qEnd = kingpin.Flag("query.end", "End time for query. Defaults to now. Layout: "+layout).Envar("PROMETHEUS_ASCII_QUERY_END").String()
gCaption = kingpin.Flag("graph.query-caption", "Adds a caption to the graph with the current query").Envar("PROMETHEUS_ASCII_GRAPH_QUERYCAPTION").Default("False").Bool()
gWidth = kingpin.Flag("graph.width", "Width of the graph.").Envar("PROMETHEUS_ASCII_GRAPH_WIDTH").Default("100").Int()
gHeight = kingpin.Flag("graph.height", "Height of the graph.").Envar("PROMETHEUS_ASCII_GRAPH_HEIGHT").Default("10").Int()
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print(name))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
level.Info(logger).Log("msg", "Starting", "version", version.Info())
var qEndTime time.Time
var qEndTimeErr error
if *qEnd != "" {
qEndTime, qEndTimeErr = time.Parse(layout, *qEnd)
if qEndTimeErr != nil {
level.Error(logger).Log("msg", "Could not parse end time", "err", qEndTimeErr)
os.Exit(1)
}
} else {
qEndTime = time.Now()
}
var qStartTime time.Time
var qStartTimeErr error
if *qStart != "" {
qStartTime, qStartTimeErr = time.Parse(layout, *qStart)
if qStartTimeErr != nil {
level.Error(logger).Log("msg", "Could not parse start time", "err", qStartTimeErr)
os.Exit(1)
}
} else {
qStartTime = qEndTime.Add(-*qDur)
}
queryDuration := qEndTime.Sub(qStartTime)
level.Info(logger).Log("msg", "Query duration", "seconds", queryDuration.Seconds())
calcQueryStep := math.Round(queryDuration.Seconds() / float64(*gWidth))
calcQueryStepDur := time.Second * time.Duration(calcQueryStep)
level.Info(logger).Log("msg", "Calculated step", "seconds", calcQueryStepDur.Seconds())
asciigraphOptions := []asciigraph.Option{
asciigraph.Height(*gHeight),
asciigraph.Width(*gWidth),
}
if *gCaption == true {
asciigraphOptions = append(asciigraphOptions, asciigraph.Caption(*promQuery))
}
queryPrometheus(*promQuery, *promServer, qStartTime, qEndTime, calcQueryStepDur, logger, asciigraphOptions...)
}