wavefront-cli 2.18.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +4 -5
  5. data/HISTORY.md +20 -1
  6. data/README.md +79 -29
  7. data/lib/wavefront-cli/base.rb +26 -2
  8. data/lib/wavefront-cli/commands/alert.rb +10 -10
  9. data/lib/wavefront-cli/commands/base.rb +15 -2
  10. data/lib/wavefront-cli/commands/cloudintegration.rb +3 -3
  11. data/lib/wavefront-cli/commands/config.rb +2 -1
  12. data/lib/wavefront-cli/commands/dashboard.rb +8 -6
  13. data/lib/wavefront-cli/commands/derivedmetric.rb +5 -5
  14. data/lib/wavefront-cli/commands/event.rb +3 -3
  15. data/lib/wavefront-cli/commands/integration.rb +5 -5
  16. data/lib/wavefront-cli/commands/link.rb +3 -3
  17. data/lib/wavefront-cli/commands/message.rb +2 -2
  18. data/lib/wavefront-cli/commands/metric.rb +1 -1
  19. data/lib/wavefront-cli/commands/notificant.rb +3 -3
  20. data/lib/wavefront-cli/commands/proxy.rb +3 -3
  21. data/lib/wavefront-cli/commands/query.rb +3 -3
  22. data/lib/wavefront-cli/commands/savedsearch.rb +3 -3
  23. data/lib/wavefront-cli/commands/settings.rb +22 -0
  24. data/lib/wavefront-cli/commands/source.rb +3 -3
  25. data/lib/wavefront-cli/commands/user.rb +4 -4
  26. data/lib/wavefront-cli/commands/usergroup.rb +5 -8
  27. data/lib/wavefront-cli/commands/webhook.rb +3 -3
  28. data/lib/wavefront-cli/commands/window.rb +3 -3
  29. data/lib/wavefront-cli/commands/write.rb +6 -4
  30. data/lib/wavefront-cli/config.rb +14 -0
  31. data/lib/wavefront-cli/controller.rb +2 -0
  32. data/lib/wavefront-cli/dashboard.rb +133 -14
  33. data/lib/wavefront-cli/display/base.rb +28 -7
  34. data/lib/wavefront-cli/display/dashboard.rb +32 -8
  35. data/lib/wavefront-cli/display/distribution.rb +4 -1
  36. data/lib/wavefront-cli/display/printer/long.rb +149 -140
  37. data/lib/wavefront-cli/display/printer/terse.rb +19 -42
  38. data/lib/wavefront-cli/display/query.rb +6 -0
  39. data/lib/wavefront-cli/display/settings.rb +21 -0
  40. data/lib/wavefront-cli/display/write.rb +12 -5
  41. data/lib/wavefront-cli/event.rb +1 -1
  42. data/lib/wavefront-cli/exception.rb +1 -0
  43. data/lib/wavefront-cli/settings.rb +37 -0
  44. data/lib/wavefront-cli/stdlib/array.rb +20 -0
  45. data/lib/wavefront-cli/stdlib/string.rb +8 -0
  46. data/lib/wavefront-cli/user.rb +1 -1
  47. data/lib/wavefront-cli/version.rb +1 -1
  48. data/lib/wavefront-cli/write.rb +310 -10
  49. data/spec/.rubocop.yml +3 -0
  50. data/spec/spec_helper.rb +81 -1
  51. data/spec/wavefront-cli/alert_spec.rb +1 -0
  52. data/spec/wavefront-cli/cloudintegration_spec.rb +1 -0
  53. data/spec/wavefront-cli/dashboard_spec.rb +47 -4
  54. data/spec/wavefront-cli/derivedmetric_spec.rb +1 -0
  55. data/spec/wavefront-cli/display/base_spec.rb +16 -9
  56. data/spec/wavefront-cli/display/printer/long_spec.rb +75 -106
  57. data/spec/wavefront-cli/display/printer/terse_spec.rb +33 -21
  58. data/spec/wavefront-cli/event_spec.rb +1 -0
  59. data/spec/wavefront-cli/externallink_spec.rb +1 -0
  60. data/spec/wavefront-cli/integration_spec.rb +1 -0
  61. data/spec/wavefront-cli/maintenancewindow_spec.rb +1 -0
  62. data/spec/wavefront-cli/proxy_spec.rb +2 -0
  63. data/spec/wavefront-cli/query_spec.rb +9 -0
  64. data/spec/wavefront-cli/resources/display/alert-human-long +24 -0
  65. data/spec/wavefront-cli/resources/display/alert-input.json +37 -0
  66. data/spec/wavefront-cli/resources/display/alerts-human-terse +9 -0
  67. data/spec/wavefront-cli/resources/display/alerts-input.json +1 -0
  68. data/spec/wavefront-cli/resources/display/user-human-long +19 -0
  69. data/spec/wavefront-cli/resources/display/user-human-long-no_sep +18 -0
  70. data/spec/wavefront-cli/resources/display/user-input.json +1 -0
  71. data/spec/wavefront-cli/resources/responses/README.md +2 -0
  72. data/spec/wavefront-cli/resources/responses/alert-list.json +1 -0
  73. data/spec/wavefront-cli/resources/responses/backups.json +1 -0
  74. data/spec/wavefront-cli/resources/responses/cloudintegration-list.json +1 -0
  75. data/spec/wavefront-cli/resources/responses/dashboard-list.json +1 -0
  76. data/spec/wavefront-cli/resources/responses/derivedmetric-list.json +1 -0
  77. data/spec/wavefront-cli/resources/responses/event-list.json +1 -0
  78. data/spec/wavefront-cli/resources/responses/integration-list.json +1 -0
  79. data/spec/wavefront-cli/resources/responses/link-list.json +1 -0
  80. data/spec/wavefront-cli/resources/responses/notificant-list.json +1 -0
  81. data/spec/wavefront-cli/resources/responses/proxy-list.json +1 -0
  82. data/spec/wavefront-cli/resources/responses/query-cpu.json +1 -0
  83. data/spec/wavefront-cli/resources/responses/savedsearch-list.json +1 -0
  84. data/spec/wavefront-cli/resources/responses/user-list.json +1 -0
  85. data/spec/wavefront-cli/resources/responses/usergroup-list.json +1 -0
  86. data/spec/wavefront-cli/resources/responses/webhook-list.json +1 -0
  87. data/spec/wavefront-cli/resources/responses/window-list.json +1 -0
  88. data/spec/wavefront-cli/savedsearch_spec.rb +1 -0
  89. data/spec/wavefront-cli/settings_spec.rb +19 -0
  90. data/spec/wavefront-cli/stdlib/array_spec.rb +20 -0
  91. data/spec/wavefront-cli/stdlib/string_spec.rb +13 -1
  92. data/spec/wavefront-cli/user_spec.rb +1 -0
  93. data/spec/wavefront-cli/usergroup_spec.rb +1 -0
  94. data/spec/wavefront-cli/webhook_spec.rb +1 -0
  95. data/spec/wavefront-cli/write_spec.rb +167 -0
  96. data/wavefront-cli.gemspec +3 -4
  97. metadata +65 -31
  98. data/.gitlab-ci.yml +0 -16
  99. data/lib/wavefront-cli/base_write.rb +0 -298
  100. data/lib/wavefront-cli/commands/report.rb +0 -37
  101. data/lib/wavefront-cli/display/printer/base.rb +0 -24
  102. data/lib/wavefront-cli/display/report.rb +0 -17
  103. data/lib/wavefront-cli/report.rb +0 -18
  104. data/spec/wavefront-cli/display/printer/base_spec.rb +0 -20
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.description = 'CLI for Wavefront (wavefront.com) API v2 '
13
13
 
