Class: MoolDisk
- Inherits:
-
Object
- Object
- MoolDisk
- Defined in:
- lib/mool/disk.rb
Constant Summary collapse
- PATH_DEV_BLOCK =
Dir.glob('/sys/dev/block/*')
Instance Attribute Summary collapse
-
#block_free ⇒ Object
Returns the value of attribute block_free.
-
#block_used ⇒ Object
Returns the value of attribute block_used.
-
#devname ⇒ Object
Returns the value of attribute devname.
-
#devtype ⇒ Object
Returns the value of attribute devtype.
-
#file_system ⇒ Object
Returns the value of attribute file_system.
-
#major ⇒ Object
Returns the value of attribute major.
-
#minor ⇒ Object
Returns the value of attribute minor.
-
#mount_point ⇒ Object
Returns the value of attribute mount_point.
-
#partitions ⇒ Object
Returns the value of attribute partitions.
-
#path ⇒ Object
Returns the value of attribute path.
-
#size ⇒ Object
Returns the value of attribute size.
-
#slaves ⇒ Object
Returns the value of attribute slaves.
-
#swap ⇒ Object
Returns the value of attribute swap.
-
#total_block ⇒ Object
Returns the value of attribute total_block.
-
#unity ⇒ Object
Returns the value of attribute unity.
Class Method Summary collapse
Instance Method Summary collapse
- #capacity ⇒ Object
- #dev ⇒ Object
- #free_percent ⇒ Object
-
#initialize(dev_name) ⇒ MoolDisk
constructor
A new instance of MoolDisk.
- #is_disk? ⇒ Boolean
- #is_partition? ⇒ Boolean
- #logical_name ⇒ Object
- #mounting_point ⇒ Object
- #read_uevent ⇒ Object
- #to_b ⇒ Object
- #to_gb ⇒ Object
- #to_kb ⇒ Object
- #to_mb ⇒ Object
- #used_percent ⇒ Object
Constructor Details
#initialize(dev_name) ⇒ MoolDisk
Returns a new instance of MoolDisk.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mool/disk.rb', line 6 def initialize(dev_name) @path = PATH_DEV_BLOCK.select{ |entry| ( File.read("#{entry}/dev").include?(dev_name)) || ( File.read("#{entry}/uevent").include?(dev_name)) || ( File.exist?("#{entry}/dm/name") && File.read("#{entry}/dm/name").include?(dev_name)) }.first raise "Does't exist #{dev_name}!" if @path.nil? read_uevent logical_name swap capacity @unity = Mool::BYTES mounting_point file_system end |
Instance Attribute Details
#block_free ⇒ Object
Returns the value of attribute block_free.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def block_free @block_free end |
#block_used ⇒ Object
Returns the value of attribute block_used.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def block_used @block_used end |
#devname ⇒ Object
Returns the value of attribute devname.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def devname @devname end |
#devtype ⇒ Object
Returns the value of attribute devtype.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def devtype @devtype end |
#file_system ⇒ Object
Returns the value of attribute file_system.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def file_system @file_system end |
#major ⇒ Object
Returns the value of attribute major.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def minor @minor end |
#mount_point ⇒ Object
Returns the value of attribute mount_point.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def mount_point @mount_point end |
#partitions ⇒ Object
Returns the value of attribute partitions.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def partitions @partitions end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def path @path end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def size @size end |
#slaves ⇒ Object
Returns the value of attribute slaves.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def slaves @slaves end |
#swap ⇒ Object
Returns the value of attribute swap.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def swap @swap end |
#total_block ⇒ Object
Returns the value of attribute total_block.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def total_block @total_block end |
#unity ⇒ Object
Returns the value of attribute unity.
4 5 6 |
# File 'lib/mool/disk.rb', line 4 def unity @unity end |
Class Method Details
.all ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mool/disk.rb', line 84 def self.all disks = [] PATH_DEV_BLOCK.each do |entry| real_path = `readlink -f #{entry}`.chomp disks << MoolDisk.new(entry.split("/").last) if (not real_path.include?("virtual")) && (not real_path.include?("/sr")) && (not File.exist?("#{real_path}/partition")) && Dir.glob("#{real_path}/slaves/*").empty? end disks.each{ |disk| disk.partitions.each { |part| part.partitions and part.slaves }} disks.each{ |disk| disk.slaves.each { |part| part.partitions and part.slaves }} disks end |
.all_usable ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/mool/disk.rb', line 110 def self.all_usable result = MoolDisk.all result.each do |disk| result += (disk.partitions + disk.slaves + (disk.partitions + disk.slaves).collect{|p| p.partitions + p.slaves }.flatten) end result.reject(&:blank?).select{|d| (d.partitions + d.slaves).blank? } end |
.swap ⇒ Object
105 106 107 108 |
# File 'lib/mool/disk.rb', line 105 def self.swap result = File.read("/proc/swaps").scan(/.*\n\/dev\/(\S+)/).flatten.first MoolDisk.new(result) unless result.nil? end |
Instance Method Details
#capacity ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/mool/disk.rb', line 45 def capacity unless (defined?(@total_block) && defined?(@block_used) && defined?(@block_free)) result = `df`.scan(/(#{@logical_name}|#{@devname})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/).flatten @total_block = File.read("#{@path}/size").chomp.to_f * Mool::BLOCK_SIZE @block_used = result[2].to_f * Mool::BLOCK_SIZE @block_free = @total_block - @block_used end end |
#dev ⇒ Object
37 |
# File 'lib/mool/disk.rb', line 37 def dev; @major + @minor; end |
#free_percent ⇒ Object
80 81 82 |
# File 'lib/mool/disk.rb', line 80 def free_percent @block_free / @total_block end |
#is_disk? ⇒ Boolean
39 |
# File 'lib/mool/disk.rb', line 39 def is_disk?; @devtype == "disk"; end |
#is_partition? ⇒ Boolean
41 |
# File 'lib/mool/disk.rb', line 41 def is_partition?; @devtype == "partition"; end |
#logical_name ⇒ Object
29 30 31 |
# File 'lib/mool/disk.rb', line 29 def logical_name @logical_name ||= File.exists?("#{@path}/dm/name")? File.read("#{@path}/dm/name").chomp : @devname end |
#mounting_point ⇒ Object
21 22 23 |
# File 'lib/mool/disk.rb', line 21 def mounting_point @mount_point ||= File.read("/proc/mounts").scan(/#{@logical_name} (\S+)/).flatten.first if File.read("/proc/mounts").include?(@logical_name) end |
#read_uevent ⇒ Object
33 34 35 |
# File 'lib/mool/disk.rb', line 33 def read_uevent @major, @minor, @devname, @devtype = File.read("#{@path}/uevent").scan(/.*=(\d+)\n.*=(\d+)\n.*=(\S+)\n.*=(\w+)\n/).flatten end |
#to_b ⇒ Object
100 |
# File 'lib/mool/disk.rb', line 100 def to_b; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::BYTES); end |
#to_gb ⇒ Object
103 |
# File 'lib/mool/disk.rb', line 103 def to_gb; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::GBYTES); end |
#to_kb ⇒ Object
101 |
# File 'lib/mool/disk.rb', line 101 def to_kb; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::KBYTES); end |
#to_mb ⇒ Object
102 |
# File 'lib/mool/disk.rb', line 102 def to_mb; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::MBYTES); end |
#used_percent ⇒ Object
76 77 78 |
# File 'lib/mool/disk.rb', line 76 def used_percent @block_used / @total_block end |