#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my $mailto = 'linerva-root@mit.edu'; my $ssh = 0; GetOptions("ssh" => \$ssh, "mailto=s" => \$mailto) or die ("Bad arguments\n"); my (undef,undef,undef,undef,$mon,undef,undef,undef,undef) = localtime(time); $mon--; $mon = 11 if $mon < 0; my @months = qw(January February March April May June July August September October November December); $mon = $months[$mon]; # last -f /var/log/wtmp.1 | cut -f 1 -d ' ' | sort -nr | uniq -c | sort -nr my $pipe; if($ssh) { open($pipe, "-|", "ssh", "linerva.mit.edu", "last -f /var/log/wtmp.1 | cut -f 1 -d ' ' | sort -nr | uniq -c"); } else { open($pipe, "-|", "last -f /var/log/wtmp.1 | cut -f 1 -d ' ' | sort -nr | uniq -c | sort -nr"); } my $mail; open($mail, "|-", '/usr/bin/mutt', '-x', $mailto, '-s', "Linerva usage for $mon"); my %users; my %buckets = ( 1 => 0, 10 => 0, 100 => 0, 1000 => 0); print $mail "Linerva usage for $mon:\n\n"; while(defined(my $line = <$pipe>)) { chomp($line); $line =~ s/^\s+//; $line =~ s/\s+$//; my ($count, $who) = split /\s/, $line; next unless $who; for my $i (1,10,100,1000) { $buckets{$i}++ if $count >= $i; } $users{$who} = $count; } close($pipe); print $mail < $users{$a} || $a cmp $b} keys %users; for my $u (@users) { printf $mail '% 6d %s', $users{$u}, $u; print $mail "\n"; } close($mail);