| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
# |
| 4 |
# longontime - simple wtmp analyzer |
| 5 |
# written by Jan Engelhardt, 2004-2007 |
| 6 |
|
| 7 |
use Getopt::Long; |
| 8 |
use strict; |
| 9 |
|
| 10 |
my %Total; |
| 11 |
my $ARG_time; |
| 12 |
|
| 13 |
&Getopt::Long::Configure(qw(bundling)); |
| 14 |
&GetOptions("t" => \$ARG_time); |
| 15 |
|
| 16 |
open(IN, "last -ax |"); |
| 17 |
while (defined(my $l = <IN>)) { |
| 18 |
chomp $l; |
| 19 |
my($user, $dur) = ($l =~ /^(\w+)\s+.+?\((.*?)\)/); |
| 20 |
if ($user eq "") { |
| 21 |
next; |
| 22 |
} |
| 23 |
|
| 24 |
my($h, $m) = split(/:/o, $dur); |
| 25 |
$Total{$user} += $h * 60 + $m; |
| 26 |
} |
| 27 |
close IN; |
| 28 |
|
| 29 |
if ($ARG_time) { |
| 30 |
# Time sort |
| 31 |
foreach my $u (sort { $Total{$b} <=> $Total{$a} } keys %Total) { |
| 32 |
printf "%-8s: %3ud %2uh %2um (%5um)\n", |
| 33 |
$u, int($Total{$u} / 1440), int($Total{$u} / 60 % 24), |
| 34 |
int($Total{$u} % 60), $Total{$u}; |
| 35 |
} |
| 36 |
} else { |
| 37 |
# Alphabetical sort |
| 38 |
foreach my $u (sort keys %Total) { |
| 39 |
printf "%-8s: %3ud %2uh %2um (%5um)\n", |
| 40 |
$u, int($Total{$u} / 86400), |
| 41 |
int($Total{$u} / 60 % 86400), int($Total{$u} % 60), |
| 42 |
$Total{$u}; |
| 43 |
} |
| 44 |
} |