Module: LinuxStat::Swap
- Defined in:
- lib/linux_stat/swap.rb
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.
-
.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
Show 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.
18 19 20 |
# File 'lib/linux_stat/swap.rb', line 18 def any? !!IO.foreach('/proc/swaps').drop(1).first 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.
59 60 61 62 63 |
# File 'lib/linux_stat/swap.rb', line 59 def available return nil unless swaps_readable? values_t = read_usage values_t[0].sum - values_t[1].sum end |
.list ⇒ Object
List all swap devices and returns a Hash. If the info isn’t available, it will return an empty Hash.
6 7 8 9 10 11 12 13 14 |
# File 'lib/linux_stat/swap.rb', line 6 def list return {} unless swaps_readable? file = IO.readlines('/proc/swaps').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.
89 90 91 92 93 94 95 96 97 |
# File 'lib/linux_stat/swap.rb', line 89 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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/linux_stat/swap.rb', line 76 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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/linux_stat/swap.rb', line 27 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
Show 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.
50 51 52 53 |
# File 'lib/linux_stat/swap.rb', line 50 def total return nil unless swaps_readable? read_usage[0].sum 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.
69 70 71 72 |
# File 'lib/linux_stat/swap.rb', line 69 def used return nil unless swaps_readable? read_usage[-1].sum end |