elasticsearch-xpack 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +62 -0
  7. data/Rakefile +95 -0
  8. data/elasticsearch-xpack.gemspec +39 -0
  9. data/examples/watcher/error_500.rb +163 -0
  10. data/lib/elasticsearch/xpack.rb +32 -0
  11. data/lib/elasticsearch/xpack/api/actions/graph/explore.rb +39 -0
  12. data/lib/elasticsearch/xpack/api/actions/info.rb +30 -0
  13. data/lib/elasticsearch/xpack/api/actions/license/delete.rb +23 -0
  14. data/lib/elasticsearch/xpack/api/actions/license/get.rb +26 -0
  15. data/lib/elasticsearch/xpack/api/actions/license/post.rb +28 -0
  16. data/lib/elasticsearch/xpack/api/actions/monitoring/bulk.rb +45 -0
  17. data/lib/elasticsearch/xpack/api/actions/security/authenticate.rb +24 -0
  18. data/lib/elasticsearch/xpack/api/actions/security/change_password.rb +34 -0
  19. data/lib/elasticsearch/xpack/api/actions/security/clear_cached_realms.rb +33 -0
  20. data/lib/elasticsearch/xpack/api/actions/security/clear_cached_roles.rb +27 -0
  21. data/lib/elasticsearch/xpack/api/actions/security/delete_role.rb +34 -0
  22. data/lib/elasticsearch/xpack/api/actions/security/delete_user.rb +37 -0
  23. data/lib/elasticsearch/xpack/api/actions/security/get_role.rb +29 -0
  24. data/lib/elasticsearch/xpack/api/actions/security/get_user.rb +29 -0
  25. data/lib/elasticsearch/xpack/api/actions/security/put_role.rb +32 -0
  26. data/lib/elasticsearch/xpack/api/actions/security/put_user.rb +34 -0
  27. data/lib/elasticsearch/xpack/api/actions/usage.rb +23 -0
  28. data/lib/elasticsearch/xpack/api/actions/watcher/ack_watch.rb +36 -0
  29. data/lib/elasticsearch/xpack/api/actions/watcher/activate_watch.rb +32 -0
  30. data/lib/elasticsearch/xpack/api/actions/watcher/deactivate_watch.rb +32 -0
  31. data/lib/elasticsearch/xpack/api/actions/watcher/delete_watch.rb +35 -0
  32. data/lib/elasticsearch/xpack/api/actions/watcher/execute_watch.rb +28 -0
  33. data/lib/elasticsearch/xpack/api/actions/watcher/get_watch.rb +27 -0
  34. data/lib/elasticsearch/xpack/api/actions/watcher/put_watch.rb +35 -0
  35. data/lib/elasticsearch/xpack/api/actions/watcher/restart.rb +23 -0
  36. data/lib/elasticsearch/xpack/api/actions/watcher/start.rb +24 -0
  37. data/lib/elasticsearch/xpack/api/actions/watcher/stats.rb +27 -0
  38. data/lib/elasticsearch/xpack/api/actions/watcher/stop.rb +24 -0
  39. data/lib/elasticsearch/xpack/api/namespace/graph.rb +18 -0
  40. data/lib/elasticsearch/xpack/api/namespace/license.rb +18 -0
  41. data/lib/elasticsearch/xpack/api/namespace/monitoring.rb +18 -0
  42. data/lib/elasticsearch/xpack/api/namespace/security.rb +18 -0
  43. data/lib/elasticsearch/xpack/api/namespace/watcher.rb +18 -0
  44. data/lib/elasticsearch/xpack/version.rb +5 -0
  45. metadata +297 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72d019a7e50f9f6e17e5876243f036ab1162a0f8
4
+ data.tar.gz: da5a762394b24d1b99f77c0ad7b4e31916b620c6
5
+ SHA512:
6
+ metadata.gz: 41e5df6ce07833d9a8b979da33e321d2279464f0956a963ec472666a8cee8dab484a30ceb8cafb215084cc51c3b6df9c1bd9682ed3c975db406c37d2e64ed077
7
+ data.tar.gz: f35e4f346658bd0f2f6e803e29eda9449fe1e0be21870d4b6e4591aacecf0ee94ce0bb38955703cc9ac44fe9dd27212dfb0c242b260d0c5dc3291498fa8b5f81
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elasticsearch-xpack.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Karel Minarik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ # Elasticsearch::XPack
2
+
3
+ A Ruby integration for the [X-Pack extensions](https://www.elastic.co/v5)
4
+ for Elasticsearch.
5
+
6
+
7
+ ## Installation
8
+
9
+ Install the package from [Rubygems](https://rubygems.org):
10
+
11
+ gem install elasticsearch-xpack
12
+
13
+ To use an unreleased version, either add it to your `Gemfile` for [Bundler](http://gembundler.com):
14
+
15
+ gem 'elasticsearch-xpack', git: 'git://github.com/elastic/elasticsearch-ruby-xpack.git'
16
+
17
+ or install it from a source code checkout:
18
+
19
+ git clone https://github.com/elasticsearch/elasticsearch-ruby-xpack.git
20
+ bundle install
21
+ rake install
22
+
23
+ ## Usage
24
+
25
+ If you use the official [Ruby client for Elasticsearch](https://github.com/elastic/elasticsearch-ruby),
26
+ require the library in your code, and all the methods will be automatically available in the `xpack` namespace:
27
+
28
+ ```ruby
29
+ require 'elasticsearch'
30
+ require 'elasticsearch/xpack'
31
+
32
+ client = Elasticsearch::Client.new url: 'http://elastic:changeme@localhost:9200'
33
+
34
+ client.xpack.info
35
+ # => {"build"=> ..., "features"=> ...}
36
+ ```
37
+
38
+ The integration is designed as a standalone `Elasticsearch::XPack::API` module, so it's easy
39
+ to mix it into a different client, and the methods will be available in the top namespace.
40
+
41
+ For documentation, look into the RDoc annotations in the source files, which contain links to the
42
+ official [X-Pack for the Elastic Stack](https://www.elastic.co/guide/en/x-pack/current/index.html) documentation.
43
+
44
+ For examples, look into the [`examples`](examples) folder in this repository.
45
+
46
+ ## License
47
+
48
+ This software is licensed under the Apache 2 license, quoted below.
49
+
50
+ Copyright (c) 2016 Elasticsearch <http://www.elasticsearch.org>
51
+
52
+ Licensed under the Apache License, Version 2.0 (the "License");
53
+ you may not use this file except in compliance with the License.
54
+ You may obtain a copy of the License at
55
+
56
+ http://www.apache.org/licenses/LICENSE-2.0
57
+
58
+ Unless required by applicable law or agreed to in writing, software
59
+ distributed under the License is distributed on an "AS IS" BASIS,
60
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61
+ See the License for the specific language governing permissions and
62
+ limitations under the License.
@@ -0,0 +1,95 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ task :default do
6
+ exec "rake --tasks"
7
+ end
8
+
9
+ Rake::TestTask.new('test:unit') do |test|
10
+ test.libs << 'test'
11
+ test.test_files = FileList['test/unit/**/*_test.rb']
12
+ # test.verbose = true
13
+ # test.warning = true
14
+ end
15
+
16
+ namespace :test do
17
+ desc "Run integration tests"
18
+ task :integration do
19
+ require 'ansi'
20
+
21
+ suites = %w[
22
+ x-plugins/elasticsearch/x-pack/src/test/resources/rest-api-spec/test
23
+ x-plugins/elasticsearch/x-pack/graph/src/test/resources/rest-api-spec/test
24
+ x-plugins/elasticsearch/x-pack/monitoring/src/test/resources/rest-api-spec/test/monitoring
25
+ x-plugins/elasticsearch/x-pack/security/src/test/resources/rest-api-spec/test
26
+ x-plugins/elasticsearch/x-pack/watcher/src/test/resources/rest-api-spec/test/xpack/watcher
27
+ x-plugins/elasticsearch/x-pack/license-plugin/src/test/resources/rest-api-spec/test
28
+ ]
29
+
30
+ # TEMPORARY
31
+ disabled_suites = %w[
32
+ ]
33
+
34
+ suites = (suites + disabled_suites).select! { |d| d =~ Regexp.new(ENV['SUITE'].gsub(/,/, '|')) } if ENV['SUITE']
35
+
36
+ executed_suites = []
37
+
38
+ at_exit do
39
+ errors = executed_suites.any? { |d| d.values.first == 1 }
40
+ color = errors ? :red : :green
41
+
42
+ if errors
43
+ puts "----- ".ansi(color) + "ERROR".ansi(color).ansi(:bold) + ('-'*(80-12)).ansi(color)
44
+ else
45
+ puts "----- ".ansi(color) + "SUCCESS".ansi(color).ansi(:bold) + ('-'*(80-13)).ansi(color)
46
+ end
47
+
48
+ executed_suites.each do |d|
49
+ name = d.keys.first.gsub(%r{x-plugins/elasticsearch/x-pack/([^/]+)/.*}, '\1')
50
+ status = d.values.first == 0 ? 'OK'.ansi(:green) : 'KO'.ansi(:red)
51
+ puts "#{status} #{name.ansi(:bold)}"
52
+ end
53
+
54
+ puts ('-'*80).ansi(color)
55
+
56
+ exit( errors ? 1 : 0 )
57
+ end
58
+
59
+ suites.each do |suite|
60
+ begin
61
+ sh <<-COMMAND
62
+ TEST_REST_API_SPEC=../#{suite} bundle exec ruby -I lib:test test/integration/yaml_test_runner.rb
63
+ COMMAND
64
+ executed_suites << { suite => 0 }
65
+ rescue RuntimeError
66
+ executed_suites << { suite => 1 }
67
+ end
68
+
69
+ puts '', '-'*80, ''
70
+ end
71
+ end
72
+ end
73
+
74
+ namespace :elasticsearch do
75
+ desc "Start Elasticsearch node for tests"
76
+ task :start do
77
+ require 'elasticsearch/extensions/test/cluster'
78
+ Elasticsearch::Extensions::Test::Cluster.start(port: ENV.fetch('TEST_CLUSTER_PORT', 9260), nodes: 1, path_logs: '/tmp')
79
+ end
80
+
81
+ desc "Stop Elasticsearch node for tests"
82
+ task :stop do
83
+ require 'elasticsearch/extensions/test/cluster'
84
+ Elasticsearch::Extensions::Test::Cluster.stop(port: ENV.fetch('TEST_CLUSTER_PORT', 9260), nodes: 1)
85
+ end
86
+
87
+ task :status do
88
+ require 'elasticsearch/extensions/test/cluster'
89
+ begin
90
+ Elasticsearch::Extensions::Test::Cluster.__print_cluster_info(ENV.fetch('TEST_CLUSTER_PORT', 9260))
91
+ rescue Errno::ECONNREFUSED
92
+ puts "\e[31m[!] Test cluster not running\e[0m"; exit(1)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elasticsearch/xpack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elasticsearch-xpack"
8
+ spec.version = Elasticsearch::XPack::VERSION
9
+ spec.authors = ["Karel Minarik"]
10
+ spec.email = ["karel@elastic.co"]
11
+
12
+ spec.summary = "Ruby integrations for the X-Pack extensions for Elasticsearch"
13
+ spec.description = "Ruby integrations for the X-Pack extensions for Elasticsearch"
14
+ spec.homepage = "https://github.com/elastic/elasticsearch-xpack-ruby"
15
+ spec.license = "Apache 2"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 11.1"
23
+
24
+ spec.add_dependency "elasticsearch-api"
25
+
26
+ spec.add_development_dependency "elasticsearch"
27
+ spec.add_development_dependency "elasticsearch-transport"
28
+ spec.add_development_dependency "elasticsearch-extensions"
29
+
30
+ spec.add_development_dependency "activesupport"
31
+ spec.add_development_dependency "ansi"
32
+ spec.add_development_dependency "turn"
33
+ spec.add_development_dependency "minitest"
34
+ spec.add_development_dependency "minitest-reporters"
35
+ spec.add_development_dependency "shoulda-context"
36
+ spec.add_development_dependency "mocha"
37
+ spec.add_development_dependency "yard"
38
+ spec.add_development_dependency "pry"
39
+ end
@@ -0,0 +1,163 @@
1
+ # An example of a complex configuration for Elasticsearch Watcher alerting and notification system.
2
+ #
3
+ # Execute this file from the root of the repository:
4
+ #
5
+ # bundle exec ruby -I lib ./examples/watcher/error_500.rb
6
+ #
7
+ # The watch searches for `500` errors in a specific index on a periodic basis, and performs three
8
+ # actions when at least 3 errors have been received in the last 5 minutes:
9
+ #
10
+ # 1. indexes the error documents and aggregations returned from search,
11
+ # 2. sends a notification via e-mail, and
12
+ # 3. sends the data to a HTTP API.
13
+ #
14
+ # If you want to test sending the e-mail, you have to configure Watcher:
15
+ # <https://www.elastic.co/guide/en/x-pack/current/actions-email.html#configuring-email>
16
+ #
17
+ # You can run a simple Sinatra based web server to test the webhook action with this script:
18
+ #
19
+ # $ ruby -r sinatra -r json -e 'post("/") { json = JSON.parse(request.body.read); puts %Q~Received #{json["watch_id"]} with payload: #{json["payload"]}~ }'
20
+ #
21
+
22
+ require 'elasticsearch'
23
+ require 'elasticsearch/xpack'
24
+
25
+ client = Elasticsearch::Client.new url: 'http://elastic:changeme@localhost:9260', log: true
26
+ client.transport.logger.formatter = proc do |severity, datetime, progname, msg| "\e[2m#{msg}\e[0m\n" end
27
+
28
+ # Delete the Watcher and test indices
29
+ #
30
+ ['test_errors', 'alerts', '.watcher-history-*'].each do |index|
31
+ client.indices.delete index: index, ignore: 404
32
+ end
33
+
34
+ # Print information about the Watcher plugin
35
+ #
36
+ puts "X-Pack #{client.xpack.info['build']['hash']}"
37
+
38
+ # Register a new watch
39
+ #
40
+ client.xpack.watcher.put_watch id: 'error_500', body: {
41
+ # Label the watch
42
+ #
43
+ metadata: { tags: ['errors'] },
44
+
45
+ # Run the watch every 10 seconds
46
+ #
47
+ trigger: { schedule: { interval: '10s' } },
48
+
49
+ # Search for at least 3 documents matching the condition
50
+ #
51
+ condition: { compare: { 'ctx.payload.hits.total' => { gt: 3 } } },
52
+
53
+ # Throttle the watch execution for 30 seconds
54
+ #
55
+ throttle_period: '30s',
56
+
57
+ # The search request to execute
58
+ #
59
+ input: {
60
+ search: {
61
+ request: {
62
+ indices: ['test_errors'],
63
+ body: {
64
+ query: {
65
+ bool: {
66
+ must: [
67
+ { match: { status: 500 } } ,
68
+ { range: { timestamp: { from: '{{ctx.trigger.scheduled_time}}||-5m',
69
+ to: '{{ctx.trigger.triggered_time}}' } } }
70
+ ]
71
+ }
72
+ },
73
+ # Return hosts with most errors
74
+ #
75
+ aggregations: {
76
+ hosts: { terms: { field: 'host' } }
77
+ }
78
+ }}}
79
+ },
80
+
81
+ # The actions to perform
82
+ #
83
+ actions: {
84
+ send_email: {
85
+ transform: {
86
+ # Transform the data for the template
87
+ #
88
+ script: {
89
+ lang: 'painless',
90
+ inline: "[ 'total': ctx.payload.hits.total, 'hosts': ctx.payload.aggregations.hosts.buckets.collect(bucket -> [ 'host': bucket.key, 'errors': bucket.doc_count ]), 'errors': ctx.payload.hits.hits.collect(d -> d._source) ]"
91
+ }
92
+ },
93
+ email: { to: 'alerts@example.com',
94
+ subject: '[ALERT] {{ctx.watch_id}}',
95
+ body: <<-BODY.gsub(/^ {28}/, ''),
96
+ Received {{ctx.payload.total}} errors in the last 5 minutes.
97
+
98
+ Hosts:
99
+
100
+ {{#ctx.payload.hosts}}- {{host}} ({{errors}} errors)\n{{/ctx.payload.hosts}}
101
+
102
+ A file with complete data is attached to this message.\n
103
+ BODY
104
+ attachments: { 'data.yml' => { data: { format: 'yaml' } } }
105
+ }
106
+ },
107
+
108
+ index_payload: {
109
+ # Transform the data to be stored
110
+ #
111
+ transform: {
112
+ script: {
113
+ lang: 'painless',
114
+ inline: "[ 'watch_id': ctx.watch_id, 'payload': ctx.payload ]"
115
+ }
116
+ },
117
+ index: { index: 'alerts', doc_type: 'alert' }
118
+ },
119
+
120
+ ping_webhook: {
121
+ webhook: {
122
+ method: 'post',
123
+ url: 'http://localhost:4567',
124
+ body: %q|{"watch_id" : "{{ctx.watch_id}}", "payload" : "{{ctx.payload}}"}| }
125
+ }
126
+ }
127
+ }
128
+
129
+ # Create the index with example documents
130
+ #
131
+ client.indices.create index: 'test_errors', body: {
132
+ mappings: {
133
+ d: {
134
+ properties: {
135
+ host: { type: 'keyword' }
136
+ }
137
+ }
138
+ }
139
+ }
140
+
141
+ # Index documents to trigger the watch
142
+ #
143
+ 10.times do
144
+ client.index index: 'test_errors', type: 'd',
145
+ body: { timestamp: Time.now.utc.iso8601, status: "#{rand(4..5)}00", host: "10.0.0.#{rand(1..3)}" }
146
+ end
147
+
148
+ # Wait a bit...
149
+ #
150
+ print "Waiting 30 seconds..."
151
+ $i=0; while $i < 30 do
152
+ sleep(1); print('.'); $i+=1
153
+ end; puts "\n"
154
+
155
+ # Display information about watch execution
156
+ #
157
+ client.search(index: '.watcher-history-*', q: 'watch_id:error_500', sort: 'trigger_event.triggered_time:asc')['hits']['hits'].each do |r|
158
+ puts "#{r['_source']['watch_id']} #{r['_source']['state'].upcase} at #{r['_source']['result']['execution_time']}"
159
+ end
160
+
161
+ # Delete the watch
162
+ #
163
+ client.xpack.watcher.delete_watch id: 'error_500', master_timeout: '30s', force: true
@@ -0,0 +1,32 @@
1
+ require "elasticsearch/api"
2
+ require "elasticsearch/xpack/version"
3
+
4
+ Dir[ File.expand_path('../xpack/api/actions/**/*.rb', __FILE__) ].each { |f| require f }
5
+ Dir[ File.expand_path('../xpack/api/namespace/**/*.rb', __FILE__) ].each { |f| require f }
6
+
7
+ module Elasticsearch
8
+ module XPack
9
+ module API
10
+ def self.included(base)
11
+ Elasticsearch::XPack::API.constants.reject {|c| c == :Client }.each do |m|
12
+ base.__send__ :include, Elasticsearch::XPack::API.const_get(m)
13
+ end
14
+ end
15
+
16
+ class Client
17
+ include Elasticsearch::API::Common::Client, Elasticsearch::API::Common::Client::Base
18
+ include Elasticsearch::XPack::API
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module Elasticsearch
25
+ module Transport
26
+ class Client
27
+ def xpack
28
+ @xpack_client ||= Elasticsearch::XPack::API::Client.new(self)
29
+ end
30
+ end
31
+ end
32
+ end if defined?(Elasticsearch::Transport::Client)