examples for lxc.network.hwaddr, lxc.network.veth.pair and lxc.mount.entry
[sysadmin-cookbook] / recepies / acct / lastcomm-duration.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use YAML;
7
8 my $too_long = shift @ARGV || 0.5; # s
9 my $stats;
10
11 open(my $lastcomm, '-|', 'lastcomm');
12 while(<$lastcomm>) {
13         chomp;
14         if ( m{^(\S+).+?(\S+)\s+(\S+)\s+(\d+\.\d+) secs} ) {
15                 my ( $command, $user, $tty, $duration ) = ( $1, $2, $3, $4 );
16                 $stats->{command}->{$command} += $duration;
17                 $stats->{user}->{$user} += $duration;
18                 $stats->{tty}->{$tty} += $duration;
19                 print "$_\n" if $duration > $too_long;
20         } else {
21                 warn "# $_";
22         }
23 }
24
25 foreach my $stat ( keys %$stats ) {
26         print "\n$stat:\n";
27         my $counter = $stats->{$stat};
28         foreach my $name ( sort { $counter->{$b} <=> $counter->{$a} } keys %$counter ) {
29                 my $d = $counter->{$name};
30                 printf "%8.2f %s\n", $d, $name if $d > $too_long;
31         }
32 }