Skip to content

Instantly share code, notes, and snippets.

@duskhacker
Created May 8, 2009 16:42
Show Gist options
  • Save duskhacker/108873 to your computer and use it in GitHub Desktop.
Save duskhacker/108873 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'activerecord'
require 'spec'
class DiskFreeSpace < ActiveRecord::Base; end
class DiskFreeSpaceRunner
def initialize(environment = 'production')
establish_database(environment)
end
def collect
`df -h`.to_a.each do | line |
next if line =~ /^Filesystem/
filesystem, size, used, available, capacity, mounted_on = line.chomp.split(/\s+/)
DiskFreeSpace.create!(
:filesystem => filesystem,
:size => size,
:used => used,
:available => available,
:capacity => capacity,
:mounted_on => mounted_on
)
end
end
def report
DiskFreeSpace.find(:all).each do | df |
puts "#{df.created_at.strftime("%Y-%m-%d %H:%M:%S")} " +
"#{df.filesystem} #{df.size} #{df.used} #{df.available} #{df.capacity} #{df.mounted_on}"
puts "-" * 80
end
end
def establish_database(environment)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => "#{File.basename($0, '.rb')}_#{environment}.db"
)
unless ActiveRecord::Base.connection.tables.include?("disk_free_spaces")
ActiveRecord::Schema.define do
create_table :disk_free_spaces do |t|
t.string :filesystem
t.string :size
t.string :used
t.string :available
t.string :capacity
t.string :mounted_on
t.timestamps
end
end
end
end
end
if __FILE__ == $0
unless ARGV.size == 1
puts "\nusage: #{__FILE__} {collect|report|test}\n\n"
exit 1
end
case ARGV.shift
when 'report'
DiskFreeSpaceRunner.new.report
when 'collect'
DiskFreeSpaceRunner.new.collect
when 'test'
runner = DiskFreeSpaceRunner.new('test')
describe DiskFreeSpace do
before(:each) do
now = Time.now
Time.stub!(:now).and_return(now)
DiskFreeSpace.delete_all
df_h = "Filesystem Size Used Avail Capacity Mounted on\n" +
"/dev/disk0s2 96Gi 87Gi 8.4Gi 91% /\n" +
"devfs 122Ki 122Ki 0Bi 100% /dev\n"
runner.should_receive(:`).with("df -h").and_return(df_h)
runner.collect
end
it "should collect disk free space data" do
dfs = DiskFreeSpace.find(:all)
dfs.size.should == 2
df = dfs.first
df.filesystem.should == "/dev/disk0s2"
df.size.should == "96Gi"
df.used.should == "87Gi"
df.available.should == "8.4Gi"
df.capacity.should == "91%"
df.mounted_on.should == "/"
df = dfs.last
df.filesystem.should == "devfs"
df.size.should == "122Ki"
df.used.should == "122Ki"
df.available.should == "0Bi"
df.capacity.should == "100%"
df.mounted_on.should == "/dev"
end
it "should report disk free space data" do
runner.should_receive(:puts).with("#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} /dev/disk0s2 96Gi 87Gi 8.4Gi 91% /")
runner.should_receive(:puts).with("#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} devfs 122Ki 122Ki 0Bi 100% /dev")
runner.should_receive(:puts).with("-" * 80).twice
runner.report
end
end
else
puts "Unrecognized option"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment