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
-
.any? ⇒ Boolean
Returns true if any swap device is available, else returns false.
-
.available ⇒ Object
Show total amount of available swap.
-
.free ⇒ Object
Shows free swap.
-
.list ⇒ Object
List all swap devices and returns a Hash.
-
.percent_available ⇒ Object
Shows the percentage of swap available.
-
.percent_used ⇒ Object
Show percentage of swap used.
-
.stat ⇒ Object
Show aggregated used and available swap.
-
.total ⇒ Object
Shows total amount of swap.
-
.used ⇒ Object
Show total amount of used swap.
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.
24 25 26 |
# File 'lib/linux_stat/swap.rb', line 24 def any? !!IO.foreach('/proc/swaps'.freeze).first(2)[1] end |
.available ⇒ Object
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.
81 82 83 84 85 |
# File 'lib/linux_stat/swap.rb', line 81 def available return nil unless swaps_readable? values_t = read_usage values_t[0].sum - values_t[1].sum end |
.free ⇒ Object
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.
70 71 72 73 |
# File 'lib/linux_stat/swap.rb', line 70 def free v = LinuxStat::Sysinfo.freeswap v ? v.fdiv(1024).to_i : nil end |
.list ⇒ Object
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_available ⇒ Object
Shows the percentage of swap available.
The return type is a Float but if the info isn’t available, it will return nil.
116 117 118 119 120 121 122 123 124 |
# File 'lib/linux_stat/swap.rb', line 116 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_used ⇒ Object
Show percentage of swap used.
The return type is a Float but if the info isn’t available, it will return nil.
102 103 104 105 106 107 108 109 110 |
# File 'lib/linux_stat/swap.rb', line 102 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 |
.stat ⇒ Object
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 |
# File 'lib/linux_stat/swap.rb', line 34 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 |
.total ⇒ Object
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.
59 60 61 62 |
# File 'lib/linux_stat/swap.rb', line 59 def total v = LinuxStat::Sysinfo.totalswap v ? v.fdiv(1024).to_i : nil end |
.used ⇒ Object
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.
93 94 95 96 |
# File 'lib/linux_stat/swap.rb', line 93 def used return nil unless swaps_readable? read_usage[-1].sum end |