Class: SystemUtils
- Inherits:
-
Object
- Object
- SystemUtils
- Defined in:
- lib/system_utils.rb
Class Method Summary collapse
- .bandwidth_usage ⇒ Object
- .cpu_load_average ⇒ Object
- .cpu_usage_percent ⇒ Object
- .disk_io_stats ⇒ Object
- .filesystem_usage_percent ⇒ Object
- .memory_usage_percent ⇒ Object
Class Method Details
.bandwidth_usage ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/system_utils.rb', line 73 def bandwidth_usage prev_stats = read_network_stats sleep(1) curr_stats = read_network_stats incoming_bytes = curr_stats[:rx_bytes] - prev_stats[:rx_bytes] outgoing_bytes = curr_stats[:tx_bytes] - prev_stats[:tx_bytes] bytes_to_mbits = 8.0 / 1_000_000 # Convert bytes to megabits { incoming_mbps: (incoming_bytes * bytes_to_mbits).round(2), outgoing_mbps: (outgoing_bytes * bytes_to_mbits).round(2) } end |
.cpu_load_average ⇒ Object
30 31 32 33 34 |
# File 'lib/system_utils.rb', line 30 def cpu_load_average File.read("#{proc_path}/loadavg").split.take(3) .map(&:to_f) .map { |value| value.round(2) } end |
.cpu_usage_percent ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/system_utils.rb', line 7 def cpu_usage_percent prev_cpu = read_cpu_stat sleep(1) current_cpu = read_cpu_stat prev_idle = prev_cpu[:idle] + prev_cpu[:iowait] idle = current_cpu[:idle] + current_cpu[:iowait] prev_non_idle = prev_cpu[:user] + prev_cpu[:nice] + prev_cpu[:system] + prev_cpu[:irq] + prev_cpu[:softirq] + prev_cpu[:steal] non_idle = current_cpu[:user] + current_cpu[:nice] + current_cpu[:system] + current_cpu[:irq] + current_cpu[:softirq] + current_cpu[:steal] prev_total = prev_idle + prev_non_idle total = idle + non_idle total_diff = total - prev_total idle_diff = idle - prev_idle cpu_percentage = ((total_diff - idle_diff).to_f / total_diff * 100).round(2) [cpu_percentage, 100.0].min.round(2) end |
.disk_io_stats ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/system_utils.rb', line 55 def disk_io_stats prev_stats = read_disk_stats sleep(1) curr_stats = read_disk_stats read_sectors = curr_stats[:read_sectors] - prev_stats[:read_sectors] write_sectors = curr_stats[:write_sectors] - prev_stats[:write_sectors] sector_size = 512 read_mb_per_sec = (read_sectors * sector_size / 1_048_576.0).round(2) write_mb_per_sec = (write_sectors * sector_size / 1_048_576.0).round(2) { read_mb_per_sec:, write_mb_per_sec: } end |
.filesystem_usage_percent ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/system_utils.rb', line 46 def filesystem_usage_percent stat = Sys::Filesystem.stat(root_path) total_blocks = stat.blocks available_blocks = stat.blocks_available used_blocks = total_blocks - available_blocks used_percent = (used_blocks.to_f / total_blocks * 100).round(2) [used_percent, 100.0].min.round(2) end |
.memory_usage_percent ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/system_utils.rb', line 36 def memory_usage_percent mem_info = File.read("#{proc_path}/meminfo") total = mem_info.match(/MemTotal:\s+(\d+)/)[1].to_f free = mem_info.match(/MemFree:\s+(\d+)/)[1].to_f buffers = mem_info.match(/Buffers:\s+(\d+)/)[1].to_f cached = mem_info.match(/Cached:\s+(\d+)/)[1].to_f used = total - free - buffers - cached (used / total * 100).round(2) end |