Module: LinuxStat::Swap

Defined in:
lib/linux_stat/swap.rb

Class Method Summary collapse

Class Method Details

.any?Boolean

Returns true if any swap device is available, else returns false.

If the info isn’t available, it will return an empty Hash.

Returns:

  • (Boolean)


22
23
24
# File 'lib/linux_stat/swap.rb', line 22

def any?
	!!IO.foreach('/proc/swaps'.freeze).first(2)[1]
end

.availableObject

Show total amount of available swap.

The value is in kilobytes.

The return type is a Integer but if the info isn’t available, it will return nil.



68
69
70
71
72
# File 'lib/linux_stat/swap.rb', line 68

def available
	return nil unless swaps_readable?
	values_t = read_usage
	values_t[0].sum - values_t[1].sum
end

.listObject

List all swap devices and returns a Hash.

If the info isn’t available, it will return an empty Hash.



8
9
10
11
12
13
14
15
16
# File 'lib/linux_stat/swap.rb', line 8

def list
	return {} unless swaps_readable?

	file = IO.readlines('/proc/swaps'.freeze).drop(1)
	file.reduce({}) do |h, x|
		name, *stats = x.strip.split
		h.merge!(name => stats.map! { |v| v.to_i.to_s == v ? v.to_i : v.to_sym })
	end
end

.percent_availableObject

Shows the percentage of swap available.

The return type is a Float but if the info isn’t available, it will return nil.



103
104
105
106
107
108
109
110
111
# File 'lib/linux_stat/swap.rb', line 103

def percent_available
	return nil unless swaps_readable?
	values_t = read_usage

	total = values_t[0].sum
	return 0.0 if total == 0

	total.-(values_t[-1].sum).*(100).fdiv(total).round(2)
end

.percent_usedObject

Show percentage of swap used.

The return type is a Float but if the info isn’t available, it will return nil.



89
90
91
92
93
94
95
96
97
# File 'lib/linux_stat/swap.rb', line 89

def percent_used
	return nil unless swaps_readable?
	values_t = read_usage

	total = values_t[0].sum
	return 0.0 if total == 0

	values_t[-1].sum.*(100).fdiv(total).round(2)
end

.statObject

Show aggregated used and available swap.

The values are in kilobytes.

The return type is Hash. If the info isn’t available, the return type is an empty Hash.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/linux_stat/swap.rb', line 32

def stat
	return {} unless swaps_readable?
	values_t = read_usage

	total, used = values_t[0].sum, values_t[-1].sum
	available = total - used
	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
	{
		total: total,
		used: used,
		available: available,
		percent_used: percent_used,
		percent_available: percent_available
	}
end

.totalObject

Shows total amount of swap.

The value is in kilobytes.

The return type is a Integer but if the info isn’t available, it will return nil.



57
58
59
60
# File 'lib/linux_stat/swap.rb', line 57

def total
	return nil unless swaps_readable?
	read_usage[0].sum
end

.usedObject

Show total amount of used swap.

The value is in kilobytes.

The return type is a Integer but if the info isn’t available, it will return nil.



80
81
82
83
# File 'lib/linux_stat/swap.rb', line 80

def used
	return nil unless swaps_readable?
	read_usage[-1].sum
end