Class: Mool::Disk

Inherits:
Base
  • Object
show all
Defined in:
lib/mool/disk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attrs

Constructor Details

#initialize(devname, path = nil) ⇒ Disk

Returns a new instance of Disk.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mool/disk.rb', line 24

def initialize(devname, path = nil)
  @unity = Mool::BYTES
  @devname = devname

  @path = if path.nil?
            Mool::Disk.proc_partitions_hash(@devname)[:path]
          else
            path
          end

  raise "Does't exist #{devname} on #{@path}" if @path.nil? || @devname.nil?
  lname = Mool::Command.logical_name(@path)
  @logical_name = lname.blank? ? @devname : lname
  read_uevent
  capacity
end

Instance Attribute Details

#block_freeObject

Returns the value of attribute block_free.



3
4
5
# File 'lib/mool/disk.rb', line 3

def block_free
  @block_free
end

#block_usedObject

Returns the value of attribute block_used.



3
4
5
# File 'lib/mool/disk.rb', line 3

def block_used
  @block_used
end

#devnameObject

Returns the value of attribute devname.



3
4
5
# File 'lib/mool/disk.rb', line 3

def devname
  @devname
end

#devtypeObject

Returns the value of attribute devtype.



3
4
5
# File 'lib/mool/disk.rb', line 3

def devtype
  @devtype
end

#file_systemObject

Returns the value of attribute file_system.



3
4
5
# File 'lib/mool/disk.rb', line 3

def file_system
  @file_system
end

#free_sizeObject

Returns the value of attribute free_size.



3
4
5
# File 'lib/mool/disk.rb', line 3

def free_size
  @free_size
end

#logical_nameObject

Returns the value of attribute logical_name.



3
4
5
# File 'lib/mool/disk.rb', line 3

def logical_name
  @logical_name
end

#majorObject

Returns the value of attribute major.



3
4
5
# File 'lib/mool/disk.rb', line 3

def major
  @major
end

#minorObject

Returns the value of attribute minor.



3
4
5
# File 'lib/mool/disk.rb', line 3

def minor
  @minor
end

#mount_pointObject

Returns the value of attribute mount_point.



3
4
5
# File 'lib/mool/disk.rb', line 3

def mount_point
  @mount_point
end

#partitionsObject

Returns the value of attribute partitions.



3
4
5
# File 'lib/mool/disk.rb', line 3

def partitions
  @partitions
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/mool/disk.rb', line 3

def path
  @path
end

#percentage_usedObject

Returns the value of attribute percentage_used.



3
4
5
# File 'lib/mool/disk.rb', line 3

def percentage_used
  @percentage_used
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/mool/disk.rb', line 3

def size
  @size
end

#slavesObject

Returns the value of attribute slaves.



3
4
5
# File 'lib/mool/disk.rb', line 3

def slaves
  @slaves
end

#swapObject

Returns the value of attribute swap.



3
4
5
# File 'lib/mool/disk.rb', line 3

def swap
  @swap
end

#total_blockObject

Returns the value of attribute total_block.



3
4
5
# File 'lib/mool/disk.rb', line 3

def total_block
  @total_block
end

#total_sizeObject

Returns the value of attribute total_size.



3
4
5
# File 'lib/mool/disk.rb', line 3

def total_size
  @total_size
end

#unityObject

Returns the value of attribute unity.



3
4
5
# File 'lib/mool/disk.rb', line 3

def unity
  @unity
end

#used_sizeObject

Returns the value of attribute used_size.



3
4
5
# File 'lib/mool/disk.rb', line 3

def used_size
  @used_size
end

Class Method Details

.allObject



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
# File 'lib/mool/disk.rb', line 238

def self.all
  disks = []

  proc_partitions_hash.each do |disk_name, disk_opts|
    all_partitions = []
    disk = Mool::Disk.new(disk_name, disk_opts[:path])

    disk_opts[:partitions].map do |partition_name, partition_opts|
      partition = Mool::Disk.new(
        partition_name,
        partition_opts[:path]
      )

      all_partitions << partition

      partition.slaves = partition_opts[:slaves].map do |slave_name, slave_opts|
        Mool::Disk.new(
          slave_name,
          slave_opts[:path]
        )
      end
    end

    disk.partitions = all_partitions

    disk.slaves = disk_opts[:slaves].map do |slave_name, slave_opts|
      Mool::Disk.new(
        slave_name,
        slave_opts[:path]
      )
    end

    disks << disk
  end

  disks.each do |disk|
    disk.partitions.each do |partition|
      partition.slaves.each do |slave|
        partition.total_size += slave.total_size
        partition.used_size += slave.used_size
        partition.free_size += slave.free_size
        partition.block_free += slave.block_free
        partition.block_used += slave.block_used
        partition.total_block += slave.total_block
      end
      disk.total_size += partition.total_size
      disk.used_size += partition.used_size
      disk.free_size += partition.free_size
      disk.block_free += partition.block_free
      disk.block_used += partition.block_used
      disk.total_block += partition.total_block
    end
  end

  disks
