通信状態表示

GNU screen hardstatus #3

意味ねええええ。ので多分すぐあきる。

#!/usr/bin/perl

use strict;
use warnings;

$|=1;

{
    package StatQue;

    sub new { bless {}, shift }

    sub add {
        my $self = shift;
        push @{$self->{que}}, shift;

        shift @{$self->{que}} if @{$self->{que}} >= 11;
    }

    sub avg {
        my $self = shift;
        my $total;
        $total += $_ for @{$self->{que}};

        $total / @{$self->{que}};
    }
}

my $in_que  = StatQue->new;
my $out_que = StatQue->new;

my @stats = grep /.+/, split /:|\s/, qx{ cat /proc/net/dev | grep eth0 };
my ( $old_in, $old_out ) = ( $stats[1], $stats[9] );

while (1) {
    sleep 1;

    @stats = grep /.+/, split /:|\s/, qx{ cat /proc/net/dev | grep eth0 };
    my ( $new_in, $new_out ) = ( $stats[1], $stats[9] );

    $in_que->add( $new_in - $old_in );
    $out_que->add( $new_out - $old_out );

    printf("in: %.1fKB/s out: %.1fKB/s\n",
            $in_que->avg / 1024,
            $out_que->avg / 1024);

    $old_in  = $new_in;
    $old_out = $new_out;
}

こんなのを netstat.pl とかしておいて screenrc に

backtick 1 0 0 ${HOME}/.screen/netstat.pl

とする。hardstatus も

-hardstatus alwayslastline "[%02c] %`%-w%{=b bw}%n %t%{-}%+w"
+hardstatus alwayslastline "[%02c] %0`%-w%{=b bw}%n %t%{-}%+w %= %1`"

こんな感じで。

昨日の backtick.pl が ID0 で netstat.pl が ID1 ってことね。

by typester / at 2005-12-06T17:49:00 / screen / Comments(0)