14
14
  gem.authors = ['Robert Fisher']
15
- gem.email = 'slackboy@gmail.com'
15
+ gem.email = 'services@id264.net'
16
16
  gem.homepage = 'https://github.com/snltd/wavefront-cli'
17
17
  gem.license = 'BSD-2-Clause'
18
18
 
@@ -24,9 +24,8 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.add_runtime_dependency 'docopt', '~> 0.6.0'
26
26
  gem.add_runtime_dependency 'inifile', '~> 3.0'
27
- gem.add_runtime_dependency 'wavefront-sdk', '~> 2.5', '>= 2.5.0'
27
+ gem.add_runtime_dependency 'wavefront-sdk', '~> 3.0', '>= 3.0.0'
28
28
 
29
- gem.add_development_dependency 'bundler', '~> 1.3'
30
29
  gem.add_development_dependency 'minitest', '~> 5.11', '>= 5.11.0'
31
30
  gem.add_development_dependency 'rake', '~> 12.0'
32
31
  gem.add_development_dependency 'rubocop', '~> 0.54.0'
@@ -34,5 +33,5 @@ Gem::Specification.new do |gem|
34
33
  gem.add_development_dependency 'webmock', '~> 3.0'
35
34
  gem.add_development_dependency 'yard', '~> 0.9.5'
