Skip to content

Instantly share code, notes, and snippets.

@huned
Forked from visnup/uptime.rb
Created July 1, 2009 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huned/139167 to your computer and use it in GitHub Desktop.
Save huned/139167 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# calculate average uptime of my machine
require 'rubygems'
require 'activesupport'
times = `last | grep ^shutdown`
times = times.map { |l| Time.parse l[/shutdown\s+~\s+(.*)/, 1] }
times = times.map { |t| t > Time.now ? t - 1.year : t }
diffs = times.zip(times[1 .. -1]).map { |(a, b)| a - b if b }.compact
times[1 .. -1].zip(diffs).each do |(t, d)|
i = d/1.day
puts "#{t.to_s(:short)} #{'%5.1f' % i}d #{'=' * i}"
end
averagable = diffs.reject { |d| d < 1.day }
average = averagable.inject {|s, d| s + d} / averagable.length / 1.day
puts "average #{'%4.1f' % average}d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment