Class: StorageVisualizer
- Inherits:
-
Object
- Object
- StorageVisualizer
- Defined in:
- lib/storage_visualizer.rb
Instance Attribute Summary collapse
-
#available ⇒ Object
Returns the value of attribute available.
-
#available_gb ⇒ Object
Returns the value of attribute available_gb.
-
#capacity ⇒ Object
disk Bytes.
-
#capacity_gb ⇒ Object
disk GB for display.
-
#diskhash ⇒ Object
Returns the value of attribute diskhash.
-
#target_dir ⇒ Object
other.
-
#threshold_pct ⇒ Object
Returns the value of attribute threshold_pct.
-
#tree ⇒ Object
Returns the value of attribute tree.
-
#tree_formatted ⇒ Object
Returns the value of attribute tree_formatted.
-
#used ⇒ Object
Returns the value of attribute used.
-
#used_gb ⇒ Object
Returns the value of attribute used_gb.
Class Method Summary collapse
-
.print_usage ⇒ Object
Static.
Instance Method Summary collapse
- #analyze_dirs(dir_to_analyze) ⇒ Object
- #format_data_for_the_chart ⇒ Object
- #get_basic_disk_info ⇒ Object
-
#initialize(target_dir_passed = nil) ⇒ StorageVisualizer
constructor
Constructor.
- #run ⇒ Object
- #write_storage_report ⇒ Object
Constructor Details
#initialize(target_dir_passed = nil) ⇒ StorageVisualizer
Constructor
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/storage_visualizer.rb', line 48 def initialize(target_dir_passed = nil) if (target_dir_passed != nil) = File.(target_dir_passed) puts "Target dir: #{expanded}" if (Dir.exist?()) self.target_dir = else raise "Target directory does not exist: #{expanded}" end else # no target passed, use the user's home dir self.target_dir = File.('~') end self.threshold_pct = 0.05 self.diskhash = {} self.tree = [] self.tree_formatted = '' end |
Instance Attribute Details
#available ⇒ Object
Returns the value of attribute available.
35 36 37 |
# File 'lib/storage_visualizer.rb', line 35 def available @available end |
#available_gb ⇒ Object
Returns the value of attribute available_gb.
39 40 41 |
# File 'lib/storage_visualizer.rb', line 39 def available_gb @available_gb end |
#capacity ⇒ Object
disk Bytes
33 34 35 |
# File 'lib/storage_visualizer.rb', line 33 def capacity @capacity end |
#capacity_gb ⇒ Object
disk GB for display
37 38 39 |
# File 'lib/storage_visualizer.rb', line 37 def capacity_gb @capacity_gb end |
#diskhash ⇒ Object
Returns the value of attribute diskhash.
44 45 46 |
# File 'lib/storage_visualizer.rb', line 44 def diskhash @diskhash end |
#target_dir ⇒ Object
other
41 42 43 |
# File 'lib/storage_visualizer.rb', line 41 def target_dir @target_dir end |
#threshold_pct ⇒ Object
Returns the value of attribute threshold_pct.
45 46 47 |
# File 'lib/storage_visualizer.rb', line 45 def threshold_pct @threshold_pct end |
#tree ⇒ Object
Returns the value of attribute tree.
42 43 44 |
# File 'lib/storage_visualizer.rb', line 42 def tree @tree end |
#tree_formatted ⇒ Object
Returns the value of attribute tree_formatted.
43 44 45 |
# File 'lib/storage_visualizer.rb', line 43 def tree_formatted @tree_formatted end |
#used ⇒ Object
Returns the value of attribute used.
34 35 36 |
# File 'lib/storage_visualizer.rb', line 34 def used @used end |
#used_gb ⇒ Object
Returns the value of attribute used_gb.
38 39 40 |
# File 'lib/storage_visualizer.rb', line 38 def used_gb @used_gb end |
Class Method Details
.print_usage ⇒ Object
Static
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/storage_visualizer.rb', line 10 def self.print_usage puts "\nThis tool helps visualize which directories are occupying the most storage. Any directory that occupies more than 5% of disk space is added to a visual hierarchichal storage report in the form of a Google Sankey diagram. The storage data is gathered using the linux `du` utility. It has been tested on Mac OSX, should work on linux systems, will not work on Windows. Run as sudo if analyzing otherwise inaccessible directories. May take a while to run\n" puts "\nCommand line usage: \n\t[sudo] ./visualize_storage.rb [directory to visualize (default ~/) | -h (help) -i | --install (install to /usr/local/bin)]\n\n" puts "API usage: " puts "\t'require storage_visualizer'" puts "\tsv = StorageVisualizer.new('[directory to visualize, ~/ by default]')" puts "\tsv.run()\n\n" puts "A report will be created in the current directory named as such: StorageReport_2015_05_25-17_19_30.html" puts "Status messages are printed to STDOUT" puts "\n\n" end |
Instance Method Details
#analyze_dirs(dir_to_analyze) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/storage_visualizer.rb', line 225 def analyze_dirs(dir_to_analyze) # bootstrap case if (dir_to_analyze == '/') # run on all child dirs Dir.entries(dir_to_analyze).reject {|d| d.start_with?('.')}.each do |name| # puts "\tentry: >#{file}<" full_path = File.join(dir_to_analyze, name) if (Dir.exist?(full_path)) # puts "Contender: >#{full_path}<" analyze_dirs(full_path) end end return end cmd = "du -sx \"#{dir_to_analyze}\"" puts "\trunning #{cmd}" output = `#{cmd}`.strip().split("\t") # puts "Du output:" # pp output size = output[0].to_i * 512 size_gb = "#{'%.0f' % (size.to_f / 1024 / 1024 / 1024)}" # puts "Size: #{size}\nCapacity: #{self.diskhash['/']['capacity']}" occupancy = (size.to_f / self.capacity.to_f) occupancy_pct = "#{'%.0f' % (occupancy * 100)}" capacity_gb = "#{'%.0f' % (self.capacity.to_f / 1024 / 1024 / 1024)}" # if this dir contains more than 5% of disk space, add it to the tree if (occupancy > self.threshold_pct) puts "Dir contains more than 5% of disk space: #{dir_to_analyze} \n\tsize:\t#{size_gb} / \ncapacity:\t#{capacity_gb} = #{occupancy_pct}%" # push this dir's info if (dir_to_analyze == self.target_dir) other_space = self.used - size other_space_gb = "#{'%.0f' % (other_space / 1024 / 1024 / 1024)}" other_space_array = ['/', 'Other', other_space_gb] short_target_dir = self.target_dir.split('/').reverse[0] short_target_dir = (short_target_dir == nil) ? self.target_dir : short_target_dir comparison = ['/', short_target_dir, size_gb] # add them to the array self.tree.push(other_space_array) self.tree.push(comparison) else # get parent dir and add to the tree short_parent = dir_to_analyze.split('/').reverse[1] # short_parent = (short_parent == nil) ? parent : short_parent # case for when parent is '/' short_parent = (short_parent == '') ? '/' : short_parent short_dir = dir_to_analyze.split('/').reverse[0] # array_to_push = [parent, dir_to_analyze, size_gb] array_to_push = [short_parent, short_dir, size_gb] self.tree.push(array_to_push) end # run on all child dirs Dir.entries(dir_to_analyze).reject {|d| d.start_with?('.')}.each do |name| # puts "\tentry: >#{file}<" full_path = File.join(dir_to_analyze, name) if (Dir.exist?(full_path)) # puts "Contender: >#{full_path}<" analyze_dirs(full_path) end end end end |
#format_data_for_the_chart ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/storage_visualizer.rb', line 72 def format_data_for_the_chart working_string = "[\n" self.tree.each_with_index do |entry, index| if(index == self.tree.length - 1) # this is the next to last element, it gets no comma working_string << "[ '#{entry[0]}', '#{entry[1]}', #{entry[2]} ]\n" else # mind the comma working_string << "[ '#{entry[0]}', '#{entry[1]}', #{entry[2]} ],\n" end end working_string << "]\n" self.tree_formatted = working_string end |
#get_basic_disk_info ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/storage_visualizer.rb', line 178 def get_basic_disk_info # df -l gets info about locally-mounted filesystems output = `df -l` # Looks like this: # {"/"=> # {"capacity"=>498876809216, "used"=>434777001984, "available"=>63837663232}, # "/Volumes/MobileBackups"=> # {"capacity"=>498876809216, "used"=>498876809216, "available"=>0} # } output.lines.each_with_index do |line, index| if (index == 0) next end cols = line.split # ["Filesystem", "512-blocks", "Used", "Available", "Capacity", "iused", "ifree", "%iused", "Mounted", "on"] # line: ["/dev/disk1", "974368768", "849157528", "124699240", "88%", "106208689", "15587405", "87%", "/"] self.diskhash[cols[8]] = { 'capacity' => (cols[1].to_i * 512).to_i, 'used' => (cols[2].to_i * 512).to_i, 'available' => (cols[3].to_i * 512).to_i } end # puts "Disk mount info:" # pp diskhash self.capacity = self.diskhash['/']['capacity'] self.used = self.diskhash['/']['used'] self.available = self.diskhash['/']['available'] free_space = (self.available).to_i free_space_gb = "#{'%.0f' % (free_space / 1024 / 1024 / 1024)}" free_space_array = ['/', 'Free Space', free_space_gb] self.tree.push(free_space_array) self.capacity_gb = "#{'%.0f' % (self.capacity.to_i / 1024 / 1024 / 1024)}" self.used_gb = "#{'%.0f' % (self.used.to_i / 1024 / 1024 / 1024)}" self.available_gb = "#{'%.0f' % (self.available.to_i / 1024 / 1024 / 1024)}" end |
#run ⇒ Object
311 312 313 314 315 316 317 |
# File 'lib/storage_visualizer.rb', line 311 def run self.get_basic_disk_info self.analyze_dirs(self.target_dir) self.format_data_for_the_chart self.write_storage_report end |
#write_storage_report ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/storage_visualizer.rb', line 90 def write_storage_report the_html = %q|<html> <body> <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['sankey']}]}"> </script> <style> td { font-family:sans-serif; font-size:8pt; } .bigger { font-family:sans-serif; font-size:10pt; font-weight:bold } </style> <div class="table"> <div class="bigger">Storage Report</div> <table> <tr> <td style="text-align:right">Disk Capacity:</td><td>| + self.capacity_gb + %q| GB</td> </tr> <tr> <td style="text-align:right">Disk Used:</td><td>| + self.used_gb + %q| GB</td> </tr> <tr> <td style="text-align:right">Free Space:</td><td>| + self.available_gb + %q| GB</td> </tr> </table> </div> <div id="sankey_multiple" style="width: 900px; height: 300px;"></div> <script type="text/javascript"> google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('string', 'From'); data.addColumn('string', 'To'); data.addColumn('number', 'Weight'); data.addRows( | + self.tree_formatted + %q|); // Set chart options var options = { width: 1000, sankey: { iterations: 32, node: { label: { fontName: 'Arial', fontSize: 10, color: '#871b47', bold: false, italic: true } } }, }; // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple')); chart.draw(data, options); } </script> </body> </html>| filename = DateTime.now.strftime("./StorageReport_%Y_%m_%d-%H_%M_%S.html") puts "Writing html file #{filename}" f = File.open(filename, 'w+') f.write(the_html) f.close end |