module FartSystem
FartSystem — free RAM, swap, load-average, uptime, and CPU ping readers for !fart replies. Public: parse_meminfo, free_ram_kb, free_swap_kb, used_swap_kb, swap_total_kb, swap_percent, format_swap, human_ram, parse_loadavg, format_loadavg, parse_uptime, format_uptime, snapshot Depends: FartCpuPing Tests: test/lib/fart_system_test.rb, test/plugins/fart_test.rb
Public Instance Methods
Source
# File lib/fart_system.rb, line 110 def default_readers { meminfo: -> { File.read("/proc/meminfo") }, loadavg: -> { File.read("/proc/loadavg") }, uptime: -> { File.read("/proc/uptime") }, cpu_ping: -> { FartCpuPing.sample_ns } } end
Source
# File lib/fart_system.rb, line 71 def format_loadavg(load) return nil unless load "#{load[:load1]} #{load[:load5]} #{load[:load15]}" end
Source
# File lib/fart_system.rb, line 48 def format_swap(info) total = swap_total_kb(info) return nil unless total.positive? "#{human_ram(used_swap_kb(info))} #{swap_percent(info)}%" end
Source
# File lib/fart_system.rb, line 81 def format_uptime(seconds) total = seconds.to_f.round days = total / 86_400 hours = (total % 86_400) / 3600 minutes = (total % 3600) / 60 parts = [] parts << "#{days}d" if days.positive? parts << "#{hours}h" if days.positive? || hours.positive? parts << "#{minutes}m" parts.join(" ") end
Source
# File lib/fart_system.rb, line 22 def free_ram_kb(info) available = info["MemAvailable"] || info["MemFree"] available.to_i end
Source
# File lib/fart_system.rb, line 27 def free_swap_kb(info) info["SwapFree"].to_i end
Source
# File lib/fart_system.rb, line 55 def human_ram(kilobytes) bytes = kilobytes.to_i * 1024 if bytes >= 1_073_741_824 format("%.1fG", bytes / 1_073_741_824.0) else format("%.1fM", bytes / 1_048_576.0) end end
Source
# File lib/fart_system.rb, line 64 def parse_loadavg(text) fields = text.to_s.strip.split return nil if fields.length < 3 { load1: fields[0], load5: fields[1], load15: fields[2] } end
Source
# File lib/fart_system.rb, line 14 def parse_meminfo(text) text.to_s.each_line.with_object({}) do |line, info| next unless line =~ /^(\w+):\s+(\d+)/ info[Regexp.last_match(1)] = Regexp.last_match(2).to_i end end
Source
# File lib/fart_system.rb, line 77 def parse_uptime(text) text.to_s.split.first.to_f end
Source
# File lib/fart_system.rb, line 94 def snapshot(readers: default_readers) meminfo = parse_meminfo(readers.fetch(:meminfo).call) free_kb = free_ram_kb(meminfo) load = parse_loadavg(readers.fetch(:loadavg).call) uptime_sec = parse_uptime(readers.fetch(:uptime).call) { free_ram: human_ram(free_kb), loadavg: format_loadavg(load), uptime: format_uptime(uptime_sec), swap: format_swap(meminfo), cpu_ns: readers.fetch(:cpu_ping).call } rescue StandardError { free_ram: nil, loadavg: nil, uptime: nil, swap: nil, cpu_ns: nil } end
Source
# File lib/fart_system.rb, line 41 def swap_percent(info) total = swap_total_kb(info) return 0 unless total.positive? (used_swap_kb(info) * 100.0 / total).round end
Source
# File lib/fart_system.rb, line 37 def swap_total_kb(info) info["SwapTotal"].to_i end
Source
# File lib/fart_system.rb, line 31 def used_swap_kb(info) total = swap_total_kb(info) free = free_swap_kb(info) [total - free, 0].max end