Changeset 276


Ignore:
Timestamp:
Nov 21, 2009, 4:34:13 PM (11 years ago)
Author:
zerodeux
Message:

Terminal size aware, limit itself to available columns. Override with usual COLUMNS env var

Location:
phptop
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • phptop/NEWS

    r269 r276  
    11phptop 0.4.1 - unreleased
     2
     3    * Now terminal size aware, overridable with COLUMNS env
    24
    35phptop 0.4 - 2009-11-14
  • phptop/TODO

    r265 r276  
    1 - Limit URI column width
    21- HTML output mode
    32- Man page, less verbose --help
  • phptop/debian/control

    r233 r276  
    88Package: phptop
    99Architecture: all
    10 Depends: perl (>= 5.8), libwww-perl, ${shlibs:Depends}, ${misc:Depends}
     10Depends: perl (>= 5.8), libwww-perl, libterm-size-perl, ${shlibs:Depends}, ${misc:Depends}
    1111Recommends: php5-cgi|libapache2-mod-php5|libapache2-mod-php5filter
    1212Description: php5 resource statistics tool
  • phptop/phptop

    r269 r276  
    2424use HTTP::Date;
    2525use POSIX;
     26use Term::Size;
    2627
    2728my $package_name    = 'phptop';
     
    6263
    6364Options:
    64   -c, --count N        Limit output to top N URIs (default is $count)
    65   -f, --full-query     Consider full URI with CGI parameters
     65  -c, --count N        Limit output to top N URLs (default is $count)
     66  -f, --full-query     Consider full URL with CGI parameters
    6667  -h, --help           Display this information
    6768  -l, --log path       Logfiles to parse, you may use several -l and wildcards
     
    115116        chomp;
    116117        next if not /\bphptop ([^ ]+) (.*)/;
    117         my ($uri, $pairs) = ($1, $2);
     118        my ($url, $pairs) = ($1, $2);
    118119
    119120        # Only keep records within expected time span. If a time stamp is not found or
     
    139140        next if @kv < 5;               # Protect against bogus/wrapped phptop records, count pairs
    140141
    141         # Fix URIs (depending on options)
    142         $uri =~ s/index\.php\/?//;  # / == /index.php (99.9% of the time)
    143         $uri =~ s/\?.*// if not defined $full_query;
    144         $uri =~ s/^https?:\/\/[^\/]+// if defined $path_only;
    145 
    146         # For each 'key:<numeric val>' sum the numeric value per URI
     142        # Fix URLs (depending on options)
     143        $url =~ s/index\.php\/?//;  # / == /index.php (99.9% of the time)
     144        $url =~ s/\?.*// if not defined $full_query;
     145        $url =~ s/^https?:\/\/[^\/]+// if defined $path_only;
     146
     147        # For each 'key:<numeric val>' sum the numeric value per URL
    147148        foreach (@kv) {
    148149            my ($key, $val) = split(/:/, $_);
     
    150151            # If we have a bogus key/value pair, zap the whole record
    151152            if ($key eq '' or not defined $val or not $val =~ /^[0-9.]+$/) {
    152                 delete $stat{$uri};
     153                delete $stat{$url};
    153154                $bogus++;
    154155                next LINE;
    155156            }
    156157
    157             $stat{$uri}{$key} += $val;
     158            $stat{$url}{$key} += $val;
    158159
    159160            # Compute max for some values (mem, inc)
    160161            if ($key eq 'mem') {
    161                 $stat{$uri}{'mem_max'} = $val if ($stat{$uri}{'mem_max'} || 0) < $val;
     162                $stat{$url}{'mem_max'} = $val if ($stat{$url}{'mem_max'} || 0) < $val;
    162163            }
    163164            elsif ($key eq 'inc') {
    164                 $stat{$uri}{'inc_max'} = $val if ($stat{$uri}{'inc_max'} || 0) < $val;
     165                $stat{$url}{'inc_max'} = $val if ($stat{$url}{'inc_max'} || 0) < $val;
    165166            }
    166167        }
    167168
    168         $stat{$uri}{'hit'}++;
     169        $stat{$url}{'hit'}++;
    169170        $hits++;
    170171    }
     
    186187    my $sortkey = shift;
    187188
     189    my %total;
     190    my @tkeys = qw/hit time user sys/;
     191
     192    # Limit URL column width, depending on output available columns
     193    my $cols  = $ENV{COLUMNS};
     194    $cols ||= Term::Size::chars;
     195    my $colmax = $cols - 1 - 9*@keys;
     196    die "Terminal width to short, try cheating with COLUMNS env var." if $colmax < 16;
     197
    188198    # Sort queries according to $sortkey (they are all numeric)
    189199    my @sorted = sort { $stat{$b}{$sortkey} - $stat{$a}{$sortkey} } keys %stat;
    190     
    191     # Compute URI column width
     200 
     201    # Compute URL column width
    192202    my $width = 0;
    193203    my $n = 0;
     
    197207        $width = $w if $w > $width;
    198208    }
    199    
    200     my %total;
    201     my @tkeys = qw/hit time user sys/;
     209    $width = $colmax if $width > $colmax;
     210
    202211    $n = 0;
    203    
    204212    printf("%-${width}s %s\n", "", join(' ', map { sprintf('%8s', ucfirst($_ =~ /^(mem|inc)$/ ? "$_/hit" : $_)) } @keys));
    205     foreach (@sorted) {
    206         my $s = $stat{$_};
     213    foreach my $url (@sorted) {
     214        my $s = $stat{$url};
    207215        $total{$_} += $s->{$_} foreach @tkeys;
    208216        next if $n++ >= $count; # Continue to loop and only sum totals if $count records have been displayed
    209    
    210         printf("%-${width}s", $_);
     217 
     218        my $ul = length($url);
     219        if ($ul > $width) {
     220            $url = substr($url, 0, $width - 3).'...';
     221        }
     222        printf("%-${width}s", $url);
    211223        printf($_ =~ m/hit|inc/ ? ' %8d' : ' %8.1f', $s->{$_}) foreach @keys;
    212224        print "\n";
Note: See TracChangeset for help on using the changeset viewer.