Class: DiskHandler::Disk
- Inherits:
-
Object
- Object
- DiskHandler::Disk
- Defined in:
- lib/disk_reporter/disk_handler.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#model ⇒ Object
Returns the value of attribute model.
-
#name ⇒ Object
Returns the value of attribute name.
-
#number ⇒ Object
Returns the value of attribute number.
-
#partitions ⇒ Object
Returns the value of attribute partitions.
-
#size ⇒ Object
Returns the value of attribute size.
-
#state ⇒ Object
Returns the value of attribute state.
-
#type ⇒ Object
Returns the value of attribute type.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #attrs_from_line(lsblk_line) ⇒ Object
-
#check_health! ⇒ Object
Check the SMART health.
-
#check_smart_capability! ⇒ Object
Checks if disk is capable.
- #device_path ⇒ Object
- #get_smart_info ⇒ Object
-
#initialize(lsblk_line) ⇒ Disk
constructor
A new instance of Disk.
-
#parse_smart_info ⇒ Object
Parses SMART drive info.
- #populate_partitions ⇒ Object
- #serial_number ⇒ Object
-
#smart_capable? ⇒ Boolean
Is the device SMART capable and enabled.
- #to_h ⇒ Object
- #wnn ⇒ Object
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
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def id @id end |
#model ⇒ Object
Returns the value of attribute model.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def model @model end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def name @name end |
#number ⇒ Object
Returns the value of attribute number.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def number @number end |
#partitions ⇒ Object
Returns the value of attribute partitions.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def partitions @partitions end |
#size ⇒ Object
Returns the value of attribute size.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def size @size end |
#state ⇒ Object
Returns the value of attribute state.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def state @state end |
#type ⇒ Object
Returns the value of attribute type.
10 11 12 |
# File 'lib/disk_reporter/disk_handler.rb', line 10 def type @type end |
#version ⇒ Object
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_path ⇒ Object
12 13 14 |
# File 'lib/disk_reporter/disk_handler.rb', line 12 def device_path "/dev/#{name}" end |
#get_smart_info ⇒ Object
39 40 41 |
# File 'lib/disk_reporter/disk_handler.rb', line 39 def get_smart_info `#{SMARTCTL} -i /dev/#{name}` end |
#parse_smart_info ⇒ Object
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_partitions ⇒ Object
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_number ⇒ Object
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
45 46 47 |
# File 'lib/disk_reporter/disk_handler.rb', line 45 def smart_capable? @smart_available && @smart_enabled end |
#to_h ⇒ Object
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 |
#wnn ⇒ Object
20 21 22 |
# File 'lib/disk_reporter/disk_handler.rb', line 20 def wnn id.delete(' ') unless id.nil? end |