echo_cli 0.6.15 → 0.6.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cce14808a9628b1b03dc63b3ca88680fab12b976
4
- data.tar.gz: fef6fef6c878ede550a748393be7e23fe45c3b2d
3
+ metadata.gz: 8b440b79534c4a019a1f713085751a9ca0548cb4
4
+ data.tar.gz: c4ce497a6fa9735fc79a7b42d884df56bdab1104
5
5
  SHA512:
6
- metadata.gz: fca7aec294eb8116fe4ff9008dfbf576fdbd539f8e4b3196da96046b57ce50aaf594f3b157b01aa197b1d307845d0ea4667a4c70f0429d151efdf4a347b46e17
7
- data.tar.gz: 841bbd583443686031901a383f33aa3f6ac8eab35c8bade43e66bffbfda24c8ebc01f197c3f159f8eca745963339ee3a94d0c4862592632a337e3e2355664774
6
+ metadata.gz: 08c8130f3db46a4b0eea93e1bd69646a47c9f610b54ca967c31f0a7933cc192ba5cb17f90d6b8d1bb98d1e0c9b844b622aa8ef97b4ea5b29ac57b9012dd6e058
7
+ data.tar.gz: 5d3412d36a2cec318d34b2280194a120655e84376687a72868cbca10c75a1f824b5e8157275817fe56b230d8775097bdad59d9e7b2ea4371ba9b2caeadec2db9
data/README.md CHANGED
@@ -55,11 +55,11 @@ $ echo_cli help post
55
55
 
56
56
  #### post
57
57
  ```bash
58
- $ echo_cli post <IP> '<metric>' {number} {-q}
58
+ $ echo_cli post <URL> '<metric>' {number} {-q}
59
59
  ```
60
60
  Parameters:
61
61
 
62
- `<IP>`: (required) specifies the StatsD endpoint you are attempting to post a metric to
62
+ `<URL>`: (required) specifies the StatsD endpoint you are attempting to post a metric to
63
63
 
64
64
  `'<metric>'`: (required) the metric name, as well as any tags, and data value and data type to be posted to StatsD
65
65
 
@@ -71,17 +71,17 @@ Parameters:
71
71
 
72
72
  Example:
73
73
  ```bash
74
- $ echo_cli localhost 'sample.post.here:1|c' 10 -q
74
+ $ echo_cli https://localhost:8125/metrix 'sample.post.here:1|c' 10 -q
75
75
  ```
76
- The previous example would post the metric 'sample' with tags 'post' and 'here' with data value '1' and with data type 'c' (for count). This information would be posted to the instance of StatsD located at 'localhost', and it would be posted 10. Additionally, because of the '-q' specified option, OpenTSDB will be queried immediately after the post to StatsD has been verified.
76
+ The previous example would post the metric 'sample' with tags 'post' and 'here' with data value '1' and with data type 'c' (for count). This information would be posted to the instance of StatsD located at 'https://localhost:8125/metrix', and it would be posted 10. Additionally, because of the '-q' specified option, OpenTSDB will be queried immediately after the post to StatsD has been verified.
77
77
 
78
78
  #### query
79
79
  ```bash
80
- $ echo_cli query <IP> '<metric>' <time_start> <time_end> {-v}
80
+ $ echo_cli query <URL> '<metric>' <time_start> <time_end> {-v}
81
81
  ```
82
82
  Parameters:
83
83
 
84
- `<IP>`: (required) specifies the OpenTSDB endpoint you are attempting to query
84
+ `<URL>`: (required) specifies the OpenTSDB endpoint you are attempting to query
85
85
 
86
86
  `'<metric>'`: (required) the metric name, including any tags, with which the query for OpenTSDB will be constructed
87
87
 
@@ -93,17 +93,17 @@ Parameters:
93
93
 
94
94
  Example:
95
95
  ```bash
96
- $ echo_cli localhost 'sample.query.here' 1492464745 1492465045 -v
96
+ $ echo_cli https://localhost:4242/api/query 'sample.query.here' 1492464745 1492465045 -v
97
97
  ```
