#!/bin/env ruby -w # mpstatavg - Average the output of the Solaris mpstat(1M) command # # Tim.Cook@sun.com, 5 Feb 2008 PROG = __FILE__.split('/')[-1] state = 0 done_header = false cpus = 0 date = nil sums = Array.new(15) #-- The classic state machine ARGF.each_line do |line| next if line == "\n" f = line.split if line =~ /^[A-Z][a-z][a-z] [A-Z][a-z][a-z]\s+\d/ then #-- Allow for a date (ctime format) at the start of any line date = f[0..4] cpu = f[5] values = f[6..999] else cpu = f[0] values = f[1..999] end case state when 0 sums.fill(0) #-- Set all values in sums to 0 if cpu == "CPU" then #-- Good, that is what we were expecting if ! done_header then line.sub!("CPU ", "CPUs ") print line done_header = true end cpus = 0 state = 1 else #-- Weren't expecting that STDERR.print PROG, ": SNAFU - ", values.join(","), "\n" exit 1 end when 1 if cpu == "CPU" then #-- Need to print averages if ! date.nil? then print date.join(" ") end print " ", cpus sums.each { |x| print " ", x / cpus } print "\n" #-- Reset sums.fill(0) cpus = 0; else values.each_index { |x| sums[x] = sums[x].to_f + values[x].to_f } cpus = cpus + 1 end end end if cpus > 0 then if ! date.nil? then print date.join(" ") end print " ", cpus sums.each { |x| print " ", x / cpus } print "\n" end