Top Level Namespace
Defined Under Namespace
Classes: HardwareCategory, LinuxSystemMemory, LinuxSystemNetwork, LinuxSystemProcessors, MacSystemMemory, MacSystemNetwork, MacSystemProcessors, Probench, SystemMemory, SystemNetwork, SystemProcessors, WindowsSystemMemory, WindowsSystemNetwork, WindowsSystemProcessors
Instance Method Summary collapse
-
#cpuinfo ⇒ Object
converts /proc/cpuinfo into a hash.
-
#lscpu ⇒ Object
converts lscpu output into hash.
-
#sysctl(category, noprefix = true) ⇒ Object
convert output of sysctl to a hash noprefix to false to not remove key prefixes i.e.
- #wmic(category) ⇒ Object
Instance Method Details
#cpuinfo ⇒ Object
converts /proc/cpuinfo into a hash
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/linux_wrappers.rb', line 19 def cpuinfo hash = {} IO.popen('cat /proc/cpuinfo') do |io| while (line = io.gets) key = line.split(':')[0] key = key.strip unless key.nil? val = line.split(':')[1] val = val.strip unless val.nil? hash[key] = val unless key.nil? || val.nil? end end hash end |
#lscpu ⇒ Object
converts lscpu output into hash
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/linux_wrappers.rb', line 4 def lscpu hash = {} IO.popen('lscpu') do |io| while (line = io.gets) key = line.split(':')[0] key = key.strip unless key.nil? val = line.split(':')[1] val = val.strip unless val.nil? hash[key] = val unless key.nil? || val.nil? end end hash end |
#sysctl(category, noprefix = true) ⇒ Object
convert output of sysctl to a hash noprefix to false to not remove key prefixes i.e. machdep.cpu etc
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mac_wrappers.rb', line 5 def sysctl(category, noprefix = true) raise ArgumentError unless category.is_a?(HardwareCategory) cmd = 'sysctl -a' prefixes = [] # noinspection RubyResolve case category when HardwareCategory::PROCESSORS cmd += " | egrep '(hw|cpu)'" prefixes = %w[machdep.cpu. hw.] when HardwareCategory::MEMORY cmd += ' | grep machdep.mem' prefixes = ['machdep.mem.'] else puts 'none of the above' end hash = {} IO.popen(cmd) do |io| while (line = io.gets) key = line.split(':')[0] key = key.strip unless key.nil? next if key.nil? if noprefix && !key.nil? prefixes.each do |prefix| # noinspection RubyNilAnalysis key = key[prefix.length..-1] if key.start_with?(prefix) && noprefix end end val = line.split(':')[1] val = val.strip unless val.nil? hash[key] = val unless key.nil? || val.nil? end end hash end |
#wmic(category) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/windows_wrappers.rb', line 3 def wmic(category) raise ArgumentError unless category.is_a?(HardwareCategory) cmd = 'wmic ' # noinspection RubyResolve case category when HardwareCategory::PROCESSORS cmd += 'cpu' when HardwareCategory::MEMORY cmd += 'memory' else raise ArgumentError 'Bad category' end lines = [] IO.popen(cmd) do |io| while (line = io.gets) lines << line end end keys_str = lines[0] vals_str = lines[2] hash = {} index = -1 key = nil val = nil while index < keys_str.size - 1 index += 1 lookahead = index < keys_str.size - 2 ? keys_str[index + 1] : nil # transiting to inside when space turns into nonspace if keys_str[index] == ' ' && lookahead != ' ' # noinspection RubyNilAnalysis key = key.strip # noinspection RubyNilAnalysis val = val.strip hash[key] = val unless val.nil? || val.size.zero? key = nil val = nil elsif key.nil? && val.nil? key = keys_str[index] val = vals_str[index] elsif !key.nil? && !val.nil? key << keys_str[index] val << vals_str[index] end end # presuming multiple processors add extra lines hash['count'] = lines.size - 5 hash end |