#!/usr/bin/env ruby require 'cpu' require 'tins/go' include Tins::GO require 'more_math' class ParseCommands def self.parse(argv) new(argv).parse end def initialize(argv) @argv = argv end def set_t_j_max(temp) t_j_max = temp and CPU.t_j_max = t_j_max.to_i end def create_history_path(filename) filename ||= '~/.coretemp-history' File.expand_path(filename) end def measure_data(opts) set_t_j_max opts['t'] CPU.usage(@duration || 1) end def simple opts = go 't', @argv puts measure_data(opts).map(&:temperature) * ' ' end def collect opts = go 'tfn:', @argv @duration = (opts['n'] || 1).to_f filename = create_history_path opts['f'] begin temperatures = measure_data(opts).map(&:temperature) line = ([ Time.now.to_f ] + temperatures) * ' ' if filename open(filename, 'a') do |o| o.puts line end end end while @duration > 0 end def clear opts = go 'f', @argv filename = create_history_path opts['f'] if filename and File.exist?(filename) File.unlink(filename) end end def stats opts = go 'f', @argv filename = create_history_path opts['f'] if filename and File.exist?(filename) history = open(filename, 'r') { |o| o.map do |line| time, *temps = line.split(/\s+/) [ Time.at(Float(time)) ] + temps.map { |t| Integer(t) } end } measurements_last = history.last[1..-1] times, *measurements = history.transpose sequences = measurements.map { |m| MoreMath::Sequence.new(m) } puts " min: %s" % (sequences.map { |a| a.min } * ' ') puts "last: %s" % (measurements_last * ' ') puts " max: %s" % (sequences.map { |a| a.max } * ' ') puts "mean: %s" % (sequences.map { |a| a.mean.round } * ' ') end end def parse case command = @argv.shift when 'collect' then collect when 'clear' then clear when 'stats' then stats else simple end end end ParseCommands.parse(ARGV)