36
35
 
37
- gem.required_ruby_version = Gem::Requirement.new('>= 2.2.0')
36
+ gem.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
38
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavefront-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.18.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Fisher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-23 00:00:00.000000000 Z
11
+ date: 2019-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt
@@ -44,34 +44,20 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 2.5.0
47
+ version: 3.0.0
48
48
  - - "~>"
49
49
  - !ruby/object:Gem::Version
50
- version: '2.5'
50
+ version: '3.0'
51
51
  type: :runtime
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - ">="
56
56
  - !ruby/object:Gem::Version
57
- version: 2.5.0
57
+ version: 3.0.0
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '2.5'
61
- - !ruby/object:Gem::Dependency
62
- name: bundler
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '1.3'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '1.3'
60
+ version: '3.0'
75
61
  - !ruby/object:Gem::Dependency
76
62
  name: minitest
77
63
  requirement: !ruby/object:Gem::Requirement
@@ -163,7 +149,7 @@ dependencies:
163
149
  - !ruby/object:Gem::Version
164
150
  version: 0.9.5
165
151
  description: 'CLI for Wavefront (wavefront.com) API v2 '
166
- email: slackboy@gmail.com
152
+ email: services@id264.net
167
153
  executables:
168
154
  - wf
169
155
  extensions: []
@@ -171,7 +157,6 @@ extra_rdoc_files: []
171
157
  files:
172
158
  - ".codeclimate.yml"
173
159
  - ".gitignore"
174
- - ".gitlab-ci.yml"
175
160
  - ".rubocop.yml"
176
161
  - ".travis.yml"
177
162
  - Gemfile
@@ -182,7 +167,6 @@ files:
182
167
  - bin/wf
183
168
  - lib/wavefront-cli/alert.rb
184
169
  - lib/wavefront-cli/base.rb
185
- - lib/wavefront-cli/base_write.rb
186
170
  - lib/wavefront-cli/cloudintegration.rb
187
171
  - lib/wavefront-cli/commands/.rubocop.yml
188
172
  - lib/wavefront-cli/commands/alert.rb
@@ -199,8 +183,8 @@ files:
199
183
  - lib/wavefront-cli/commands/notificant.rb
200
184
  - lib/wavefront-cli/commands/proxy.rb
201
185
  - lib/wavefront-cli/commands/query.rb
202
- - lib/wavefront-cli/commands/report.rb
203
186
  - lib/wavefront-cli/commands/savedsearch.rb
187
+ - lib/wavefront-cli/commands/settings.rb
204
188
  - lib/wavefront-cli/commands/source.rb
205
189
  - lib/wavefront-cli/commands/user.rb
206
190
  - lib/wavefront-cli/commands/usergroup.rb
@@ -225,14 +209,13 @@ files:
225
209
  - lib/wavefront-cli/display/message.rb
226
210
  - lib/wavefront-cli/display/metric.rb
227
211
  - lib/wavefront-cli/display/notificant.rb
228
- - lib/wavefront-cli/display/printer/base.rb
229
212
  - lib/wavefront-cli/display/printer/long.rb
230
213
  - lib/wavefront-cli/display/printer/sparkline.rb
