-
-
Save odyssey4me/6421703 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/bin/ruby | |
# vim:ts=2:expandtab | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
#http://wiki.apache.org/couchdb/Compaction | |
STDOUT.sync = true | |
print "Checking chef database size." | |
uri = URI.parse("http://localhost:5984/chef") | |
res = Net::HTTP.get_response(uri) | |
current_size = JSON::parse(res.body)["disk_size"] | |
if current_size > 100_000_000 | |
print " Current size of #{current_size} is too large. Initiating compact process.\n" | |
uri = URI.parse('http://localhost:5984/chef/_compact') | |
http = Net::HTTP.new(uri.host, uri.port) | |
req = Net::HTTP::Post.new(uri.request_uri) | |
req.set_form_data({}) | |
req["Content-Type"] = "application/json" | |
res = http.request(req) | |
puts JSON.pretty_generate(JSON.parse(res.body)) | |
else | |
print " Current size of #{current_size} is fine. Compacting not necessary.\n" | |
end | |
%w(nodes roles registrations clients data_bags data_bag_items users checksums cookbooks sandboxes environments id_map).each do |view| | |
begin | |
uri = URI.parse("http://localhost:5984/chef/_design/#{view}/_info") | |
res = Net::HTTP.get_response(uri) | |
current_size = JSON::parse(res.body)["view_index"]["disk_size"] | |
print "Checking view size for #{view}." | |
if current_size > 100_000_000 | |
print " Current size of #{current_size} is too large. Initiating compact process.\n" | |
uri = URI.parse("http://localhost:5984/chef/_compact/#{view}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
req = Net::HTTP::Post.new(uri.request_uri) | |
req.set_form_data({}) | |
req["Content-Type"] = "application/json" | |
res = http.request(req) | |
puts JSON.pretty_generate(JSON.parse(res.body)) | |
else | |
print " Current size of #{current_size} is fine. Compacting not necessary.\n" | |
end | |
rescue | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment