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



46
47
48
49
# File 'lib/linux_stat/memory.rb', line 46

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



74
75
76
77
78
# File 'lib/linux_stat/memory.rb', line 74

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



64
65
66
67
68
69
# File 'lib/linux_stat/memory.rb', line 64

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.



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

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.



37
38
39
40
# File 'lib/linux_stat/memory.rb', line 37

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.



55
56
57
58
59
# File 'lib/linux_stat/memory.rb', line 55

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