Module: LinuxStat::Swap

Defined in:
lib/linux_stat/swap.rb

Overview

Shows various Swap devices related information of the current system.

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)


24
25
26
# File 'lib/linux_stat/swap.rb', line 24

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.



84
85
86
87
88
89
90
# File 'lib/linux_stat/swap.rb', line 84

def available
	return nil unless swaps_readable?
	values_t = read_usage
	t = values_t[0].reduce(:+)
	u = values_t[1].reduce(:+)
	(t && u) ? t - u : nil
end

.freeObject

Shows free swap.

The value is in kilobytes.

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



73
74
75
76
# File 'lib/linux_stat/swap.rb', line 73

def free
	v = LinuxStat::Sysinfo.freeswap
	v ? v.fdiv(1024).to_i : nil
end

.listObject

List all swap devices and returns a Hash.

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



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

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.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/linux_stat/swap.rb', line 127

def percent_available
	return nil unless swaps_readable?
	values_t = read_usage

	_total = values_t[0]
	_used = values_t[-1]
	return nil if _total.empty? || _used.empty?

	total, used = _total.reduce(:+), _used.reduce(:+)

	return 0.0 if total == 0

	total.-(used).*(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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/linux_stat/swap.rb', line 107

def percent_used
	return nil unless swaps_readable?
	values_t = read_usage

	_total = values_t[0]
	_used = values_t[-1]
	return nil if _total.empty? || _used.empty?

	total = _total.reduce(:+)
	used = _used.reduce(:+)

	return 0.0 if total == 0

	used.*(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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/linux_stat/swap.rb', line 34

def stat
	return {} unless swaps_readable?
	values_t = read_usage

	_total, _used = values_t[0], values_t[-1]
	return {} if _total.empty? || _used.empty?

	total, used = _total.reduce(:+), _used.reduce(:+)
	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.



62
63
64
65
# File 'lib/linux_stat/swap.rb', line 62

def total
	v = LinuxStat::Sysinfo.totalswap
	v ? v.fdiv(1024).to_i : nil
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.



98
99
100
101
# File 'lib/linux_stat/swap.rb', line 98

def used
	return nil unless swaps_readable?
	read_usage[-1].reduce(:+)
end