231
214
  - lib/wavefront-cli/display/printer/terse.rb
232
215
  - lib/wavefront-cli/display/proxy.rb
233
216
  - lib/wavefront-cli/display/query.rb
234
- - lib/wavefront-cli/display/report.rb
235
217
  - lib/wavefront-cli/display/savedsearch.rb
218
+ - lib/wavefront-cli/display/settings.rb
236
219
  - lib/wavefront-cli/display/source.rb
237
220
  - lib/wavefront-cli/display/user.rb
238
221
  - lib/wavefront-cli/display/usergroup.rb
@@ -265,9 +248,10 @@ files:
265
248
  - lib/wavefront-cli/output/yaml.rb
266
249
  - lib/wavefront-cli/proxy.rb
267
250
  - lib/wavefront-cli/query.rb
268
- - lib/wavefront-cli/report.rb
269
251
  - lib/wavefront-cli/savedsearch.rb
252
+ - lib/wavefront-cli/settings.rb
270
253
  - lib/wavefront-cli/source.rb
254
+ - lib/wavefront-cli/stdlib/array.rb
271
255
  - lib/wavefront-cli/stdlib/string.rb
272
256
  - lib/wavefront-cli/user.rb
273
257
  - lib/wavefront-cli/usergroup.rb
@@ -300,7 +284,6 @@ files:
300
284
  - spec/wavefront-cli/dashboard_spec.rb
301
285
  - spec/wavefront-cli/derivedmetric_spec.rb
302
286
  - spec/wavefront-cli/display/base_spec.rb
303
- - spec/wavefront-cli/display/printer/base_spec.rb
304
287
  - spec/wavefront-cli/display/printer/long_spec.rb
305
288
  - spec/wavefront-cli/display/printer/terse_spec.rb
306
289
  - spec/wavefront-cli/display/spec_helper.rb
@@ -321,11 +304,37 @@ files:
321
304
  - spec/wavefront-cli/output/yaml_spec.rb
322
305
  - spec/wavefront-cli/proxy_spec.rb
323
306
  - spec/wavefront-cli/query_spec.rb
307
+ - spec/wavefront-cli/resources/display/alert-human-long
308
+ - spec/wavefront-cli/resources/display/alert-input.json
309
+ - spec/wavefront-cli/resources/display/alerts-human-terse
310
+ - spec/wavefront-cli/resources/display/alerts-input.json
311
+ - spec/wavefront-cli/resources/display/user-human-long
312
+ - spec/wavefront-cli/resources/display/user-human-long-no_sep
313
+ - spec/wavefront-cli/resources/display/user-input.json
314
+ - spec/wavefront-cli/resources/responses/README.md
315
+ - spec/wavefront-cli/resources/responses/alert-list.json
316
+ - spec/wavefront-cli/resources/responses/backups.json
317
+ - spec/wavefront-cli/resources/responses/cloudintegration-list.json
318
+ - spec/wavefront-cli/resources/responses/dashboard-list.json
319
+ - spec/wavefront-cli/resources/responses/derivedmetric-list.json
320
+ - spec/wavefront-cli/resources/responses/event-list.json
321
+ - spec/wavefront-cli/resources/responses/integration-list.json
322
+ - spec/wavefront-cli/resources/responses/link-list.json
323
+ - spec/wavefront-cli/resources/responses/notificant-list.json
324
+ - spec/wavefront-cli/resources/responses/proxy-list.json
325
+ - spec/wavefront-cli/resources/responses/query-cpu.json
326
+ - spec/wavefront-cli/resources/responses/savedsearch-list.json
327
+ - spec/wavefront-cli/resources/responses/user-list.json
328
+ - spec/wavefront-cli/resources/responses/usergroup-list.json
329
+ - spec/wavefront-cli/resources/responses/webhook-list.json
330
+ - spec/wavefront-cli/resources/responses/window-list.json
324
331
  - spec/wavefront-cli/resources/sample_query_response.json
325
332
  - spec/wavefront-cli/resources/sample_raw_query_response.json
326
333
  - spec/wavefront-cli/resources/wavefront.conf
327
334
  - spec/wavefront-cli/savedsearch_spec.rb
335
+ - spec/wavefront-cli/settings_spec.rb
328
336
  - spec/wavefront-cli/source_spec.rb
337
+ - spec/wavefront-cli/stdlib/array_spec.rb
329
338
  - spec/wavefront-cli/stdlib/string_spec.rb
330
339
  - spec/wavefront-cli/user_spec.rb
331
340
  - spec/wavefront-cli/usergroup_spec.rb
@@ -344,14 +353,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
344
353
  requirements:
345
354
  - - ">="
346
355
  - !ruby/object:Gem::Version
347
- version: 2.2.0
356
+ version: 2.3.0
348
357
  required_rubygems_version: !ruby/object:Gem::Requirement
349
358
  requirements:
350
359
  - - ">="
351
360
  - !ruby/object:Gem::Version
352
361
  version: '0'
353
362
  requirements: []
354
- rubygems_version: 3.0.2
363
+ rubygems_version: 3.0.3
355
364
  signing_key:
356
365
  specification_version: 4
357
366
  summary: CLI for Wavefront API v2
@@ -382,7 +391,6 @@ test_files:
382
391
  - spec/wavefront-cli/dashboard_spec.rb
383
392
  - spec/wavefront-cli/derivedmetric_spec.rb
384
393
  - spec/wavefront-cli/display/base_spec.rb
385
- - spec/wavefront-cli/display/printer/base_spec.rb
386
394
  - spec/wavefront-cli/display/printer/long_spec.rb
387
395
  - spec/wavefront-cli/display/printer/terse_spec.rb
388
396
  - spec/wavefront-cli/display/spec_helper.rb
@@ -403,11 +411,37 @@ test_files:
403
411
  - spec/wavefront-cli/output/yaml_spec.rb
404
412
  - spec/wavefront-cli/proxy_spec.rb
405
413
  - spec/wavefront-cli/query_spec.rb
414
+ - spec/wavefront-cli/resources/display/alert-human-long
415
+ - spec/wavefront-cli/resources/display/alert-input.json
416
+ - spec/wavefront-cli/resources/display/alerts-human-terse
417
+ - spec/wavefront-cli/resources/display/alerts-input.json
418
+ - spec/wavefront-cli/resources/display/user-human-long
419
+ - spec/wavefront-cli/resources/display/user-human-long-no_sep
420
+ - spec/wavefront-cli/resources/display/user-input.json
421
+ - spec/wavefront-cli/resources/responses/README.md
422
+ - spec/wavefront-cli/resources/responses/alert-list.json
423
+ - spec/wavefront-cli/resources/responses/backups.json
424
+ - spec/wavefront-cli/resources/responses/cloudintegration-list.json
425
+ - spec/wavefront-cli/resources/responses/dashboard-list.json
426
+ - spec/wavefront-cli/resources/responses/derivedmetric-list.json
427
+ - spec/wavefront-cli/resources/responses/event-list.json
428
+ - spec/wavefront-cli/resources/responses/integration-list.json
429
+ - spec/wavefront-cli/resources/responses/link-list.json
430
+ - spec/wavefront-cli/resources/responses/notificant-list.json
431
+ - spec/wavefront-cli/resources/responses/proxy-list.json
432
+ - spec/wavefront-cli/resources/responses/query-cpu.json
433
+ - spec/wavefront-cli/resources/responses/savedsearch-list.json
434
+ - spec/wavefront-cli/resources/responses/user-list.json
435
+ - spec/wavefront-cli/resources/responses/usergroup-list.json
436
+ - spec/wavefront-cli/resources/responses/webhook-list.json
437
+ - spec/wavefront-cli/resources/responses/window-list.json
406
438
  - spec/wavefront-cli/resources/sample_query_response.json
407
439
  - spec/wavefront-cli/resources/sample_raw_query_response.json
408
440
  - spec/wavefront-cli/resources/wavefront.conf
409
441
  - spec/wavefront-cli/savedsearch_spec.rb
