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.
19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def id @id end |
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def model @model end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def name @name end |
#number ⇒ Object
Returns the value of attribute number.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def number @number end |
#partitions ⇒ Object
Returns the value of attribute partitions.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def partitions @partitions end |
#size ⇒ Object
Returns the value of attribute size.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def size @size end |
#state ⇒ Object
Returns the value of attribute state.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def state @state end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def type @type end |
#version ⇒ Object
Returns the value of attribute version.
5 6 7 |
# File 'lib/disk_reporter/disk_handler.rb', line 5 def version @version end |
Instance Method Details
#attrs_from_line(lsblk_line) ⇒ Object
27 28 29 30 31 32 |
# 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! ⇒ Object
Check the SMART health
46 47 48 49 50 |
# 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! ⇒ Object
Checks if disk is capable
62 63 64 65 66 67 |
# 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 ⇒ Object
7 8 9 |
# File 'lib/disk_reporter/disk_handler.rb', line 7 def device_path "/dev/#{name}" end |
#get_smart_info ⇒ Object
34 35 36 |
# File 'lib/disk_reporter/disk_handler.rb', line 34 def get_smart_info `smartctl -i /dev/#{name}` end |
#parse_smart_info ⇒ Object
Parses SMART drive info
54 55 56 57 58 59 |
# 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 ⇒ Object
74 75 76 77 78 79 |
# 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 ⇒ Object
11 12 13 |
# File 'lib/disk_reporter/disk_handler.rb', line 11 def serial_number number.strip end |
#smart_capable? ⇒ Boolean
Is the device SMART capable and enabled
40 41 42 |
# File 'lib/disk_reporter/disk_handler.rb', line 40 def smart_capable? @smart_available && @smart_enabled end |
#to_h ⇒ Object
69 70 71 72 |
# 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 ⇒ Object
15 16 17 |
# File 'lib/disk_reporter/disk_handler.rb', line 15 def wnn id.delete(' ') end |