#!/usr/bin/perl -w # -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*- # # sa(8) analysis (summarise accounting information summaries) # Version 1.0 # # Copyright (c) 2002 by Ian Hickson # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # use strict; my $secsPerDay = 24000; # XXX hardcoded my $numDays = scalar(@ARGV); my $max = $numDays * $secsPerDay; my %processes = (); my $totalcpu = 0; my $totalcount = 0; while (<>) { chomp; if (m/^.+ (.+) cpu (.+)k mem ...... .. (.+)[* ]$/) { if (exists $processes{$3}) { $processes{$3}->{cpu} += $1; $processes{$3}->{count}++; } else { $processes{$3}->{cpu} = 0+$1; $processes{$3}->{count} = 1; } $totalcpu += $1; $totalcount++; } else { print STDERR "$ARGV:$.: $_\n"; } } print "Process CPU seconds user machine count average\n"; foreach (sort { $processes{$b}->{cpu} <=> $processes{$a}->{cpu} } keys %processes) { my $name = $_; my $cpu = $processes{$_}->{cpu}; my $count = $processes{$_}->{count}; my $p1 = $cpu / $totalcpu * 100; my $p2 = $cpu / $max * 100; my $avg = $cpu / $count; format STDOUT = @<<<<<<<<<<<<<<<<<< @######.#### @###.###% @###.###% @>>>>>> @###.### $name, $cpu, $p1, $p2, $count, $avg . write; } print "----------------------------------------------------------------------\n"; my $fraction = $totalcpu / $max * 100; my $avgPerDay = $totalcpu / $numDays; format footer = Total: @######.#### 100.000% @###.###% @>>>>>> $totalcpu, $fraction, $totalcount Average per day: @######.#### @>>> days $avgPerDay, $numDays . $~ = 'footer'; write; print "CPU percentage assumes $secsPerDay cpu seconds per day total.\n";