Last active
August 29, 2015 14:05
-
-
Save zachad/7037725c7ce5cec856f1 to your computer and use it in GitHub Desktop.
Example script using the CloudShark search API call to delete capture files older than a certain number of days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/cloudshark/ruby/bin/ruby | |
# | |
# | |
# EXAMPLE CAPTURE PRUNING SCRIPT | |
# | |
# This is example code which must be run AT YOUR OWN RISK | |
# CloudShark is not responsible for any unintended data loss. | |
# This script is not officially supported by CloudShark or QA Cafe. | |
# | |
require 'date' | |
require 'time' | |
require 'json' | |
require 'open-uri' | |
############################################################################### | |
# | |
# Please fill in the following section with your settings: | |
# | |
################ VARIABLES FOR THE SYSADMIN TO SET ############################ | |
$api_token="<API Token with GET, SEARCH, and DELETE PERMISSIONS" | |
$server="http://<CloudShark URL>/" | |
# The number of days back to start deleting files from. | |
# Please change this to the actual cutoff you prefer. A month=30, etc. | |
delete_files_older_than_this_many_days=365 | |
# Show the warning that you are about to delete a lot of files. | |
# Set this to false when deploying in production to speed up the script. | |
show_warning = true | |
################ END VARIABLES ################################################ | |
def delete_by_capture_id(entry) | |
uri = URI "#{$server}/api/v1/#{$api_token}/delete/#{entry['id']}" | |
puts "Deleting capture #{entry['id']}: #{entry['filename']}" | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
begin | |
request = Net::HTTP::Post.new uri.request_uri | |
response = http.request request | |
res=JSON.parse(response.body) | |
if res['exceptions'] | |
puts "Error from CloudShark server: #{res['exceptions'].first}" | |
exit 1 | |
end | |
rescue Exception=>e | |
puts "There was an error deleting the capture by its id. #{e.message}" | |
exit 1 | |
else | |
puts "Capture session #{entry['id']} deleted." | |
end | |
end | |
end | |
# calculate the date to delete files from before | |
ending_date = (Date.today.to_time - (delete_files_older_than_this_many_days * 86400)).strftime("%m/%d/%Y") | |
puts "Deleting captures uploaded on or before #{ending_date}" | |
params = "search[date]=1/1/1999-#{ending_date}&limit=90" | |
uri = "#{$server}/api/v1/#{$api_token}/search.json?#{params}" | |
puts "Searching for files via API:" | |
puts uri | |
# we need to run this search multiple times | |
# to get each *page* of results. The first time | |
# we will delete from the first page. Then we | |
# run the same query again to see if there are more results. | |
begin | |
loop do | |
response = open(uri).read | |
res = JSON.parse(response) | |
if res['total'].to_i == 0 || res['captures'].nil? | |
puts "There were no captures matching the search parameters." if show_warning | |
exit | |
end | |
# display a warning the first time through | |
# remove this block of code if you do not need | |
# an escape plan | |
if show_warning | |
show_warning = false | |
puts "There are #{res['total']} captures about to be deleted." | |
puts "Press Control+C now if you don't want that to happen." | |
(1..10).each do | |
print "." | |
sleep 1 | |
end | |
puts "" | |
end | |
# delete each of the captures on this page of results | |
res['captures'].each do |c| | |
delete_by_capture_id(c) | |
end | |
end | |
rescue Interrupt => e | |
puts "\nInterrupted by user." | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment