class DiskHandler::Disk

Attributes

id[RW]
model[RW]
name[RW]
number[RW]
partitions[RW]
size[RW]
state[RW]
type[RW]
version[RW]

Public Class Methods

new(lsblk_line) click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 19
def initialize lsblk_line
  attrs_from_line lsblk_line
  check_smart_capability!
  check_health! if smart_capable?
  parse_smart_info if smart_capable?
  populate_partitions
end

Public Instance Methods

attrs_from_line(lsblk_line) click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 27
def attrs_from_line lsblk_line
  %w{NAME TYPE SIZE MODEL STATE}.each do |key|
    matches = lsblk_line.match(/#{key}="([^"]*)"/)
    self.send("#{key.downcase}=", matches[1]) if matches
  end
end
check_health!() click to toggle source

Check the SMART health

# File lib/disk_reporter/disk_handler.rb, line 46
def check_health!
  output = `sudo smartctl -H #{device_path}`
  @smart_healthy = !output.scan(/PASSED/).empty?
  @health_output = output
end
check_smart_capability!() click to toggle source

Checks if disk is capable

# File lib/disk_reporter/disk_handler.rb, line 62
def check_smart_capability!
  output = `sudo smartctl -i #{device_path}`
  @smart_available = !output.scan(/SMART support is: Available/).empty?
  @smart_enabled = !output.scan(/SMART support is: Enabled/).empty?
  @capability_output = output
end
device_path() click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 7
def device_path
  "/dev/#{name}"
end
get_smart_info() click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 34
def get_smart_info
  `smartctl -i /dev/#{name}`
end
parse_smart_info() click to toggle source

Parses SMART drive info

# File lib/disk_reporter/disk_handler.rb, line 54
def parse_smart_info
   %w{Id Number Version}.each do |key|
     matches = @capability_output.match(/#{key}:\s+([^\n]*)\n/)
     self.send("#{key.downcase}=", matches[1]) if matches
   end    
end
populate_partitions() click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 74
def populate_partitions
  self.partitions = []
  `ls #{device_path}[0-9]* 2>/dev/null`.each_line do |name|
     self.partitions << Partition.new(self, name)
  end
end
serial_number() click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 11
def serial_number
 number.strip
end
smart_capable?() click to toggle source

Is the device SMART capable and enabled

# File lib/disk_reporter/disk_handler.rb, line 40
def smart_capable?
  @smart_available && @smart_enabled
end
to_h() click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 69
def to_h
  { name: name, size: size, model: model, smart_available: @smart_available, smart_enabled: @smart_enabled, wnn: wnn, serial: serial_number, version: version 
   }
end
wnn() click to toggle source
# File lib/disk_reporter/disk_handler.rb, line 15
def wnn
  id.delete(' ')
end