Skip to content

Commit

Permalink
add search-job.rb script
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wang committed Jun 25, 2015
1 parent 2c55977 commit 8f3ac51
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ Sumo Logic Ruby SDK
[![Code Climate](https://codeclimate.com/github/grokify/sumologic-sdk-ruby/badges/gpa.svg)](https://codeclimate.com/github/grokify/sumologic-sdk-ruby)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/grokify/sumologic-sdk-ruby/master/LICENSE.txt)

Ruby interface to the Sumo Logic REST API. The idea is to make it easier to
hit the API in Ruby code. Please add your scripts and programs to the `scripts`
folder.
Ruby interface to the Sumo Logic REST API.

For more info see the [Sumo Logic API Documentation](https://github.com/SumoLogic/sumo-api-doc/wiki)
Please add your scripts and programs to the `scripts` folder.

## Usage

The interface for this SDK is still being built out but follows the structure in the
[Sumo Logic Python SDK](https://github.com/SumoLogic/sumologic-python-sdk).

Exampe scripts are located in the `scripts` director of the GitHub repo.

## More Info

1. [Sumo Logic API Documentation](https://github.com/SumoLogic/sumo-api-doc/wiki)
2. [Sumo Logic Python SDK](https://github.com/SumoLogic/sumologic-python-sdk)
65 changes: 65 additions & 0 deletions scripts/search-job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env ruby

require 'multi_json'
require 'optparse'
require 'sumologic'
require 'pp'
require 'time'

options = {:query => ''}

OptionParser.new do |opts|
opts.banner = "Usage: search-job.rb [options]"

opts.on('-u', '--username ACCESS_ID/USERNAME' , 'Access Id / Username') { |v| options[:access_id] = v }
opts.on('-p', '--password ACCESS_KEY/PASSWORD', 'Access Key / Password') { |v| options[:access_key] = v }
opts.on('-q', '--query QUERY', 'Query') { |v| options[:query] = v ? v : '' }
opts.on('-v', '--verbose BOOL', 'Verbose') { |v| options[:verbose] = v ? true : false }
opts.on('-o', '--output OUTPUT_PATH', 'Output Path') { |v| options[:output_path] = v ? v : '/tmp/sumo_api_output.json' }
options[:query].to_s.gsub!(/[\s\n]+/, ' ')
end.parse!

puts options[:query] if options[:verbose]

sdk = SumoLogic::Client.new(options[:access_id], options[:access_key])

ms_now = (Time.now.to_f * 1000).to_i
ms_30daysago = ms_now - 60*60*24*15*1000

res = sdk.search_job(options[:query], ms_30daysago, ms_now)

pp(res.body) if options[:verbose]

sj = res.body

res = sdk.search_job_status(sj)
status = res.body

delay = 2
puts status['state'] if options[:verbose]

while status['state'] != 'DONE GATHERING RESULTS' do
if status['state'] == 'CANCELLED'
break
end
sleep(delay)
delay *= 2
res = sdk.search_job_status(sj)
status = res.body
pp status if options[:verbose]
end

limit = 200

if status['state'] == 'DONE GATHERING RESULTS'
count = status['recordCount'].to_i
limit = (count < limit) ? count : limit # compensate bad limit check
res = sdk.search_job_records(sj, limit)
pp(res.body)
bodyJson = MultiJson.encode(res.body)
out_file = options[:output_path]
File.open(out_file,'w') do |fh|
fh.puts bodyJson
end
puts "WROTE #{options[:output_path]}" if options[:verbose]
end

0 comments on commit 8f3ac51

Please sign in to comment.