Module: LinuxStat::Memory

Defined in:
lib/linux_stat/memory.rb

Class Method Summary collapse

Class Method Details

.availableObject

Returns the total memory details reported by /proc/meminfo. The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil



50
51
52
53
# File 'lib/linux_stat/memory.rb', line 50

def available
	return nil unless meminfo?
	IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
end

.percent_availableObject

Returns the percentage of memory used reported by /proc/meminfo.

It retuns an Integer but if the info is not available, it will return nil



81
82
83
84
85
# File 'lib/linux_stat/memory.rb', line 81

def percent_available
	return nil unless meminfo?
	memory = IO.foreach('/proc/meminfo').first(3)
	memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
end

.percent_usedObject

Returns the percentage of memory used reported by /proc/meminfo.

It retuns an Integer but if the info is not available, it will return nil



70
71
72
73
74
75
# File 'lib/linux_stat/memory.rb', line 70

def percent_used
	return nil unless meminfo?
	memory = IO.foreach('/proc/meminfo').first(3)
	total = memory[0].split[1].to_i
	total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
end

.statObject

Returns the memory details reported by /proc/meminfo. In this format:

{:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}

The values are in Kilobyte.

If the statistics is not available, it will return an empty Hash.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/linux_stat/memory.rb', line 11

def stat
	return {} unless meminfo?

	memory = IO.foreach('/proc/meminfo').first(3)

	total = memory[0].split[1].to_i
	available = memory[2].split[1].to_i
	used = total - available

	percent_used = total == 0 ? 0.0 : used.*(100).fdiv(total).round(2)
	percent_available = total == 0 ? 0.0 : available.*(100).fdiv(total).round(2)

	# We have all the methods, but each methods reads the same file
	#
	# So better to use the above calculation
	{
		total: total,
		used: used,
		available: available,
		percent_used: percent_used,
		percent_available: percent_available
	}
end

.totalObject

Returns the total memory details reported by /proc/meminfo. The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil.



40
41
42
43
# File 'lib/linux_stat/memory.rb', line 40

def total
	return nil unless meminfo?
	IO.foreach('/proc/meminfo').first.split[1].to_i
end

.usedObject

Returns the amount of memory used reported by /proc/meminfo. The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil.



60
61
62
63
64
# File 'lib/linux_stat/memory.rb', line 60

def used
	return nil unless meminfo?
	memory = IO.foreach('/proc/meminfo').first(3)
	memory[0].split[1].to_i - memory[2].split[1].to_i
end