Skip to content

Instantly share code, notes, and snippets.

@chockenberry
Created April 8, 2026 18:12
Show Gist options
  • Select an option

  • Save chockenberry/c7641e07a9114fc0aa9088066d3ba657 to your computer and use it in GitHub Desktop.

Select an option

Save chockenberry/c7641e07a9114fc0aa9088066d3ba657 to your computer and use it in GitHub Desktop.
A simple Ruby script to gather and display uptime, load average, memory usage, and network activity.
require 'time'
now = Time.now
boottime = `/usr/sbin/sysctl kern.boottime`
begin
timestamp = boottime.split(/\}/).last
boot = Time.parse(timestamp)
@days_uptime = ((now.to_i - boot.to_i) / (24 * 60 * 60)).to_s
@start_uptime = boot.strftime("%d.%b.%Y")
rescue
@days_uptime = 'Unknown'
@start_uptime = 'Unknown'
end
loadavg = `/usr/sbin/sysctl vm.loadavg`
begin
@load_average = loadavg.split[-3]
rescue
@load_average = 'Unknown'
end
vm_stat = `/usr/bin/vm_stat`
begin
lines = vm_stat.split(/$/)
free_pages = lines[1].split[-1].to_i
active_pages = lines[2].split[-1].to_i
inactive_pages = lines[3].split[-1].to_i
wired_pages = lines[4].split[-1].to_i
used_pages = wired_pages + active_pages
total_pages = free_pages + active_pages + inactive_pages + wired_pages
@memory_usage = sprintf('%.0f%%', (used_pages.to_f / total_pages.to_f) * 100.0)
rescue
@memory_usage = 'Unknown'
end
swapusage = `/usr/sbin/sysctl vm.swapusage`
begin
md = swapusage.match(/(\d+\.\d+)\D+(\d+\.\d+)\D+(\d+\.\d+)/)
swap_megabytes = md[2].to_f
@swap_usage = sprintf('%.2f GB', swap_megabytes / 1024)
rescue
@swap_usage = 'Unknown'
end
netstat = `/usr/sbin/netstat -I en0 -b`
begin
lines = netstat.split(/$/)
link = lines[1].split
link_bytes_in = link[-5].to_f rescue 0
link_bytes_out = link[-2].to_f rescue 0
@link_in = sprintf('%.2f GB', link_bytes_in / 1024 / 1024 / 1024)
@link_out = sprintf('%.2f GB', link_bytes_out / 1024 / 1024 / 1024)
rescue
@link_in = 'Unknown'
@link_out = 'Unknown'
end
puts "Uptime: " + @days_uptime + " days (since " + @start_uptime + ")"
puts "Load: " + @load_average
puts "Memory: " + @memory_usage + " (" + @swap_usage + " swapped)"
puts "Network: " + @link_in + " in, " + @link_out + " out"
@chockenberry
Copy link
Copy Markdown
Author

It generates a short summary like this:

Uptime:  11 days (since 27.Mar.2026)
Load:    1.66
Memory:  50% (0.00 GB swapped)
Network: 8.18 GB in, 0.67 GB out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment