lib/splunk_client/splunk_client.rb in splunk-client-0.8.0 vs lib/splunk_client/splunk_client.rb in splunk-client-0.8.1

- old
+ new

@@ -11,12 +11,18 @@ class SplunkClient def initialize(username, password, host, port=8089) @USER=username; @PASS=password; @HOST=host; @PORT=port - - @SESSION_KEY = { 'authorization' => "Splunk #{get_session_key}" } + + sessionKey = get_session_key + + if (sessionKey == "") + raise SplunkSessionError, 'Session key is invalid. Please check your username, password and host' + else + @SESSION_KEY = { 'authorization' => "Splunk #{sessionKey}" } + end end def search(search) create_search(search) end @@ -42,12 +48,12 @@ url = "/services/search/jobs/#{sid}/results?count=#{maxResults}" url += "&output_mode=#{mode}" unless mode.nil? splunk_get_request(url) end - def get_alert_list(user="nobody") - xml = splunk_get_request("/servicesNS/#{user}/search/alerts/fired_alerts") + def get_alert_list(user="nobody", count=30) + xml = splunk_get_request("/servicesNS/#{user}/search/alerts/fired_alerts?count=#{count}") SplunkAlertFeed.new(Nokogiri::Slop(xml), self) end def get_alert(alarmName, user="nobody") xml = splunk_get_request("/servicesNS/#{user}/search/alerts/fired_alerts/#{alarmName}") @@ -69,11 +75,11 @@ http.verify_mode = OpenSSL::SSL::VERIFY_NONE return http end def splunk_get_request(path) - splunk_http_request.get(path, @SESSION_KEY).body + splunk_http_request.get(path, @SESSION_KEY.merge({'Content-Type' => 'application/x-www-form-urlencoded'})).body end def splunk_post_request(path, data=nil, headers=nil) splunk_http_request.post(path,data,headers).body end @@ -84,5 +90,10 @@ @doc = Nokogiri::Slop(xml) return @doc.xpath("//sessionKey").text end end #class SplunkClient + +class SplunkSessionError < SecurityError + # Exception class for handling invalid session tokens received by the gem +end +