Last active
October 9, 2015 15:47
-
-
Save djk29a/c5b9e8e5a7bae32c3e1c to your computer and use it in GitHub Desktop.
[Chef] Get Bookshelf Usage by-org
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
# Retrieve the size of Chef cookbooks used by different organizations on the Chef server it runs on | |
# Should run as root normally since we use su to hop across to another user | |
full_org_list='' | |
full_file_list='' | |
orgs={} | |
# Getting orgs can take a while, let's hold onto the results for repeat invocations | |
if not File.exist?('.orgs-cached') | |
# NOTE: this produces 2 header lines and a footer line that has the rowcount | |
full_org_list=`su -l opscode-pgsql -c \"psql -d opscode_chef -c 'SELECT * from orgs'\"| tail -n +3 | sed -e 's/\s|\s/|/g'` | |
File.open(".orgs-cached","w") do |f| | |
f.puts full_org_list | |
end | |
else | |
full_org_list=IO.readlines('.orgs-cached') | |
end | |
# Getting a large bookshelf directory takes a significant period of time | |
if not File.exist?('.files-cached') | |
full_file_list=`find /var/opt/opscode/drbd/data/bookshelf/ -mindepth 2 -type f -printf \"%p|%k\n\" | sort -k 12 -t /` | |
File.open(".files-cached", "w") do |f| | |
f.puts full_file_list | |
end | |
else | |
full_file_list=IO.readlines('.files-cached') | |
end | |
full_org_list.each do |org_line| | |
org_cols = org_line.split('|') | |
next if org_cols.length != 8 | |
org_cols[0].strip! | |
# column 2 is name, column 0 is the id | |
orgs[org_cols[0]] = org_cols[2].strip | |
end | |
org_usage={} | |
full_file_list.each do |file_line| | |
/organization\-(\w+).*\|(\d+)$/ =~ file_line | |
org_id=$1 | |
next if not org_id | |
accum_sz = org_usage[org_id] || 0 | |
org_usage[org_id] = accum_sz.to_i + $2.to_i | |
end | |
total_use = 0 | |
org_usage.each do |k,v| | |
org_label = orgs[k] || k | |
puts "%-40s Usage: %10d KB" % [org_label, v] | |
total_use += v | |
end | |
puts "------------\n%-40s Total: %10d KB" % ["", total_use] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment