Browse files

added basic search

  • Loading branch information...
1 parent 6b445b3 commit 98cd3cfe277100f9aede58b2614a48d370afbf2a Ben Woodall committed Apr 4, 2012
Showing with 73 additions and 7 deletions.
  1. +28 −0 lib/rsplunk.rb
  2. +23 −7 lib/rsplunk/auth.rb
  3. +22 −0 lib/rsplunk/search.rb
View
28 lib/rsplunk.rb
@@ -9,5 +9,33 @@ module Rsplunk
VERSION = '0.1.1'
require 'rsplunk/auth'
+ require 'rsplunk/search'
+
+ class Base
+
+ attr_accessor :attrs
+ alias :to_hash :attrs
+
+ def self.lazy_attr_reader(*attrs)
+ attrs.each do |attribute|
+ class_eval do
+ define_method attribute do
+ @attrs[attribute.to_s]
+ end
+ end
+ end
+ end
+
+ def initialize(attrs={})
+ @attrs = attrs.dup
+ end
+
+ def [](method)
+ self.__send__(method.to_sym)
+ rescue NoMethodError
+ nil
+ end
+
+ end
end
View
30 lib/rsplunk/auth.rb
@@ -2,27 +2,43 @@ module Rsplunk
class Auth
- def initialize(host, port)
- @host = host
- @port = port
+ # Set Splunk server. Users 'localhost:8089' if none provided
+ def initialize(host='localhost', port = 8089, user, pass)
+ $host = host
+ $port = port
+ @user = user
+ @pass = pass
+ session_token
end
attr_accessor :host, :port
+ attr_reader :user, :pass
# Initial SSL request
- def splunk_ssl_request(path, data = nil, headers = nil)
- http = Net::HTTP.new(@host, @port)
+ def self.splunk_ssl_post_request(path, data = nil, headers = nil)
+ http = Net::HTTP.new($host, $port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.post(path, data, headers).body
end
+ def self.splunk_ssl_get_request(path, headers = nil)
+ http = Net::HTTP.new($host, $port)
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ http.get(path, headers).body
+ end
+
# Grab token with username and password
- def token(user, pass)
- doc = Hpricot(splunk_ssl_request("/services/auth/login", "username=#{user}&password=#{pass}"))
+ def create_token
+ doc = Hpricot(Rsplunk::Auth.splunk_ssl_post_request("/services/auth/login", "username=#{@user}&password=#{@pass}"))
(doc/"//sessionkey").inner_html
end
+ def session_token
+ $session_token ||= create_token
+ end
+
end
end
View
22 lib/rsplunk/search.rb
@@ -0,0 +1,22 @@
+require 'rsplunk/auth'
+
+module Rsplunk
+
+ class Search
+
+ # Dirty. Creates a list of jobs currently running on Splunk server
+ def list_jobs
+ res = Hpricot(Rsplunk::Auth.splunk_ssl_post_request("/services/search/jobs/",
+ nil,
+ {'authorization' => "Splunk #{$session_token}"}))
+ end
+
+ def create_job(query)
+ end
+
+
+
+
+ end
+
+end

0 comments on commit 98cd3cf

Please sign in to comment.