Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix run and make some features #6

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
53 changes: 30 additions & 23 deletions mytop
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
#
# $Id: mytop,v 1.4 2009/01/21 01:42:10 jzawodn Exp $

=pod

Expand Down Expand Up @@ -156,7 +154,7 @@ GetOptions(
"idle|i" => \$config{idle},
"resolve|r" => \$config{resolve},
"prompt!" => \$config{prompt},
"long|!" => \$config{long_nums},
"long!" => \$config{long_nums},
"mode|m=s" => \$config{mode},
"sort=s" => \$config{sort},
);
Expand Down Expand Up @@ -907,32 +905,21 @@ sub GetData()
## Threads
##

#my $sz = $width - 52;
my @sz = (9, 9, 15, 10, 9, 6);
my $used = scalar(@sz) + Sum(@sz);
my $free = $width - $used;

print BOLD();

printf "%8s %9s %15s %10s %9s %6s %-${free}s\n",
'Id','User','Host/IP','DB','Time', 'Cmd', 'Query or State';

print RESET();

## Id User Host DB
printf "%9s %9s %15s %10s %9s %6s %-${free}s\n",
'--','----','-------','--','----', '---', '----------';
my $proc_cmd = "show full processlist";
my @data = Hashes($proc_cmd);

$lines_left -= 2;

my $proc_cmd = "show full processlist";

my @data = Hashes($proc_cmd);
my $maxid = 0;

foreach my $thread (@data)
{
last if not $lines_left;

if ($maxid < $thread->{Id}) {
$maxid = $thread->{Id};
}

## Drop Domain Name, unless it looks like an IP address. If
## it's an IP, we'll strip the port number because it's rarely
## interesting.
Expand All @@ -955,7 +942,11 @@ sub GetData()
{
$thread->{Host} =~ s/:\d+$//;
my $host = gethostbyaddr(inet_aton($thread->{Host}), AF_INET);
$host =~ s/^([^.]+).*/$1/;
if (defined $host) {
$host =~ s/^([^.]+).*/$1/;
} else {
$host = $thread->{Host};
}
$thread->{Host} = $host;
}

Expand Down Expand Up @@ -993,6 +984,22 @@ sub GetData()

}

my $id_size = length $maxid;
my @sz = ($id_size, 9, 15, 10, 9, 6);
my $used = scalar(@sz) + Sum(@sz);
my $free = $width - $used;

print BOLD();

printf "%${id_size}s %9s %15s %10s %9s %6s %-${free}s\n",
'Id','User','Host/IP','DB','Time', 'Cmd', 'Query or State';

print RESET();

## Id User Host DB
printf "%${id_size}s %9s %15s %10s %9s %6s %-${free}s\n",
'--','----','-------','--','----', '---', '----------';

## Sort by idle time (closest thing to CPU usage I can think of).

my @sorted;
Expand Down Expand Up @@ -1047,7 +1054,7 @@ sub GetData()
print GREEN() if $thread->{Command} eq 'Connect';
}

printf "%9d %9.9s %15.15s %10.10s %9d %6.6s %-${free}.${free}s\n",
printf "%${id_size}d %9.9s %15.15s %10.10s %9d %6.6s %-${free}.${free}s\n",
$thread->{Id}, $thread->{User}, $thread->{Host}, $thread->{db},
$thread->{Time}, $thread->{Command}, $smInfo;

Expand Down