echo_cli 0.6.6 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -8
- data/lib/echo_cli/cli.rb +105 -12
- data/lib/echo_cli/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6765580be1168de3d0f48cb50be74e722e5c1b4e
|
4
|
+
data.tar.gz: 02910ebe7b7f6c34fc0102b1ce5d539c6479d8b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f493862592be4db91dd6d140dfb0fe10b655bfb331afe1918a6de9ccba6981c26e550a7ec261defd86a0f2db0f581ed82afcbddb89810ef6cb1d2906a03697e3
|
7
|
+
data.tar.gz: 8b38330c4661bba0f71c21c0223f721c49403d56fad567eb2c417dab319d6d1fa3814bb78ebff11cd2b26970b767e4d896c0844d05173a5c9f247c46f46a30ea
|
data/README.md
CHANGED
@@ -6,23 +6,45 @@ TODO: Delete this and the text above, and describe your gem
|
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
Ensure git and rbenv (or rvm) are installed on your machine:
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
Install rbenv:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
|
15
|
+
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
|
16
|
+
~/.rbenv/bin/rbenv init
|
13
17
|
```
|
14
18
|
|
15
|
-
|
19
|
+
See more about rbenv here: https://github.com/rbenv/rbenv
|
16
20
|
|
17
|
-
|
21
|
+
Install 'echo_cli':
|
18
22
|
|
19
|
-
|
23
|
+
```bash
|
24
|
+
gem install echo_cli
|
25
|
+
```
|
20
26
|
|
21
|
-
|
27
|
+
Test the installation to ensure it is working properly (no errors should occur if the installation was successful):
|
28
|
+
|
29
|
+
```bash
|
30
|
+
echo_cli
|
31
|
+
```
|
22
32
|
|
23
33
|
## Usage
|
24
34
|
|
25
|
-
|
35
|
+
View available echo_cli commands and their short descriptions:
|
36
|
+
|
37
|
+
```bash
|
38
|
+
echo_cli
|
39
|
+
```
|
40
|
+
|
41
|
+
View a command's long description and an example of its use:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
echo_cli help [COMMAND]
|
45
|
+
```
|
46
|
+
|
47
|
+
|
26
48
|
|
27
49
|
## Development
|
28
50
|
|
data/lib/echo_cli/cli.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'thor'
|
2
|
+
require 'json'
|
2
3
|
require 'colorize'
|
3
4
|
require 'net/http'
|
4
5
|
require 'net/https'
|
@@ -48,18 +49,18 @@ module Echo_cli
|
|
48
49
|
end
|
49
50
|
|
50
51
|
if options[:q]
|
51
|
-
sleep
|
52
|
+
sleep 15 # ensure data reception and replication
|
52
53
|
end
|
53
54
|
time_end = Time.new.to_i.to_s
|
54
55
|
|
55
|
-
puts "\
|
56
|
+
puts "\nSuccessfully posted ".green + request_body.yellow + " to ".green + ip.blue + " " + num.to_s.green + " time(s)".green + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
|
56
57
|
|
57
58
|
if options[:q]
|
58
59
|
query(ip, request_body[0..request_body.index(":")-1], time_start, time_end)
|
59
60
|
end
|
60
61
|
|
61
62
|
rescue
|
62
|
-
puts "\
|
63
|
+
puts "\nFailed to post ".red + request_body.yellow + " to ".red + ip.blue + " " + num.to_s.red + " time(s)".red
|
63
64
|
end
|
64
65
|
|
65
66
|
else
|
@@ -69,20 +70,20 @@ module Echo_cli
|
|
69
70
|
res = https.request(req)
|
70
71
|
|
71
72
|
if options[:q]
|
72
|
-
sleep
|
73
|
+
sleep 15 # ensure data reception and replication
|
73
74
|
end
|
74
75
|
|
75
76
|
time_end = Time.new.to_i.to_s
|
76
77
|
|
77
78
|
if res.code == '200'
|
78
|
-
puts "\
|
79
|
+
puts "\nSuccessfully posted ".green + request_body.yellow + " to ".green + ip.blue + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
|
79
80
|
|
80
81
|
if options[:q]
|
81
82
|
query(ip, request_body[0..request_body.index(":")-1], time_start, time_end)
|
82
83
|
end
|
83
84
|
end
|
84
85
|
rescue
|
85
|
-
puts "\
|
86
|
+
puts "\nFailed to post ".red + request_body.yellow + " to ".red + ip.blue
|
86
87
|
end
|
87
88
|
end
|
88
89
|
return res.code
|
@@ -165,23 +166,23 @@ module Echo_cli
|
|
165
166
|
begin
|
166
167
|
res = https.request(req)
|
167
168
|
if res.code == '200'
|
168
|
-
puts "\
|
169
|
+
puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + ip.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow + "\nStatus Code: ".green + res.code.yellow + "\nResponse Body: ".green + JSON.pretty_generate(JSON.parse(res.body)).green
|
169
170
|
else
|
170
|
-
puts "\
|
171
|
+
puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + ip.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow + "\nStatus Code: ".red + res.code.yellow + "\nResponse Body: ".red + JSON.pretty_generate(JSON.parse(res.body)).red
|
171
172
|
end
|
172
173
|
rescue
|
173
|
-
puts "\
|
174
|
+
puts "\nYour request returned an error. Are you sure your query was correct?"
|
174
175
|
end
|
175
176
|
else
|
176
177
|
begin
|
177
178
|
res = https.request(req)
|
178
179
|
if res.code == '200'
|
179
|
-
puts "\
|
180
|
+
puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + ip.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow
|
180
181
|
else
|
181
|
-
puts "\
|
182
|
+
puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + ip.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow
|
182
183
|
end
|
183
184
|
rescue
|
184
|
-
puts "\
|
185
|
+
puts "\nYour request returned an error. Are you sure your query was correct?"
|
185
186
|
end
|
186
187
|
end
|
187
188
|
|
@@ -190,5 +191,97 @@ module Echo_cli
|
|
190
191
|
|
191
192
|
return res.code
|
192
193
|
end
|
194
|
+
|
195
|
+
desc "quick_query", "Query metrics on Echo for a given time range beginning at the current time. Use \"echo_cli quick_query <IP> '<metric>' <range>\""
|
196
|
+
|
197
|
+
long_desc <<-LONGDESC
|
198
|
+
`echo_cli quick_query` will query a metric directly from Echo's OpenTSDB server instance for a given time range beginning in the present.
|
199
|
+
|
200
|
+
Ensure single-quotes surround the metric.
|
201
|
+
|
202
|
+
Unlike the echo_cli post command, quick_query does not require the data value nor the data type (e.g. remove ':1|c' from 'example.post:1|c' and your query will still work).
|
203
|
+
|
204
|
+
The <range> parameter is defined in seconds, and is used to query the metric from the current time.
|
205
|
+
|
206
|
+
> $ echo_cli quick_query localhost 'example' 30
|
207
|
+
|
208
|
+
The previous example would find all data points with metric name 'example' on Echo's OpenTSDB instance at 'localhost' in the last 30 seconds.
|
209
|
+
LONGDESC
|
210
|
+
|
211
|
+
def quick_query(ip, metric, range)
|
212
|
+
metric = metric.gsub(/'/,"")
|
213
|
+
if metric.include? ":"
|
214
|
+
metric = metric[0..metric.index(":")-1]
|
215
|
+
end
|
216
|
+
|
217
|
+
opentsdb_uri = "http://" + ip + ":4242/api/query"
|
218
|
+
|
219
|
+
tags=""
|
220
|
+
|
221
|
+
if metric.include? "."
|
222
|
+
tags = metric[metric.index(".")+1..-1] + "."
|
223
|
+
metric = metric[0..metric.index(".")-1]
|
224
|
+
|
225
|
+
toReturn = ""
|
226
|
+
count = 1
|
227
|
+
while tags.include?(".") do
|
228
|
+
toReturn = toReturn + "\"tag" + count.to_s + "\": \"" + tags[0..tags.index(".")-1] + "\""
|
229
|
+
count = count + 1
|
230
|
+
tags = tags[tags.index(".")+1..-1]
|
231
|
+
if tags.include?(".")
|
232
|
+
toReturn = toReturn + ",\n"
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
tags = toReturn
|
237
|
+
end
|
238
|
+
|
239
|
+
time_end = Time.new.to_i
|
240
|
+
time_start = time_end - range.to_i
|
241
|
+
|
242
|
+
time_start = time_start.to_s
|
243
|
+
time_end = time_end.to_s
|
244
|
+
|
245
|
+
opentsdb_query =
|
246
|
+
"{
|
247
|
+
\"start\": " + time_start + ",
|
248
|
+
\"end\": " + time_end + ",
|
249
|
+
\"queries\": [
|
250
|
+
{
|
251
|
+
\"aggregator\": \"sum\",
|
252
|
+
\"metric\": \"" + metric + "\",
|
253
|
+
\"tags\": {" + tags +
|
254
|
+
"}
|
255
|
+
}
|
256
|
+
]
|
257
|
+
}"
|
258
|
+
|
259
|
+
ENV["http_proxy"] = ENV["HTTP_PROXY"]
|
260
|
+
ENV["https_proxy"] = ENV["HTTPS_PROXY"]
|
261
|
+
|
262
|
+
uri = URI.parse(opentsdb_uri)
|
263
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
264
|
+
https.use_ssl = false
|
265
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE if https.use_ssl?
|
266
|
+
req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'}, )
|
267
|
+
req.body = opentsdb_query
|
268
|
+
|
269
|
+
begin
|
270
|
+
res = https.request(req)
|
271
|
+
if res.code == '200'
|
272
|
+
puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + ip.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow + "\nData points in the last ".green + range.yellow + " seconds:\n".green + JSON.pretty_generate(JSON.parse(res.body)).green
|
273
|
+
else
|
274
|
+
puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + ip.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow + "\nStatus Code: ".red + res.code.yellow + "\nResponse Body: ".red + JSON.pretty_generate(JSON.parse(res.body)).red
|
275
|
+
end
|
276
|
+
rescue
|
277
|
+
puts "\nYour request returned an error. Are you sure your query was correct?"
|
278
|
+
end
|
279
|
+
|
280
|
+
ENV["http_proxy"] = ""
|
281
|
+
ENV["https_proxy"] = ""
|
282
|
+
|
283
|
+
return res.code
|
284
|
+
|
285
|
+
end
|
193
286
|
end
|
194
287
|
end
|
data/lib/echo_cli/version.rb
CHANGED