Get Available Diskspace in Ruby

Get available diskspace in ruby

You could use the sys-filesystem gem (cross platform friendly)

require 'sys/filesystem'

stat = Sys::Filesystem.stat("/")
mb_available = stat.block_size * stat.blocks_available / 1024 / 1024

Find available disk space for a network location using Ruby

As suggested by lurker, the Windows API can return the free disk space:

require "Win32API"  

GetDiskFreeSpaceEx = Win32API.new("kernel32", "GetDiskFreeSpaceEx",
['p','p','p','p'], 'i')

def get_disk_free_space(path)
free_caller = " " * 8
total = " " * 8
free = " " * 8
GetDiskFreeSpaceEx.call(path, free_caller, total, free)
l,h = free_caller.unpack("II")
l + (h << 32)
end

path = "\\\\server\\share\\"
puts "#{get_disk_free_space(path)} bytes"
puts "#{(get_disk_free_space(path)/1073741824.0).round(2)} GB"

Ensuring that the path has a trailing backslash.

Ruby Get Available Disk Drives

The article Brian gave correctly states the following code:

require 'win32ole'

file_system = WIN32OLE.new("Scripting.FileSystemObject")
drives = file_system.Drives
drives.each do |drive|
puts "Available space: #{drive.AvailableSpace}"
puts "Drive letter: #{drive.DriveLetter}"
puts "Drive type: #{drive.DriveType}"
puts "File system: #{drive.FileSystem}"
puts "Is ready: #{drive.IsReady}"
puts "Path: #{drive.Path}"
puts "Root folder: #{drive.RootFolder}"
puts "Serial number: #{drive.SerialNumber}"
puts "Share name: #{drive.ShareName}"
puts "Total size: #{drive.TotalSize}"
puts "Volume name: #{drive.VolumeName}"
end

How to get the total size of files in a Directory in Ruby

In Windows, You can use win32ole gem to calculate the same

 require 'win32ole'

fso = WIN32OLE.new('Scripting.FileSystemObject')

folder = fso.GetFolder('directory path')

p folder.name

p folder.size

p folder.path

Rails server disk space keeps showing out of space

One of the issues could be that processes may be removing some large files but the files may still be on disk, and would be removed when the process gets a SIGHUP or the process is restarted.

You can find such files by doing:

lsof -n | grep -i deleted

This will show you a list of deleted files and the process. You can restart that process to free up the disk or you can send a SIGHUP signal to the process.

To see what's taking up disk space, you will have to keep a watch on a few things. You can create a cron job that runs every 5 minutes (or every 10 minutes or 30 minutes, you choose) that does:

date >> /tmp/deleted-files.txt && lsof -n | grep -i deleted >> /tmp/deleted-files.txt

Analyze the file and see if files are being created and deleted chronically.

If you have identified the directory that keeps on growing, you can also create a cron job that runs every few minutes to save the file listing in a temporary file like so

date >> /tmp/file-list.txt && ls -ltrh >> /tmp/file-list.txt

That way you can watch the files that are being generated and review their contents. It is possible that someone may be logging in debug mode.

If you are using Ruby on Rails (RoR), Ruby on Rails production log rotation thread can help you set up log rotation. You can be aggressive about log rotation to get a handle on the disk size.

One thing I can tell you is that if you attach an EBS volume worth 200 GB, the cost will be ~$200 for the year and you will have to spend less time urgently on the issue. If your time savings generates more revenue than $200 in the year, getting an EBS volume and storing logs on that would be a much cheaper proposition in the long run.

What is the fastest way to calculate disk usage per customer?

If you want to go with pure Ruby you can try this code. Although if you're looking for speed I'm sure du would be faster.

def dir_size(dir_path)
require 'find'
size = 0
Find.find(dir_path) { |f| size += File.size(f) if File.file?(f) }
size
end

dir_size('/tmp/')

Ruby: getting disk usage information on Linux from /proc (or some other way that is non-blocking and doesn't spawn a new process)

The information can be accessed via the system call statfs

http://man7.org/linux/man-pages/man2/statfs.2.html

I can see there is a ruby interface to this here:

http://ruby-doc.org/core-trunk/File/Statfs.html

how to find the amount of space in a directory or the partition directory is in

I assume you mean you have a specific directory, on a specific partition that you wish to fill until there is only 100 MB left.

The df command will return the amount of disk space left on a given directory's disk/partition.

df musicfolder/

The fourth column will give free space

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1 151733412 24153792 119871924 17% /

you can use awk to gain the fourth column value and ignore the headers. So your script will be something like:

freespace=$(df /musicfolder | awk 'FNR>1{print $4}')

while [ $freespace -gt 10000000 ] ; do
(copy files from wherever)
freespace=$(df ~/musicfolder | awk 'FNR>1{print $4}')
done

Recursively getting the size of a directory

Looks like sys-filesystem handles this, but you'll need to do some math to convert the available blocks into bytes (by multiplying by block-size).



Related Topics



Leave a reply



Submit