splunker 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/Rakefile +5 -0
- data/VERSION +1 -0
- data/lib/splunker.rb +37 -0
- data/lib/splunker/auth.rb +18 -0
- data/lib/splunker/auth/http_auth.rb +24 -0
- data/lib/splunker/auth/splunk_auth.rb +48 -0
- data/lib/splunker/auth/token_auth.rb +7 -0
- data/lib/splunker/cli.rb +89 -0
- data/lib/splunker/cli/setup.rb +18 -0
- data/lib/splunker/client.rb +27 -0
- data/lib/splunker/configuration.rb +47 -0
- data/lib/splunker/connection.rb +28 -0
- data/lib/splunker/errors.rb +39 -0
- data/lib/splunker/faraday_middleware.rb +15 -0
- data/lib/splunker/models/finders.rb +35 -0
- data/lib/splunker/models/resource.rb +30 -0
- data/lib/splunker/models/search/jobs.rb +12 -0
- data/lib/splunker/models/search/results.rb +6 -0
- data/lib/splunker/models/search/saved.rb +17 -0
- data/lib/splunker/request.rb +38 -0
- data/lib/splunker/version.rb +3 -0
- data/script/console +14 -0
- data/spec/fixtures/search_results_from_job.xml +613 -0
- data/spec/rspec_let_definitions.rb +15 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/unit/auth/http_auth_spec.rb +68 -0
- data/spec/unit/auth/splunk_auth_spec.rb +29 -0
- data/spec/unit/client_spec.rb +21 -0
- data/spec/unit/configuration_spec.rb +23 -0
- data/spec/unit/errors_spec.rb +21 -0
- data/spec/unit/splunker_spec.rb +15 -0
- data/splunker.gemspec +23 -0
- metadata +170 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joshua Murray
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Splunker
|
2
|
+
|
3
|
+
A Ruby client for the [RESTful Splunk API](http://dev.splunk.com/view/rest-api-overview/SP-CAAADP8)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'splunker'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install splunker
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Models are on there way, but you can access resources by directly invoking the
|
22
|
+
HTTP helper methods.
|
23
|
+
|
24
|
+
# Basic Auth
|
25
|
+
c = Splunker.client(:auth_mode => :http_auth, :username => "MYUSERNAME",
|
26
|
+
:password => "MYPASSWORD", :endpoint => "https://splunk.mysite.com")
|
27
|
+
# Returns Nokogiri::XML::Document
|
28
|
+
r = c.get("/servicesNS/joshua/search/saved/searches/MySearch/history")
|
29
|
+
# Process away
|
30
|
+
r.xpath("...")
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/splunker.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require "splunker/version"
|
3
|
+
require "splunker/client"
|
4
|
+
require "splunker/errors"
|
5
|
+
|
6
|
+
module Splunker
|
7
|
+
def self.client(options={})
|
8
|
+
unless options.empty?
|
9
|
+
Thread.current[:splunker_client] = Splunker::Client.new(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
Thread.current[:splunker_client]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.logger
|
16
|
+
if @logger.nil?
|
17
|
+
@logger = Logger.new(STDOUT)
|
18
|
+
@logger.level = Logger::INFO
|
19
|
+
end
|
20
|
+
@logger
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.logger=(custom_logger)
|
24
|
+
@logger = custom_logger
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.method_missing(method,*args,&block)
|
28
|
+
if self.client.respond_to?(method)
|
29
|
+
return self.client.send(method,*args,&block)
|
30
|
+
end
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.respond_to?(method)
|
35
|
+
self.client.respond_to?(method) || super
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'splunker/request'
|
2
|
+
require 'splunker/auth/splunk_auth'
|
3
|
+
Dir[File.dirname(__FILE__) + '/auth/*.rb'].each do |file|
|
4
|
+
next if file.match("splunk_auth")
|
5
|
+
require file
|
6
|
+
end
|
7
|
+
|
8
|
+
module Splunker
|
9
|
+
module Auth
|
10
|
+
def self.create(auth_type, configuration)
|
11
|
+
if (obj = Splunker::Auth.const_get("#{auth_type}".split('_').collect(&:capitalize).join))
|
12
|
+
obj.new(configuration)
|
13
|
+
else
|
14
|
+
raise ArgumentError, "Unknown auth type of #{auth_type}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Splunker
|
2
|
+
module Auth
|
3
|
+
class HttpAuth < SplunkAuth
|
4
|
+
def authenticate_connection(conn)
|
5
|
+
return if self.configured?
|
6
|
+
conn.basic_auth(configuration[:username],
|
7
|
+
configuration[:password])
|
8
|
+
@configured = true
|
9
|
+
end
|
10
|
+
|
11
|
+
def configuration_valid?
|
12
|
+
!configuration[:username].nil? &&
|
13
|
+
!configuration[:password].nil?
|
14
|
+
end
|
15
|
+
|
16
|
+
def verify_configuration!
|
17
|
+
unless configuration_valid?
|
18
|
+
raise Splunker::Errors::ConfigurationError,
|
19
|
+
"HttpAuth requires both :username and :password configuration options be set"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Splunker
|
2
|
+
module Auth
|
3
|
+
class SplunkAuth
|
4
|
+
include Splunker::Request
|
5
|
+
|
6
|
+
attr_accessor :client
|
7
|
+
attr_reader :configured
|
8
|
+
|
9
|
+
# Parameters:
|
10
|
+
# * client => An instance of Splunker::Client
|
11
|
+
def initialize(client)
|
12
|
+
self.client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# A helper method to access the client's configuration.
|
16
|
+
# Returns:
|
17
|
+
# * A Hash of the client's current configuration settings.
|
18
|
+
def configuration
|
19
|
+
self.client.configuration
|
20
|
+
end
|
21
|
+
|
22
|
+
# Child classes must implement their own logic to determine when
|
23
|
+
# @configured is set to true.
|
24
|
+
def configured?
|
25
|
+
@configured == true
|
26
|
+
end
|
27
|
+
|
28
|
+
# Raises Splunker::Errors::ConfigurationError if configuration_valid?
|
29
|
+
# returns false.
|
30
|
+
def verify_configuration!
|
31
|
+
unless configuration_valid?
|
32
|
+
raise Splunker::Errors::ConfigurationError
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# By default, always returns true
|
37
|
+
# Child classes must implement their own logic to determine when
|
38
|
+
# the client's configuration is invalid for its needs.
|
39
|
+
def configuration_valid?
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def reset
|
44
|
+
@configured = false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/splunker/cli.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
if ENV["SPLUNKER_CONSOLE"].nil?
|
4
|
+
require 'splunker'
|
5
|
+
else
|
6
|
+
puts "Enabling console mode for local gem"
|
7
|
+
Bundler.require(:default, "development") if defined?(Bundler)
|
8
|
+
path = File.expand_path(File.dirname(__FILE__) + "/../../lib/")
|
9
|
+
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
|
10
|
+
require path + '/splunker'
|
11
|
+
end
|
12
|
+
|
13
|
+
module Splunker
|
14
|
+
module CLI
|
15
|
+
class ConsoleCLI
|
16
|
+
|
17
|
+
OPTIONS_ENV = {
|
18
|
+
:debug=> "DEBUG",
|
19
|
+
:console => "SPLUNKER_CONSOLE"
|
20
|
+
}
|
21
|
+
|
22
|
+
def self.execute(stdout, arguments=[])
|
23
|
+
options = setup_options(stdout,arguments)
|
24
|
+
|
25
|
+
libs = " -r irb/completion"
|
26
|
+
setup = " -r #{File.dirname(__FILE__) + '/../../lib/splunker/cli/setup.rb'}"
|
27
|
+
|
28
|
+
bundler = (options[:console] ? "bundle exec" : "")
|
29
|
+
cmd = "#{export_env(options)} #{bundler} #{irb} #{libs} #{setup} --simple-prompt"
|
30
|
+
puts "Loading splunker gem..."
|
31
|
+
exec "#{cmd}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.irb()
|
35
|
+
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def self.setup_options(stdout,arguments)
|
41
|
+
env_options = {
|
42
|
+
:console => ENV[OPTIONS_ENV[:console]]
|
43
|
+
}
|
44
|
+
cli_options = {}
|
45
|
+
file_options = {}
|
46
|
+
parser = OptionParser.new do |opts|
|
47
|
+
opts.banner = <<-BANNER.gsub(/^ /,'')
|
48
|
+
#{version}
|
49
|
+
Splunk Client Console
|
50
|
+
|
51
|
+
Proudly copied from Wade Mcewen's CLI code from the spark_api client.
|
52
|
+
|
53
|
+
Usage: #{File.basename($0)} [options]
|
54
|
+
|
55
|
+
Environment Variables: some options (as indicated below), will default to values of keys set in the environment.
|
56
|
+
|
57
|
+
Options are:
|
58
|
+
BANNER
|
59
|
+
opts.separator ""
|
60
|
+
opts.on("-e","--endpoint ENDPOINT",
|
61
|
+
"URI of the API.",
|
62
|
+
"Default: ENV['#{OPTIONS_ENV[:endpoint]}']") { |arg| cli_options[:endpoint] = arg }
|
63
|
+
|
64
|
+
|
65
|
+
opts.on("-v", "--version",
|
66
|
+
"Show client version.") { stdout.puts version; exit }
|
67
|
+
opts.on("-h", "--help",
|
68
|
+
"Show this help message.") { stdout.puts opts; exit }
|
69
|
+
opts.parse!(arguments)
|
70
|
+
|
71
|
+
end
|
72
|
+
options = env_options.merge(file_options.merge(cli_options))
|
73
|
+
return options
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.export_env(options)
|
77
|
+
run_env = ""
|
78
|
+
OPTIONS_ENV.each do |k,v|
|
79
|
+
run_env << " #{v}=\"#{options[k]}\"" unless options[k].nil?
|
80
|
+
end
|
81
|
+
run_env
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.version
|
85
|
+
"Splunker v#{Splunker::VERSION}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'pp'
|
3
|
+
require 'irb/ext/save-history'
|
4
|
+
require 'splunker'
|
5
|
+
|
6
|
+
IRB.conf[:SAVE_HISTORY] = 1000
|
7
|
+
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.splunker-history"
|
8
|
+
|
9
|
+
|
10
|
+
IRB.conf[:AUTO_INDENT]=true
|
11
|
+
IRB.conf[:PROMPT][:SPLUNKER]= {
|
12
|
+
:PROMPT_I => "Splunker:%03n:%i> ",
|
13
|
+
:PROMPT_S => "Splunker:%03n:%i%l ",
|
14
|
+
:PROMPT_C => "Splunker:%03n:%i* ",
|
15
|
+
:RETURN => "%s\n"
|
16
|
+
}
|
17
|
+
|
18
|
+
IRB.conf[:PROMPT_MODE] = :SPLUNKER
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'splunker/configuration'
|
2
|
+
require 'splunker/auth'
|
3
|
+
|
4
|
+
module Splunker
|
5
|
+
class Client
|
6
|
+
include Configuration
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
self.reset
|
10
|
+
|
11
|
+
(Configuration::MUTABLE_IMPLEMENTED_OPTION_KEYS + Configuration::MUTABLE_OPTION_KEYS).each do |key|
|
12
|
+
self.send "#{key}=", options[key] if options.include?(key)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method,*args,&block)
|
17
|
+
if self.request_handler.respond_to?(method)
|
18
|
+
return self.request_handler.send(method,*args,&block)
|
19
|
+
end
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def respond_to?(method)
|
24
|
+
self.request_handler.respond_to?(method) || super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Splunker
|
2
|
+
module Configuration
|
3
|
+
MUTABLE_OPTION_KEYS = [:endpoint, :username, :password, :app, :ssl_verify]
|
4
|
+
# These options are mutable, but their setters are manually implemented
|
5
|
+
MUTABLE_IMPLEMENTED_OPTION_KEYS = [:auth_mode]
|
6
|
+
READONLY_OPTION_KEYS = [:request_handler]
|
7
|
+
|
8
|
+
DEFAULT_ENDPOINT = "https://localhost:8089"
|
9
|
+
DEFAULT_APP_NAME = "search"
|
10
|
+
DEFAULT_SSL_VERIFY = true
|
11
|
+
|
12
|
+
def self.included(base)
|
13
|
+
base.class_eval do
|
14
|
+
attr_accessor *MUTABLE_OPTION_KEYS
|
15
|
+
attr_reader *MUTABLE_IMPLEMENTED_OPTION_KEYS
|
16
|
+
attr_reader *READONLY_OPTION_KEYS
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def configure
|
21
|
+
yield self
|
22
|
+
end
|
23
|
+
|
24
|
+
def configuration
|
25
|
+
MUTABLE_OPTION_KEYS.inject({}) do |conf,key|
|
26
|
+
conf.merge!(key=>self.send(key))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def auth_mode=(mode)
|
31
|
+
@request_handler = if mode.nil?
|
32
|
+
nil
|
33
|
+
else
|
34
|
+
Auth.create(mode, self)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def reset
|
39
|
+
self.endpoint = DEFAULT_ENDPOINT
|
40
|
+
self.app = DEFAULT_APP_NAME
|
41
|
+
self.ssl_verify = DEFAULT_SSL_VERIFY
|
42
|
+
self.auth_mode = nil
|
43
|
+
@username = @password = nil
|
44
|
+
self.request_handler.reset unless self.request_handler.nil?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'splunker/faraday_middleware'
|
3
|
+
|
4
|
+
module Splunker
|
5
|
+
module Connection
|
6
|
+
def reset
|
7
|
+
@connection = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def connection
|
11
|
+
if self.configuration[:endpoint].nil?
|
12
|
+
raise ConfigurationError, "No endpoint set!"
|
13
|
+
end
|
14
|
+
|
15
|
+
opts = {
|
16
|
+
:url => self.configuration[:endpoint]
|
17
|
+
}
|
18
|
+
opts[:ssl] = {:verify => false} if !self.configuration[:ssl_verify]
|
19
|
+
|
20
|
+
@connection ||= Faraday.new(opts) do |c|
|
21
|
+
c.use Faraday::Request::UrlEncoded
|
22
|
+
c.use Faraday::Response::Logger
|
23
|
+
c.use Faraday::Adapter::NetHttp
|
24
|
+
c.use Splunker::FaradayMiddleware
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|