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)


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

def any?
	return nil unless File.readable?('/proc/swaps'.freeze)
	!!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.



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

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.



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

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
19
# 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.store(name, stats.map! { |v| LinuxStat::Misc.integer?(v) ? v.to_i : v.to_sym })
		h
	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.



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

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.



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

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.



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

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.



64
65
66
67
# File 'lib/linux_stat/swap.rb', line 64

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.



100
101
102
103
# File 'lib/linux_stat/swap.rb', line 100

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