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.



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

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def id
  @id
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def model
  @model
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def name
  @name
end

#numberObject

Returns the value of attribute number.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def number
  @number
end

#partitionsObject

Returns the value of attribute partitions.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def partitions
  @partitions
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def size
  @size
end

#stateObject

Returns the value of attribute state.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def state
  @state
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/disk_reporter/disk_handler.rb', line 5

def type
  @type
end

#versionObject

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_pathObject



7
8
9
# File 'lib/disk_reporter/disk_handler.rb', line 7

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

#get_smart_infoObject



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

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

#parse_smart_infoObject

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_partitionsObject



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_numberObject



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

Returns:

  • (Boolean)


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

def smart_capable?
  @smart_available && @smart_enabled
end

#to_hObject



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

#wnnObject



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

def wnn
  id.delete(' ')
end