use process accounting to display summary of process, user and tty usage
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 2 May 2009 14:40:15 +0000 (14:40 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 2 May 2009 14:40:15 +0000 (14:40 +0000)
git-svn-id: svn://svn.rot13.org/sysadmin-cookbook@38 191e9f34-6774-4a6d-acfc-7664dacd4a2a

recepies/acct/0.install [new file with mode: 0644]
recepies/acct/lastcomm-duration.pl [new file with mode: 0755]

diff --git a/recepies/acct/0.install b/recepies/acct/0.install
new file mode 100644 (file)
index 0000000..5338d43
--- /dev/null
@@ -0,0 +1 @@
+root@opl:/srv/sysadmin-cookbook/recepies/acct# apt-get -f install acct
diff --git a/recepies/acct/lastcomm-duration.pl b/recepies/acct/lastcomm-duration.pl
new file mode 100755 (executable)
index 0000000..f0d1dc0
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use YAML;
+
+my $too_long = 0.5; # s
+my $stats;
+
+open(my $lastcomm, '-|', 'lastcomm');
+while(<$lastcomm>) {
+       chomp;
+       if ( m{^(\S+).+?(\S+)\s+(\S+)\s+(\d+\.\d+) secs} ) {
+               my ( $command, $user, $tty, $duration ) = ( $1, $2, $3, $4 );
+               $stats->{command}->{$command} += $duration;
+               $stats->{user}->{$user} += $duration;
+               $stats->{tty}->{$tty} += $duration;
+               print "$_\n" if $duration > $too_long;
+       } else {
+               warn "# $_";
+       }
+}
+
+foreach my $stat ( keys %$stats ) {
+       print "\n$stat:\n";
+       my $counter = $stats->{$stat};
+       foreach my $name ( sort { $counter->{$b} <=> $counter->{$a} } keys %$counter ) {
+               printf "%8.2f %s\n", $counter->{$name}, $name;
+       }
+}