Class: DiskHandler::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/disk_reporter/disk_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lsblk_line) ⇒ Disk

Returns a new instance of Disk.



24
25
26
27
28
29
30
# File 'lib/disk_reporter/disk_handler.rb', line 24

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

Instance Attribute Details

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def id
  @id
end

#modelObject

Returns the value of attribute model.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def model
  @model
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def name
  @name
end

#numberObject

Returns the value of attribute number.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def number
  @number
end

#partitionsObject

Returns the value of attribute partitions.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def partitions
  @partitions
end

#sizeObject

Returns the value of attribute size.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def size
  @size
end

#stateObject

Returns the value of attribute state.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def state
  @state
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def type
  @type
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/disk_reporter/disk_handler.rb', line 10

def version
  @version
end

Instance Method Details

#attrs_from_line(lsblk_line) ⇒ Object



32
33
34
35
36
37
# File 'lib/disk_reporter/disk_handler.rb', line 32

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!Object

Check the SMART health



51
52
53
54
55
# File 'lib/disk_reporter/disk_handler.rb', line 51

def check_health!
  output = `#{SMARTCTL} -H #{device_path}`
  @smart_healthy = !output.scan(/PASSED/).empty?
  @health_output = output
end

#check_smart_capability!Object

Checks if disk is capable



67
68
69
70
71
72
# File 'lib/disk_reporter/disk_handler.rb', line 67

def check_smart_capability!
  output = `#{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_pathObject



12
13
14
# File 'lib/disk_reporter/disk_handler.rb', line 12

def device_path
  "/dev/#{name}"
end

#get_smart_infoObject



39
40
41
# File 'lib/disk_reporter/disk_handler.rb', line 39

def get_smart_info
  `#{SMARTCTL} -i /dev/#{name}`
end

#parse_smart_infoObject

Parses SMART drive info



59
60
61
62
63
64
# File 'lib/disk_reporter/disk_handler.rb', line 59

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_partitionsObject



79
80
81
82
83
84
# File 'lib/disk_reporter/disk_handler.rb', line 79

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_numberObject



16
17
18
# File 'lib/disk_reporter/disk_handler.rb', line 16

def serial_number
 number.strip
end

#smart_capable?Boolean

Is the device SMART capable and enabled

Returns:

  • (Boolean)


45
46
47
# File 'lib/disk_reporter/disk_handler.rb', line 45

def smart_capable?
  @smart_available && @smart_enabled
end

#to_hObject



74
75
76
77
# File 'lib/disk_reporter/disk_handler.rb', line 74

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

#wnnObject



20
21
22
# File 'lib/disk_reporter/disk_handler.rb', line 20

def wnn
  id.delete(' ') unless id.nil?
end