442
+ - spec/wavefront-cli/settings_spec.rb
410
443
  - spec/wavefront-cli/source_spec.rb
444
+ - spec/wavefront-cli/stdlib/array_spec.rb
411
445
  - spec/wavefront-cli/stdlib/string_spec.rb
412
446
  - spec/wavefront-cli/user_spec.rb
413
447
  - spec/wavefront-cli/usergroup_spec.rb
data/.gitlab-ci.yml DELETED
@@ -1,16 +0,0 @@
1
- dependency_scanning:
2
- image: docker:stable
3
- variables:
4
- DOCKER_DRIVER: overlay2
5
- allow_failure: true
6
- services:
7
- - docker:stable-dind
8
- script:
9
- - export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
10
- - docker run
11
- --env DEP_SCAN_DISABLE_REMOTE_CHECKS="${DEP_SCAN_DISABLE_REMOTE_CHECKS:-false}"
12
- --volume "$PWD:/code"
13
- --volume /var/run/docker.sock:/var/run/docker.sock
14
- "registry.gitlab.com/gitlab-org/security-products/dependency-scanning:$SP_VERSION" /code
15
- artifacts:
16
- paths: [gl-dependency-scanning-report.json]
@@ -1,298 +0,0 @@
1
- require 'wavefront-sdk/support/mixins'
2
- require_relative 'base'
3
-
4
- module WavefrontCli
5
- #
6
- # Send points to a proxy.
7
- #
8
- class BaseWrite < Base
9
- attr_reader :fmt
10
- include Wavefront::Mixins
11
-
12
- # rubocop:disable Metrics/AbcSize
13
- def do_point
14
- p = { path: options[:'<metric>'],
15
- value: options[:'<value>'].delete('\\').to_f,
16
- tags: tags_to_hash(options[:tag]) }
17
-
18
- p[:source] = options[:host] if options[:host]
19
- p[:ts] = parse_time(options[:time]) if options[:time]
20
- send_point(p)
21
- end
22
- # rubocop:enable Metrics/AbcSize
23
-
24
- def send_point(point)
25
- call_write(point)
26
- rescue Wavefront::Exception::InvalidEndpoint
27
- abort format("Could not connect to proxy '%s:%s'.",
28
- options[:proxy], options[:port])
29
- end
30
-
31
- def do_file
32
- valid_format?(options[:infileformat])
33
- setup_fmt(options[:infileformat] || 'tmv')
34
- process_input(options[:'<file>'])
35
- end
36
-
37
- # Read the input, from a file or from STDIN, and turn each line
38
- # into Wavefront points.
39
- #
40
- def process_input(file)
41
- if file == '-'
42
- read_stdin
43
- else
44
- data = process_input_file(load_data(Pathname.new(file)).split("\n"))
45
- call_write(data)
46
- end
47
- end
48
-
49
- def process_input_file(data)
50
- data.each_with_object([]) do |l, a|
51
- begin
52
- a.<< process_line(l)
53
- rescue WavefrontCli::Exception::UnparseableInput => e
54
- puts "Bad input. #{e.message}."
55
- next
56
- end
57
- end
58
- end
59
-
60
- # A wrapper which lets us send normal points, deltas, or
61
- # distributions
62
- #
63
- def call_write(data, openclose = true)
64
- if options[:delta]
65
- wf.write_delta(data, openclose)
66
- else
67
- wf.write(data, openclose)
68
- end
69
- end
70
-
71
- # Read from standard in and stream points through an open
72
- # socket. If the user hits ctrl-c, close the socket and exit
73
- # politely.
74
- #
75
- def read_stdin
76
- open_connection
77
- STDIN.each_line { |l| call_write(process_line(l.strip), false) }
78
- close_connection
79
- rescue SystemExit, Interrupt
80
- puts 'ctrl-c. Exiting.'
81
- wf.close
82
- exit 0
83
- end
84
-
85
- # Find and return the value in a chunked line of input
86
- #
87
- # param chunks [Array] a chunked line of input from #process_line
88
- # return [Float] the value
89
- # raise TypeError if field does not exist
90
- # raise Wavefront::Exception::InvalidValue if it's not a value
91
- #
92
- def extract_value(chunks)
93
- if fmt.include?('v')
94
- v = chunks[fmt.index('v')]
95
- v.to_f
96
- else
97
- raw = chunks[fmt.index('d')].split(',')
98
- xpanded = expand_dist(raw)
99
- wf.mk_distribution(xpanded)
100
- end
101
- end
102
-
103
- # We will let users write a distribution as '1 1 1' or '3x1' or
104
- # even a mix of the two
105
- #
106
- def expand_dist(dist)
107
- dist.map do |v|
108
- if v.is_a?(String) && v.include?('x')
109
- x, val = v.split('x', 2)
110
- Array.new(x.to_i, val.to_f)
111
- else
112
- v.to_f
113
- end
114
- end.flatten
115
- end
116
-
117
- # Find and return the source in a chunked line of input.
118
- #
119
- # @param chunks [Array] a chunked line of input from #process_line
120
- # @return [Float] the timestamp, if it is there, or the current
121
- # UTC time if it is not.
122
- #
123
- def extract_ts(chunks)
124
- ts = chunks[fmt.index('t')]
125
- return parse_time(ts) if valid_timestamp?(ts)
126
- rescue TypeError
127
- Time.now.utc.to_i
128
- end
129
-
130
- def extract_tags(chunks)
131
- tags_to_hash(chunks.last.split(/\s(?=(?:[^"]|"[^"]*")*$)/))
132
- end
133
-
134
- # Find and return the metric path in a chunked line of input.
135
- # The path can be in the data, or passed as an option, or both.
136
- # If the latter, then we assume the option is a prefix, and
137
- # concatenate the value in the data.
138
- #
139
- # param chunks [Array] a chunked line of input from #process_line
140
- # return [String] the metric path
141
- # raise TypeError if field does not exist
142
- #
143
- def extract_path(chunks)
144
- m = chunks[fmt.index('m')]
145
- options[:metric] ? [options[:metric], m].join('.') : m
146
- rescue TypeError
147
- return options[:metric] if options[:metric]
148
- raise
149
- end
150
-
151
- # Find and return the source in a chunked line of input.
152
- #
153
- # param chunks [Array] a chunked line of input from #process_line
154
- # return [String] the source, if it is there, or if not, the
155
- # value passed through by -H, or the local hostname.
156
- #
157
- def extract_source(chunks)
158
- chunks[fmt.index('s')]
159
- rescue TypeError
160
- options[:source] || Socket.gethostname
161
- end
162
-
163
- # Process a line of input, as described by the format string
164
- # held in @fmt. Produces a hash suitable for the SDK to send on.
165
- #
166
- # We let the user define most of the fields, but anything beyond
167
- # what they define is always assumed to be point tags. This is
168
- # because you can have arbitrarily many of those for each point.
169
- #
170
- # @raise WavefrontCli::Exception::UnparseableInput if the line
171
- # doesn't look right
172
- #
173
- # rubocop:disable Metrics/AbcSize
174
- # rubocop:disable Metrics/MethodLength
175
- # rubocop:disable Metrics/CyclomaticComplexity
176
- def process_line(line)
177
- return true if line.empty?
178
- chunks = line.split(/\s+/, fmt.length)
179
- enough_fields?(line) # can raise exception
180
-
181
- begin
182
- point = { path: extract_path(chunks),
183
- tags: line_tags(chunks),
184
- value: extract_value(chunks) }
185
-
186
- point[:ts] = extract_ts(chunks) if fmt.include?('t')
187
- point[:source] = extract_source(chunks) if fmt.include?('s')
188
- point[:interval] = options[:interval] || 'm' if fmt.include?('d')
189
- rescue TypeError
190
- raise(WavefrontCli::Exception::UnparseableInput,
191
- "could not process #{line}")
192
- end
193
-
194
- point
195
- end
196
- # rubocop:enable Metrics/CyclomaticComplexity
197
- # rubocop:enable Metrics/MethodLength
198
- # rubocop:enable Metrics/AbcSize
199
-
200
- # We can get tags from the file, from the -T option, or both.
201
- # Merge them, making the -T win if there is a collision.
202
- #
203
- def line_tags(chunks)
204
- file_tags = fmt.last == 'T' ? extract_tags(chunks) : {}
205
- opt_tags = tags_to_hash(options[:tag])
206
- file_tags.merge(opt_tags)
207
- end
208
-
209
- # Takes an array of key=value tags (as produced by docopt) and
210
- # turns it into a hash of key: value tags. Anything not of the
211
- # form key=val is dropped. If key or value are quoted, we
212
- # remove the quotes.
213
- #
214
- # @param tags [Array]
215
- # return Hash
216
- #
217
- def tags_to_hash(tags)
218
- return nil unless tags
219
-
220
- [tags].flatten.each_with_object({}) do |t, ret|
221
- k, v = t.split('=', 2)
222
- k.gsub!(/^["']|["']$/, '')
223
- ret[k] = v.to_s.gsub(/^["']|["']$/, '') if v
224
- end
225
- end
226
-
227
- # The format string must contain values. They can be single
228
- # values or distributions. So we must have 'v' xor 'd'. It must
229
- # not contain anything other than 'm', 't', 'T', 's', 'd', or
230
- # 'v', and the 'T', if there, must be at the end. No letter must
231
- # appear more than once.
232
- #
233
- # @param fmt [String] format of input file
234
- #
235
- # rubocop:disable Metrics/PerceivedComplexity
236
- # rubocop:disable Metrics/CyclomaticComplexity
237
- # rubocop:disable Metrics/AbcSize
238
- def valid_format?(fmt)
239
- err = if fmt.include?('v') && fmt.include?('d')
240
- "'v' and 'd' are mutually exclusive"
241
- elsif !fmt.include?('v') && !fmt.include?('d')
242
- "format string must include 'v' or 'd'"
243
- elsif !fmt.match(/^[dmstTv]+$/)
244
- 'unsupported field in format string'
245
- elsif !fmt == fmt.split('').uniq.join
246
- 'repeated field in format string'
247
- elsif fmt.include?('T') && !fmt.end_with?('T')
248
- "if used, 'T' must come at end of format string"
249
- end
250
-
251
- return true if err.nil?
252
-
253
- raise(WavefrontCli::Exception::UnparseableInput, err)
254
- end
255
- # rubocop:enable Metrics/PerceivedComplexity
256
- # rubocop:enable Metrics/CyclomaticComplexity
257
- # rubocop:enable Metrics/AbcSize
258
-
259
- # Make sure we have the right number of columns, according to
260
- # the format string. We want to take every precaution we can to
261
- # stop users accidentally polluting their metric namespace with
262
- # junk.
263
- #
264
- # If the format string says we are expecting point tags, we
265
- # may have more columns than the length of the format string.
266
- #
267
- def enough_fields?(line)
268
- ncols = line.split.length
269
- return true if fmt.include?('T') && ncols >= fmt.length
270
- return true if ncols == fmt.length
271
- raise(WavefrontCli::Exception::UnparseableInput,
272
- format('Expected %s fields, got %s', fmt.length, ncols))
273
- end
274
-
275
- # Although the SDK does value checking, we'll add another layer
276
- # of input checing here. See if the time looks valid. We'll
277
- # assume anything before 2000/01/01 or after a year from now is
278
- # wrong. Arbitrary, but there has to be a cut-off somewhere.
279
- #
280
- def valid_timestamp?(timestamp)
281
- (timestamp.is_a?(Integer) || timestamp.match(/^\d+$/)) &&
282
- timestamp.to_i > 946_684_800 &&
283
- timestamp.to_i < (Time.now.to_i + 31_557_600)
284
- end
285
-
286
- private
287
-
288
- def setup_fmt(fmt)
289
- @fmt = fmt.split('')
290
- end
291
-
292
- def load_data(file)
293
- IO.read(file)
294
- rescue StandardError
295
- raise WavefrontCli::Exception::FileNotFound
296
- end
297
- end
298
- end