Guest User

Ruby GET call to Splunk API

a guest
Jun 22nd, 2010
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.00 KB | None | 0 0
  1. require 'net/https'
  2. require 'rubygems'
  3. require 'hpricot'
  4.  
  5. class SplunkClient
  6.   HOST = 'localhost'
  7.   PORT = 8089
  8.   USER = 'admin'
  9.   PASSWORD = 'trekbiker'
  10.  
  11.  
  12.   def splunk_ssl_get_request(path, headers = nil)
  13.     http = Net::HTTP.new(HOST, PORT)
  14.     http.use_ssl = true
  15.     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  16.     http.get(path, headers).body
  17.   end
  18.  
  19.   def splunk_ssl_post_request(path, data = nil, headers = nil)
  20.     http = Net::HTTP.new(HOST, PORT)
  21.     http.use_ssl = true
  22.     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  23.     http.post(path, data, headers).body
  24.   end
  25.  
  26.   def session_key
  27.     @session_key ||= load_session_key
  28.   end
  29.  
  30.   def load_session_key
  31.     doc = Hpricot(splunk_ssl_post_request("/services/auth/login", "username=#{USER}&password=#{PASSWORD}"))
  32.     (doc/"//sessionkey").inner_html
  33.   end
  34.  
  35.   def list_users_logged_in
  36.     xml = splunk_ssl_get_request("/services/authentication/httpauth-tokens", {'authorization' => "Splunk #{session_key}"})
  37.     puts xml
  38.   end
  39.  
  40.  
  41. end
Advertisement
Add Comment
Please, Sign In to add comment