end

.find_especific_devname(obj, key) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/mool/disk.rb', line 204

def self.find_especific_devname(obj, key)
  if obj.respond_to?(:key?) && obj.key?(key)
    obj[key]
  elsif obj.respond_to?(:each)
    r = nil
    obj.find { |*a| r = find_especific_devname(a.last, key) }
    r
  end
end

.find_partitions(disk, all_partitions) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/mool/disk.rb', line 184

def self.find_partitions(disk, all_partitions)
  parts = Mool::Command.partitions(disk)

  result = {}

  parts.each do |part|
    all_partitions.each do |partition|
      devname = partition[3]
      next unless part.include?(devname)
      all_partitions.delete(partition)
      result[devname] = {
        path: part,
        slaves: find_slaves(part, all_partitions)
      }
    end
  end

  result
end

.find_slaves(path, all_partitions) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mool/disk.rb', line 167

def self.find_slaves(path, all_partitions)
  slaves = Mool::Command.holders(path)

  result = {}

  slaves.each do |slave|
    all_partitions.each do |partition|
      devname = partition[3]
      next unless slave.include?(devname)
      all_partitions.delete(partition)
      result[devname] = { path: path + "/holders/#{slave}" }
    end
  end

  result
end

.proc_partitions_hash(especific_devname = nil) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mool/disk.rb', line 214

def self.proc_partitions_hash(especific_devname = nil)
  all_partitions = Mool::Command.all_partitions.scan(/(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/)
  hash_disks = {}

  all_partitions.each do |partition|
    if partition[3].include?('ram') || partition[3].include?('sr')
      all_partitions.delete(partition)
      next
    end
    devname = partition[3]
    path = "/sys/block/#{devname}"
    next unless Mool::Command.root_block_device?(devname)
    all_partitions.delete(partition)
    hash_disks[devname] = {
      path: path,
      partitions: find_partitions(devname, all_partitions),
      slaves: find_slaves(path, all_partitions)
    }
  end

  return hash_disks if especific_devname.nil?
  find_especific_devname(hash_disks, especific_devname)
end

.swapObject



162
163
164
165
# File 'lib/mool/disk.rb', line 162

def self.swap
  result = Mool::Command.swap.scan(%r{/.*\n\/dev\/(\S+)/}).flatten.first
  Mool::Disk.new(result) unless result.nil?
end

Instance Method Details

#capacityObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mool/disk.rb', line 84

def capacity
  return if defined?(@total_block) && defined?(@block_used) && defined?(@block_free)
  result = Mool::Command.df.scan(
    /(#{@logical_name}|#{@devname})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/
  ).flatten

  # @total_block = Mool::Command.capacity_partition_command(@path).chomp.to_f
  @total_block = result[1].to_f
  @total_size = result[1].to_f * Mool::BLOCK_SIZE
  @block_used  = result[2].to_f
  @block_free  = result[3].to_f
  @used_size = result[2].to_f * Mool::BLOCK_SIZE
  @free_size = result[3].to_f * Mool::BLOCK_SIZE
  return if result.empty?
  @percentage_used = result[4].delete('%')
end

#devObject



68
69
70
# File 'lib/mool/disk.rb', line 68

def dev
  @major + @minor
end

#disk?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/mool/disk.rb', line 72

def disk?
  @devtype == 'disk'
end

#free_percentObject



126
127
128
# File 'lib/mool/disk.rb', line 126

def free_percent
  @block_free / @total_block
end

#mounting_pointObject



41
42
43
44
45
46
47
# File 'lib/mool/disk.rb', line 41

def mounting_point
  @mount_point = nil
  Mool::Command.mount.include?(@devname) &&
    @mount_point ||= Mool::Command.mount.scan(
    /#{@devname} (\S+)/
  ).flatten.first
end

#partition?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/mool/disk.rb', line 76

def partition?
  @devtype == 'partition'
end

#read_ueventObject



58
59
60
61
62
63
64
65
66
# File 'lib/mool/disk.rb', line 58

def read_uevent
  @major,
  @minor,
  @devname,
  @devtype =
  Mool::Command.uevent(@path).split("\n").map do |result|
    result.split('=').last
  end
end

#to_bObject



130
131
132
133
134
135
136
# File 'lib/mool/disk.rb', line 130

def to_b
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::BYTES)
end

#to_gbObject



154
155
156
157
158
159
160
# File 'lib/mool/disk.rb', line 154

def to_gb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::GBYTES)
end

#to_kbObject



138
139
140
141
142
143
144
# File 'lib/mool/disk.rb', line 138

def to_kb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::KBYTES)
end

#to_mbObject



146
147
148
149
150
151
152
# File 'lib/mool/disk.rb', line 146

def to_mb
  Mool.parse_to(self,
                ['@total_block',
                 '@block_used',
                 '@block_free'],
                Mool::MBYTES)
end

#used_percentObject



122
123
124
# File 'lib/mool/disk.rb', line 122

def used_percent
  @block_used / @total_block
end