98
98
  The previous example will query OpenTSDB with the metric name 'sample' and tags 'query' and 'here' between the times of '1492464745' and '1492465045'. Additionally, the '-v' specified option will also return the response code and response body upon completion.
99
99
 
100
100
  #### quick_query
101
101
  ```bash
102
- $ echo_cli quick_query <IP> '<metric>' <range> {-h}
102
+ $ echo_cli quick_query <URL> '<metric>' <range> {-h}
103
103
  ```
104
104
  Parameters:
105
105
 
106
- `<IP>`: (required) specifies the OpenTSDB endpoint you are attempting to query
106
+ `<URL>`: (required) specifies the OpenTSDB endpoint you are attempting to query
107
107
 
108
108
  `'<metric>'`: (required) the metric name, including any tags, with which the query for OpenTSDB will be constructed
109
109
 
@@ -113,9 +113,9 @@ Parameters:
113
113
 
114
114
  Example:
115
115
  ```bash
116
- $ echo_cli localhost 'sample.query.here' 300
116
+ $ echo_cli https://localhost:4242/api/query 'sample.query.here' 300
117
117
  ```
118
- The previous example will query OpenTSDB located on 'localhost' with the metric name 'sample' and tags 'query' and 'here', within the time range spanning from the current time to '300' seconds before the current time.
118
+ The previous example will query OpenTSDB located on 'https://localhost:4242/api/query' with the metric name 'sample' and tags 'query' and 'here', within the time range spanning from the current time to '300' seconds before the current time.
119
119
 
120
120
  #### convert
121
121
  ```bash
@@ -159,9 +159,9 @@ $ ruby test/echo_cli_test.rb
159
159
 
160
160
  ## Contributing
161
161
 
162
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/echo_cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
162
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/echo_cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org) code of conduct.
163
163
 
164
164
 
165
165
  ## License
166
166
 
167
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
167
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/lib/echo_cli/cli.rb CHANGED
@@ -4,10 +4,12 @@ require 'time'
4
4
  require 'colorize'
5
5
  require 'net/http'
6
6
  require 'net/https'
7
+ require 'uri'
8
+ require 'openssl'
7
9
 
8
10
  module Echo_cli
9
11
  class Echo < Thor
10
- desc "post", "Post metrics to Echo. Use \"echo_cli post <IP> '<metric>' {number} {-q}\""
12
+ desc "post", "Post metrics to Echo. Use \"echo_cli post <URL> '<metric>' {number} {-q}\""
11
13
 
12
14
  option :q
13
15
 
@@ -20,30 +22,33 @@ module Echo_cli
20
22
 
21
23
  With -q (query-verify) option, post will perform a query of previously posted metric in Echo's instance of OpenTSDB, and provide a message verifying the datapoint exists.
22
24
 
23
- > $ echo_cli post localhost 'example.post:1|c' {10} {-q}
25
+ > $ echo_cli post https://localhost:8125/metrix 'example.post:1|c' {10} {-q}
24
26
  LONGDESC
25
27
 
26
- def post(ip, metric, num = 0)
27
- request_body = metric.gsub(/'/,"")
28
- statsd_uri = "http://" + ip + ":8125/metrix"
28
+ def post(statsd_uri, metric, num = 0)
29
29
 
30
30
  ENV["http_proxy"] = ENV["HTTP_PROXY"]
31
31
  ENV["https_proxy"] = ENV["HTTPS_PROXY"]
32
32
 
33
33
  uri = URI.parse(statsd_uri)
34
- https = Net::HTTP.new(uri.host, uri.port)
35
- https.use_ssl = true
36
- https.verify_mode = OpenSSL::SSL::VERIFY_NONE if https.use_ssl?
37
- req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'} )
38
- req.body = request_body
34
+ request = Net::HTTP::Post.new(uri)
35
+ request["Token"] = "mySecretToken"
36
+ request.body = metric.gsub(/'/,"")
37
+
38
+ req_options = {
39
+ use_ssl: uri.scheme == "https",
40
+ verify_mode: OpenSSL::SSL::VERIFY_NONE,
41
+ }
39
42
 
40
43
  if(num.to_i > 0)
41
44
  begin
42
45
  time_start = Time.new.to_i.to_s
43
46
 
44
47
  for i in 1..num.to_i
45
- res = https.request(req)
46
- if res.code != '200'
48
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
49
+ http.request(request)
50
+ end
51
+ if response.code != '200'
47
52
  puts "Final run at: " + i
48
53
  raise
49
54
  end
@@ -54,21 +59,24 @@ module Echo_cli
54
59
  end
55
60
  time_end = Time.new.to_i.to_s
56
61
 
57
- 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
62
+ puts "\nSuccessfully posted ".green + request.body.yellow + " to ".green + statsd_uri.blue + " " + num.to_s.green + " time(s)".green + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
58
63
 
59
64
  if options[:q]
60
- query(ip, request_body[0..request_body.index(":")-1], time_start, time_end)
65
+ opentsdb_uri = statsd_uri[0..statsd_uri.index(':8125')-1] + ":4242/api/query"
66
+ query(opentsdb_uri, request.body[0..request.body.index(":")-1], time_start, time_end)
61
67
  end
62
68
 
63
69
  rescue
64
- puts "\nFailed to post ".red + request_body.yellow + " to ".red + ip.blue + " " + num.to_s.red + " time(s)".red
70
+ puts "\nFailed to post ".red + request.body.yellow + " to ".red + statsd_uri.blue + " " + num.to_s.red + " time(s)".red
65
71
  end
66
72
 
67
73
  else
68
74
  begin
69
75
  time_start = Time.new.to_i.to_s
70
76
 
71
- res = https.request(req)
77
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
78
+ http.request(request)
79
+ end
72
80
 
73
81
  if options[:q]
74
82
  sleep 15 # ensure data reception and replication
@@ -76,24 +84,26 @@ module Echo_cli
76
84
 
77
85
  time_end = Time.new.to_i.to_s
78
86
 
79
- if res.code == '200'
80
- puts "\nSuccessfully posted ".green + request_body.yellow + " to ".green + ip.blue + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
87
+ if response.code == '200'
88
+ puts "\nSuccessfully posted ".green + request.body.yellow + " to ".green + statsd_uri.blue + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow
89
+
81
90
 
82
91
  if options[:q]
83
- query(ip, request_body[0..request_body.index(":")-1], time_start, time_end)
92
+ opentsdb_uri = statsd_uri[0..statsd_uri.index(':8125')-1] + ":4242/api/query"
93
+ query(opentsdb_uri, request.body[0..request.body.index(":")-1], time_start, time_end)
84
94
  end
85
95
  end
86
96
  rescue
87
- puts "\nFailed to post ".red + request_body.yellow + " to ".red + ip.blue
97
+ puts "\nFailed to post ".red + request.body.yellow + " to ".red + statsd_uri.blue
88
98
  end
89
99
  end
90
- return res.code
100
+ return response
91
101
 
92
102
  ENV["http_proxy"] = ""
93
103
  ENV["https_proxy"] = ""
94
104
  end
95
105
 
96
- desc "query", "Query metrics on Echo. Use \"echo_cli query <IP> '<metric>' <time_start> <time_end> {-v}\""
106
+ desc "query", "Query metrics on Echo. Use \"echo_cli query <URL> '<metric>' <time_start> <time_end> {-v}\""
97
107
 
98
108
  option :v
99
109
 
@@ -110,17 +120,15 @@ module Echo_cli
110
120
 
111
121
  With -v (verbose) option, query will print the status code and response body upon completion of the request.
112
122
 
113
- > $ echo_cli query localhost 'example.post' 1492186212 1492186214 {-v}
123
+ > $ echo_cli query https://localhost:4242/api/query 'example.post' 1492186212 1492186214 {-v}
114
124
  LONGDESC
115
125
 
116
- def query(ip, metric, time_start, time_end)
126
+ def query(opentsdb_uri, metric, time_start, time_end)
117
127
  metric = metric.gsub(/'/,"")
118
128
  if metric.include? ":"
119
129
  metric = metric[0..metric.index(":")-1]
120
130
  end
121
131
 
122
- opentsdb_uri = "http://" + ip + ":4242/api/query"
123
-
124
132
  tags=""
125
133
 
126
134
  if metric.include? "."
@@ -169,9 +177,9 @@ module Echo_cli
169
177
  begin
170
178
  res = https.request(req)
171
179
  if res.code == '200'
172
- 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
180
+ puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + opentsdb_uri.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
173
181
  else
174
- 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
182
+ puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + opentsdb_uri.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
175
183
  end
176
184
  rescue
177
185
  puts "\nYour request returned an error. Are you sure your query was correct?"
@@ -180,9 +188,9 @@ module Echo_cli
180
188
  begin
181
189
  res = https.request(req)
182
190
  if res.code == '200'
183
- puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + ip.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow
191
+ puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + opentsdb_uri.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow
184
192
  else
185
- puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + ip.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow
193
+ puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + opentsdb_uri.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow
186
194
  end
187
195
  rescue
188
196
  puts "\nYour request returned an error. Are you sure your query was correct?"
@@ -195,7 +203,7 @@ module Echo_cli
195
203
  return res.code
196
204
  end
197
205
 
198
- desc "quick_query", "Query metrics on Echo from the current time. Use \"echo_cli quick_query <IP> '<metric>' <range> {-e}\""
206
+ desc "quick_query", "Query metrics on Echo from the current time. Use \"echo_cli quick_query <URL> '<metric>' <range> {-e}\""
199
207
 
200
208
  option :e
201
209
 
@@ -210,19 +218,17 @@ module Echo_cli
210
218
 
211
219
  With -e (epoch) option, echo_cli will format the response datapoints such that their timestamps will be in epoch format.
212
220
 
213
- > $ echo_cli quick_query localhost 'example' 30
221
+ > $ echo_cli quick_query https://localhost:4242/api/query 'example' 30
214
222
 
215
223
  The previous example would find all data points with metric name 'example' on Echo's OpenTSDB instance at 'localhost' in the last 30 seconds.
216
224
  LONGDESC
217
225
 
218
- def quick_query(ip, metric, range)
226
+ def quick_query(opentsdb_uri, metric, range)
219
227
  metric = metric.gsub(/'/,"")
220
228
  if metric.include? ":"
221
229
  metric = metric[0..metric.index(":")-1]
222
230
  end
223
231
 
224
- opentsdb_uri = "http://" + ip + ":4242/api/query"
225
-
226
232
  tags=""
227
233
 
228
234
  if metric.include? "."
@@ -273,7 +279,7 @@ module Echo_cli
273
279
  req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'}, )
274
280
  req.body = opentsdb_query
275
281
 
276
- # begin
282
+ begin
277
283
  res = https.request(req)
278
284
  if res.code == '200'
279
285
 
@@ -288,13 +294,13 @@ module Echo_cli
288
294
  end
289
295
  end
290
296
 
291
- 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).green
297
+ puts "\nSuccessfully retrieved ".green + metric.yellow + " from ".green + opentsdb_uri.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).green
292
298
  else
293
- 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
299
+ puts "\nFailed to retrieve ".red + metric.yellow + " from ".red + opentsdb_uri.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
294
300
  end
295
- # rescue
296
- # puts "\nYour request returned an error. Are you sure your query was correct?"
297
- # end
301
+ rescue
302
+ puts "\nYour request returned an error. Are you sure your query was correct?"
303
+ end
298
304
 
299
305
  ENV["http_proxy"] = ""
300
306
  ENV["https_proxy"] = ""
@@ -1,3 +1,3 @@
1
1
  module Echo_cli
2
- VERSION = "0.6.15"
2
+ VERSION = "0.6.16"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: echo_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.15
4
+ version: 0.6.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Raphael