Class: Informo::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/informo/storage.rb

Overview

This class is used to get info for the available and configured storage. For example: number of disks, partition info, etc…

Instance Method Summary collapse

Instance Method Details

#drive_countObject

returns the total number of disks



8
9
10
11
12
13
14
15
16
17
# File 'lib/informo/storage.rb', line 8

def drive_count
  count = 0
  `fdisk -l 2>/dev/null`.each_line do |line|
    if line =~ /^Disk \/dev\/[^mapper]/
      count += 1
    end
  end
  
  return count
end

#drive_details(disk) ⇒ Object

returns the details for a given disk

  • size

  • model

  • interface (scsi, sas, ide)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/informo/storage.rb', line 24

def drive_details(disk)
  details = Hash.new
  
  `fdisk -l #{disk} 2>/dev/null`.each_line do |i|
    details["size"] = $1 if i =~ /^Disk \/\w+\/\w+\:\s+(\d+.+\w+)\,/
  end
  
  `hdparm -i #{disk} 2>/dev/null`.each_line do |i|
    details["model"] = $1 if i =~ /Model=(.+?)\,/
  end
  
  File.exists?("/sbin/udevadm") ? cmd = "udevadm info" : cmd = "udevinfo"
  
  `#{cmd} -q symlink -n #{disk}`.each_line do |i|
    details["interface"] = $1 if i =~ /disk\/by-path\/\w+-\d+:\d+:\d+\.\d+-(\w+)-\d+:\d+/
  end
  
  return details
end

#drivesObject

returns an array of disks present



45
46
47
48
49
50
51
52
53
54
# File 'lib/informo/storage.rb', line 45

def drives
  drives = Array.new
  `fdisk -l 2>/dev/null`.each_line do |line|
    if line =~ /^Disk (\/\w+\/\w+)\:/
      drives.push($1)
    end
  end
  
  return drives
end

#mount_details(mountpoint) ⇒ Object

returns a hash of details for a given mount

  • device (/dev/sda? etc)

  • mount (/foo)

  • fstype (ext4)

  • options (mount options; eg: ro)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/informo/storage.rb', line 62

def mount_details(mountpoint)
  details = Hash.new
  File.open("/proc/mounts").each_line do |line|
    entry = line.split(/\s/)
    if line != /rootfs/ and entry[1] == mountpoint
      details["device"] = entry[0]
      details["mount"] = entry[1]
      details["fstype"] = entry[2]
      details["options"] = entry[3]
    end
  end
  
  return details
end

#mounts(type = ".+") ⇒ Object

returns an array of mounts that are mounted



78
79
80
81
82
83
84
85
# File 'lib/informo/storage.rb', line 78

def mounts(type=".+")
  mounts = Array.new
  File.open("/proc/mounts").each_line do |line|
    mounts.push(line.split(/\s/)[1]) if line != /rootfs/ and line.split(/\s/)[2] =~ /#{type}/
  end
  
  return mounts
end