This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcli.go
148 lines (131 loc) · 3.36 KB
/
cli.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
// Copyright 2013 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"os"
"strconv"
"time"
)
var (
server = flag.String("server", "", "URL of the Prometheus server to query")
timeout = flag.Duration("timeout", time.Minute, "Timeout to use when querying the Prometheus server")
useCSV = flag.Bool("csv", true, "Whether to format output as CSV")
csvDelim = flag.String("csvDelimiter", ";", "Single-character delimiter to use in CSV output")
)
func die(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr, "")
os.Exit(1)
}
func printQueryResponse(r QueryResponse) {
if *useCSV {
fmt.Print(r.ToCSV(rune((*csvDelim)[0])))
} else {
fmt.Print(r.ToText())
}
}
func query(c *Client) {
if flag.NArg() != 2 {
flag.Usage()
die("Please supply a query expression")
}
resp, err := c.Query(flag.Arg(1))
if err != nil {
die("Error querying server: %s", err)
}
printQueryResponse(resp)
}
func queryRange(c *Client) {
if flag.NArg() != 4 && flag.NArg() != 5 {
flag.Usage()
die("Wrong number of range query arguments")
}
end, err := strconv.ParseFloat(flag.Arg(2), 64)
if err != nil {
flag.Usage()
die("Invalid end timestamp '%s'", flag.Arg(2))
}
rangeSec, err := strconv.ParseUint(flag.Arg(3), 10, 64)
if err != nil {
flag.Usage()
die("Invalid query range '%s'", flag.Arg(3))
}
var step uint64
if flag.NArg() == 5 {
step, err = strconv.ParseUint(flag.Arg(4), 10, 64)
if err != nil {
flag.Usage()
die("Invalid query step '%s'", flag.Arg(4))
}
} else {
step = rangeSec / 250
}
if step < 1 {
step = 1
}
resp, err := c.QueryRange(flag.Arg(1), end, rangeSec, step)
if err != nil {
die("Error querying server: %s", err)
}
printQueryResponse(resp)
}
func metrics(c *Client) {
if flag.NArg() != 1 {
flag.Usage()
die("Too many arguments")
}
if metrics, err := c.Metrics(); err != nil {
die("Error querying server: %s", err)
} else {
for _, m := range metrics {
fmt.Println(m)
}
}
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\t%s [flags] query <expression>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s [flags] query_range <expression> <end_timestamp> <range_seconds> [<step_seconds>]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s [flags] metrics\n", os.Args[0])
fmt.Printf("\nFlags:\n")
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
flag.Parse()
if *server == "" {
flag.Usage()
die("Please provide a server name.")
}
if flag.NArg() < 1 {
flag.Usage()
die("Please provide a command.")
}
if len(*csvDelim) != 1 {
flag.Usage()
die("CSV delimiter may be a single character only")
}
c := NewClient(*server, *timeout)
switch flag.Arg(0) {
case "query":
query(c)
case "query_range":
queryRange(c)
case "metrics":
metrics(c)
default:
die("Unknown command '%s'", flag.Arg(0))
}
}