summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpbuetow (lap824) <puppet@mx.buetow.org>2012-01-22 10:41:27 +0100
committerpbuetow (lap824) <puppet@mx.buetow.org>2012-01-22 10:41:27 +0100
commite206bea5b3e2cb58b32b726acdc9d5c1127e057b (patch)
treee8bbf7335a454344cdd07d1b62e755a3a512a2b5
parent799a9c65ac87b49eee41eb2100f56eeea355518b (diff)
run perltdy
-rwxr-xr-xloadbars1479
1 files changed, 866 insertions, 613 deletions
diff --git a/loadbars b/loadbars
index 246a7d4..e5aefde 100755
--- a/loadbars
+++ b/loadbars
@@ -25,28 +25,28 @@ use threads;
use threads::shared;
use constant {
- VERSION => 'loadbars v0.4.0-devel',
- Copyright => '2010-2011 (c) Paul Buetow <loadbars@mx.buetow.org>',
- CSSH_CONFFILE => '/etc/clusters',
+ VERSION => 'loadbars v0.4.0-devel',
+ Copyright => '2010-2011 (c) Paul Buetow <loadbars@mx.buetow.org>',
+ CSSH_CONFFILE => '/etc/clusters',
CSSH_MAX_RECURSION => 10,
- COLOR_DEPTH => 8,
- BLACK => SDL::Color->new(-r => 0x00, -g => 0x00, -b => 0x00),
- BLUE0=> SDL::Color->new(-r => 0x00, -g => 0x00, -b => 0xff),
- BLUE => SDL::Color->new(-r => 0x00, -g => 0x00, -b => 0x88),
- GREEN => SDL::Color->new(-r => 0x00, -g => 0x90, -b => 0x00),
- ORANGE => SDL::Color->new(-r => 0xff, -g => 0x70, -b => 0x00),
- PURPLE => SDL::Color->new(-r => 0xa0, -g => 0x20, -b => 0xf0),
- RED => SDL::Color->new(-r => 0xff, -g => 0x00, -b => 0x00),
- WHITE => SDL::Color->new(-r => 0xff, -g => 0xff, -b => 0xff),
- GREY0 => SDL::Color->new(-r => 0x11, -g => 0x11, -b => 0x11),
- GREY => SDL::Color->new(-r => 0xaa, -g => 0xaa, -b => 0xaa),
- YELLOW0 => SDL::Color->new(-r => 0xff, -g => 0xa0, -b => 0x00),
- YELLOW => SDL::Color->new(-r => 0xff, -g => 0xc0, -b => 0x00),
- SYSTEM_BLUE0 => 30,
- USER_ORANGE => 70,
- USER_YELLOW0 => 50,
- NULL => 0,
- DEBUG => 0,
+ COLOR_DEPTH => 8,
+ BLACK => SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0x00 ),
+ BLUE0 => SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ),
+ BLUE => SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0x88 ),
+ GREEN => SDL::Color->new( -r => 0x00, -g => 0x90, -b => 0x00 ),
+ ORANGE => SDL::Color->new( -r => 0xff, -g => 0x70, -b => 0x00 ),
+ PURPLE => SDL::Color->new( -r => 0xa0, -g => 0x20, -b => 0xf0 ),
+ RED => SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ),
+ WHITE => SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ),
+ GREY0 => SDL::Color->new( -r => 0x11, -g => 0x11, -b => 0x11 ),
+ GREY => SDL::Color->new( -r => 0xaa, -g => 0xaa, -b => 0xaa ),
+ YELLOW0 => SDL::Color->new( -r => 0xff, -g => 0xa0, -b => 0x00 ),
+ YELLOW => SDL::Color->new( -r => 0xff, -g => 0xc0, -b => 0x00 ),
+ SYSTEM_BLUE0 => 30,
+ USER_ORANGE => 70,
+ USER_YELLOW0 => 50,
+ NULL => 0,
+ DEBUG => 0,
};
$| = 1;
@@ -60,19 +60,19 @@ my %C : shared;
# Setting defaults
%C = (
- title => Loadbars::VERSION . ' (press h for help on stdout)',
- average => 15,
- showcores => 0,
- cpuregexp => 'cpu',
- factor => 1,
- extended => 0,
- displaytxt => 1,
- displaytxthost => 0,
- inter => 0.1,
- samples => 1000,
- sshopts => '',
- width => 1250,
- height => 230,
+ title => Loadbars::VERSION . ' (press h for help on stdout)',
+ average => 15,
+ showcores => 0,
+ cpuregexp => 'cpu',
+ factor => 1,
+ extended => 0,
+ displaytxt => 1,
+ displaytxthost => 0,
+ inter => 0.1,
+ samples => 1000,
+ sshopts => '',
+ width => 1250,
+ height => 230,
);
# Quick n dirty helpers
@@ -84,36 +84,38 @@ sub null ($) { my $arg = shift; return defined $arg ? $arg : 0 }
sub set_showcores_regexp () { $C{cpuregexp} = $C{showcores} ? 'cpu' : 'cpu ' }
sub error ($) { die shift, "\n" }
-sub norm ($) {
- my $n = shift;
+sub norm ($) {
+ my $n = shift;
return $n if $C{factor} != 1;
- return $n > 100 ? 100 : ($n < 0 ? 0 : $n);
+ return $n > 100 ? 100 : ( $n < 0 ? 0 : $n );
}
sub parse_cpu_line ($) {
my $line = shift;
- my ($name, %load);
+ my ( $name, %load );
- ($name, @load{qw(user nice system idle iowait irq softirq steal guest)}) = split ' ', $line;
+ ( $name, @load{qw(user nice system idle iowait irq softirq steal guest)} ) =
+ split ' ', $line;
$load{steal} = 0 unless defined $load{steal};
$load{guest} = 0 unless defined $load{guest};
- $load{TOTAL} = sum @load{qw(user nice system idle iowait irq softirq steal guest)};
+ $load{TOTAL} =
+ sum @load{qw(user nice system idle iowait irq softirq steal guest)};
- return ($name, \%load);
+ return ( $name, \%load );
}
sub thread_get_stats ($;$) {
- my ($host, $user) = @_;
- $user = defined $user ? "-l $user" : '';
+ my ( $host, $user ) = @_;
+ $user = defined $user ? "-l $user" : '';
- my ($sigusr1, $quit) = (0, 0);
- my $loadavgexp = qr/(\d+\.\d{2}) (\d+\.\d{2}) (\d+\.\d{2})/;
+ my ( $sigusr1, $quit ) = ( 0, 0 );
+ my $loadavgexp = qr/(\d+\.\d{2}) (\d+\.\d{2}) (\d+\.\d{2})/;
- for (;;) {
- my $bash = <<"BASH";
+ for ( ; ; ) {
+ my $bash = <<"BASH";
if [ -e /proc/stat ]; then
loadavg=/proc/loadavg
stat=/proc/stat
@@ -134,486 +136,591 @@ sub thread_get_stats ($;$) {
fi
BASH
+ my $cmd =
+ $host eq 'localhost'
+ ? $bash
+ : "ssh $user -o StrictHostKeyChecking=no $C{sshopts} $host '$bash'";
- my $cmd = $host eq 'localhost' ? $bash
- : "ssh $user -o StrictHostKeyChecking=no $C{sshopts} $host '$bash'";
+ my $pid = open my $pipe, "$cmd |" or do {
+ say "Warning: $!";
+ sleep 3;
+ next;
+ };
- my $pid = open my $pipe, "$cmd |" or do {
- say "Warning: $!";
- sleep 3;
- next;
- };
+ # Toggle CPUs
+ $SIG{USR1} = sub { $sigusr1 = 1 };
+ my $cpuregexp = qr/$C{cpuregexp}/;
- # Toggle CPUs
- $SIG{USR1} = sub { $sigusr1 = 1 };
- my $cpuregexp = qr/$C{cpuregexp}/;
+ while (<$pipe>) {
+ if (/^$loadavgexp/) {
+ $AVGSTATS{$host} = "$1;$2;$3";
- while (<$pipe>) {
- if (/^$loadavgexp/) {
- $AVGSTATS{$host} = "$1;$2;$3";
-
- } elsif (/$cpuregexp/) {
- my ($name, $load) = parse_cpu_line $_;
- $CPUSTATS{"$host;$name"} = join ';',
- map { $_ . '=' . $load->{$_} }
- grep { defined $load->{$_} } keys %$load;
- }
+ }
+ elsif (/$cpuregexp/) {
+ my ( $name, $load ) = parse_cpu_line $_;
+ $CPUSTATS{"$host;$name"} = join ';',
+ map { $_ . '=' . $load->{$_} }
+ grep { defined $load->{$_} } keys %$load;
+ }
- if ($sigusr1) {
- $cpuregexp = qr/$C{cpuregexp}/;
- $sigusr1 = 0;
- }
- }
+ if ($sigusr1) {
+ $cpuregexp = qr/$C{cpuregexp}/;
+ $sigusr1 = 0;
+ }
+ }
- }
+ }
- return undef;
+ return undef;
}
sub get_rect ($$) {
- my ($rects, $name) = @_;
+ my ( $rects, $name ) = @_;
- return $rects->{$name} if exists $rects->{$name};
- return $rects->{$name} = SDL::Rect->new();
+ return $rects->{$name} if exists $rects->{$name};
+ return $rects->{$name} = SDL::Rect->new();
}
sub normalize_loads (%) {
- my %loads = @_;
+ my %loads = @_;
- return %loads unless exists $loads{TOTAL};
+ return %loads unless exists $loads{TOTAL};
- my $total = $loads{TOTAL} == 0 ? 1 : $loads{TOTAL};
- return map { $_ => $loads{$_} / ($total / 100) } keys %loads;
+ my $total = $loads{TOTAL} == 0 ? 1 : $loads{TOTAL};
+ return map { $_ => $loads{$_} / ( $total / 100 ) } keys %loads;
}
sub get_cpuaverage ($@) {
- my ($factor, @loads) = @_;
- my (%cpumax, %cpuaverage);
+ my ( $factor, @loads ) = @_;
+ my ( %cpumax, %cpuaverage );
- for my $l (@loads) {
- for (keys %$l) {
- $cpuaverage{$_} += $l->{$_};
+ for my $l (@loads) {
+ for ( keys %$l ) {
+ $cpuaverage{$_} += $l->{$_};
- $cpumax{$_} = $l->{$_}
- if not exists $cpumax{$_} or $cpumax{$_} < $l->{$_};
- }
- }
+ $cpumax{$_} = $l->{$_}
+ if not exists $cpumax{$_}
+ or $cpumax{$_} < $l->{$_};
+ }
+ }
- my $div = @loads / $factor;
+ my $div = @loads / $factor;
- for (keys %cpuaverage) {
- $cpuaverage{$_} /= $div;
- $cpumax{$_} /= $factor;
- }
+ for ( keys %cpuaverage ) {
+ $cpuaverage{$_} /= $div;
+ $cpumax{$_} /= $factor;
+ }
- return (\%cpumax, \%cpuaverage);
+ return ( \%cpumax, \%cpuaverage );
}
sub draw_background ($$) {
- my ($app, $rects) = @_;
- my $rect = get_rect $rects, 'background';
+ my ( $app, $rects ) = @_;
+ my $rect = get_rect $rects, 'background';
- $rect->width($C{width});
- $rect->height($C{height});
- $app->fill($rect, Loadbars::BLACK);
- $app->update($rect);
+ $rect->width( $C{width} );
+ $rect->height( $C{height} );
+ $app->fill( $rect, Loadbars::BLACK );
+ $app->update($rect);
- return undef;
+ return undef;
}
sub create_threads (@) {
- return map {
- $_->detach();
- $_;
+ return map {
+ $_->detach();
+ $_;
- } map {
- threads->create('thread_get_stats', split /:/);
+ } map {
+ threads->create( 'thread_get_stats', split /:/ );
- } @_;
+ } @_;
}
sub main_loop ($@) {
- my ($dispatch, @threads) = @_;
-
- my $app = SDL::App->new(
- -title => $C{title},
- -icon_title => $C{title},
- -width => $C{width},
- -height => $C{height},
- -depth => Loadbars::COLOR_DEPTH,
- -resizeable => 0,
- );
-
- SDL::Font->new('font.png')->use();
-
- my $num_stats = keys %CPUSTATS;
-
- my $rects = {};
- my %prev_stats;
- my %last_loads;
-
- my $redraw_background = 0;
- my $font_height = 14;
-
- my $displayinfo_time = 5;
- my $displayinfo_start = 0;
- my $displayinfo : shared = '';
- my $infotxt : shared = '';
- my $quit : shared = 0;
-
- my ($t1, $t2) = (Time::HiRes::time(), undef);
- my $event = SDL::Event->new();
-
- my $event_thread = async {
- for (;;) {
- $event->pump();
- $event->poll();
- $event->wait();
-
- my $type = $event->type();
- my $key_name = $event->key_name();
-
- debugsay "Event type=$type key_name=$key_name" if Loadbars::DEBUG;
- next if $type != 2;
-
- if ($key_name eq '1') {
- $C{showcores} = !$C{showcores};
- set_showcores_regexp;
- $_->kill('USR1') for @threads;
- %AVGSTATS = ();
- %CPUSTATS = ();
- $displayinfo = 'Toggled CPUs';
-
- } elsif ($key_name eq 'e') {
- $C{extended} = !$C{extended};
- $displayinfo = 'Toggled extended display';
-
- } elsif ($key_name eq 'h') {
- say '=> Hotkeys to use in the SDL interface';
- say $dispatch->('hotkeys');
- $displayinfo = 'Hotkeys help printed on terminal stdout';
-
- } elsif ($key_name eq 't') {
- $C{displaytxt} = !$C{displaytxt};
- $displayinfo = 'Toggled text display';
-
- } elsif ($key_name eq 'u') {
- $C{displaytxthost} = !$C{displaytxthost};
- $displayinfo = 'Toggled number/hostname display';
-
- } elsif ($key_name eq 'q') {
- $quit = 1;
- last;
-
- # Increase and decrease pairs
- } elsif ($key_name eq 'a') {
- ++$C{average};
- $displayinfo = "Set sample average to $C{average}";
- } elsif ($key_name eq 'y' or $key_name eq 'z') {
- my $avg = $C{average};
- --$avg;
- $C{average} = $avg > 1 ? $avg : 2;
- $displayinfo = "Set sample average to $C{average}";
-
- } elsif ($key_name eq 's') {
- $C{factor} += 0.1;
- $displayinfo = "Set scale factor to $C{factor}";
- } elsif ($key_name eq 'x' or $key_name eq 'z') {
- $C{factor} -= 0.1;
- $displayinfo = "Set scale factor to $C{factor}";
-
- } elsif ($key_name eq 'd') {
- $C{inter} += 0.1;
- $displayinfo = "Set graph update interval to $C{inter}";
- } elsif ($key_name eq 'c' or $key_name eq 'z') {
- my $int = $C{inter};
- $int -= 0.1;
- $C{inter} = $int > 0 ? $int : 0.1;
- $displayinfo = "Set graph update interval to $C{inter}";
- }
- }
- };
-
- do {
- my ($x, $y) = (0, 0);
- my %is_host_summary;
-
- my $new_num_stats = keys %CPUSTATS;
-
- if ($new_num_stats != $num_stats) {
- %prev_stats = ();
- %last_loads = ();
-
- $num_stats = $new_num_stats;
- $redraw_background = 1;
- }
-
- # Avoid division by null
- # Also substract 1 (each bar is followed by an 1px separator bar)
- my $width = $C{width} / ($num_stats ? $num_stats : 1) - 1;
-
- my ($current_barnum, $current_corenum) = (-1, -1);
-
- for my $key (sort keys %CPUSTATS) {
- ++$current_barnum;
- ++$current_corenum;
- my ($host, $name) = split ';', $key;
-
- next unless defined $CPUSTATS{$key};
-
- my %stat = map {
- my ($k, $v) = split '='; $k => $v
-
- } split ';', $CPUSTATS{$key};
-
- unless (exists $prev_stats{$key}) {
- $prev_stats{$key} = \%stat;
- next;
- }
-
- my $prev_stat = $prev_stats{$key};
- my %loads = null $stat{TOTAL} == null $prev_stat->{TOTAL}
- ? %stat : map {
- $_ => $stat{$_} - $prev_stat->{$_}
- } keys %stat;
-
- $prev_stats{$key} = \%stat;
-
- %loads = normalize_loads %loads;
- push @{$last_loads{$key}}, \%loads;
- shift @{$last_loads{$key}} while @{$last_loads{$key}} >= $C{average};
-
- my ($cpumax, $cpuaverage) = get_cpuaverage $C{factor}, @{$last_loads{$key}};
-
- my %heights = map {
- $_ => defined $cpuaverage->{$_} ? $cpuaverage->{$_} * ($C{height}/100) : 1
- } keys %$cpuaverage;
-
- my $is_host_summary = exists $is_host_summary{$host};
-
- my $rect_separator = undef;
-
- my $rect_idle = get_rect $rects, "$key;idle";
- my $rect_steal = get_rect $rects, "$key;steal";
- my $rect_guest = get_rect $rects, "$key;guest";
- my $rect_irq = get_rect $rects, "$key;irq";
- my $rect_softirq = get_rect $rects, "$key;softirq";
- my $rect_nice = get_rect $rects, "$key;nice";
- my $rect_iowait = get_rect $rects, "$key;iowait";
- my $rect_user = get_rect $rects, "$key;user";
- my $rect_system = get_rect $rects, "$key;system";
-
- my $rect_peak;
-
- unless ($is_host_summary || !$C{showcores}) {
- $current_corenum = 0;
- $rect_separator = get_rect $rects, "$key;separator";
- $rect_separator->width(1);
- $rect_separator->height($C{height});
- $rect_separator->x($x-1);
- $rect_separator->y(0);
- $app->fill($rect_separator, Loadbars::GREY);
- }
-
- $y = $C{height} - $heights{system};
- $rect_system->width($width);
- $rect_system->height($heights{system});
- $rect_system->x($x);
- $rect_system->y($y);
-
- $y -= $heights{user};
- $rect_user->width($width);
- $rect_user->height($heights{user});
- $rect_user->x($x);
- $rect_user->y($y);
-
- $y -= $heights{nice};
- $rect_nice->width($width);
- $rect_nice->height($heights{nice});
- $rect_nice->x($x);
- $rect_nice->y($y);
-
- $y -= $heights{idle};
- $rect_idle->width($width);
- $rect_idle->height($heights{idle});
- $rect_idle->x($x);
- $rect_idle->y($y);
-
- $y -= $heights{iowait};
- $rect_iowait->width($width);
- $rect_iowait->height($heights{iowait});
- $rect_iowait->x($x);
- $rect_iowait->y($y);
-
- $y -= $heights{irq};
- $rect_irq->width($width);
- $rect_irq->height($heights{irq});
- $rect_irq->x($x);
- $rect_irq->y($y);
-
- $y -= $heights{softirq};
- $rect_softirq->width($width);
- $rect_softirq->height($heights{softirq});
- $rect_softirq->x($x);
- $rect_softirq->y($y);
-
- $y -= $heights{guest};
- $rect_guest->width($width);
- $rect_guest->height($heights{guest});
- $rect_guest->x($x);
- $rect_guest->y($y);
-
- $y -= $heights{steal};
- $rect_steal->width($width);
- $rect_steal->height($heights{steal});
- $rect_steal->x($x);
- $rect_steal->y($y);
-
- my $all = 100 - $cpuaverage->{idle};
+ my ( $dispatch, @threads ) = @_;
+
+ my $app = SDL::App->new(
+ -title => $C{title},
+ -icon_title => $C{title},
+ -width => $C{width},
+ -height => $C{height},
+ -depth => Loadbars::COLOR_DEPTH,
+ -resizeable => 0,
+ );
+
+ SDL::Font->new('font.png')->use();
+
+ my $num_stats = keys %CPUSTATS;
+
+ my $rects = {};
+ my %prev_stats;
+ my %last_loads;
+
+ my $redraw_background = 0;
+ my $font_height = 14;
+
+ my $displayinfo_time = 5;
+ my $displayinfo_start = 0;
+ my $displayinfo : shared = '';
+ my $infotxt : shared = '';
+ my $quit : shared = 0;
+
+ my ( $t1, $t2 ) = ( Time::HiRes::time(), undef );
+ my $event = SDL::Event->new();
+
+ my $event_thread = async {
+ for ( ; ; ) {
+ $event->pump();
+ $event->poll();
+ $event->wait();
+
+ my $type = $event->type();
+ my $key_name = $event->key_name();
+
+ debugsay "Event type=$type key_name=$key_name" if Loadbars::DEBUG;
+ next if $type != 2;
+
+ if ( $key_name eq '1' ) {
+ $C{showcores} = !$C{showcores};
+ set_showcores_regexp;
+ $_->kill('USR1') for @threads;
+ %AVGSTATS = ();
+ %CPUSTATS = ();
+ $displayinfo = 'Toggled CPUs';
+
+ }
+ elsif ( $key_name eq 'e' ) {
+ $C{extended} = !$C{extended};
+ $displayinfo = 'Toggled extended display';
+
+ }
+ elsif ( $key_name eq 'h' ) {
+ say '=> Hotkeys to use in the SDL interface';
+ say $dispatch->('hotkeys');
+ $displayinfo = 'Hotkeys help printed on terminal stdout';
+
+ }
+ elsif ( $key_name eq 't' ) {
+ $C{displaytxt} = !$C{displaytxt};
+ $displayinfo = 'Toggled text display';
+
+ }
+ elsif ( $key_name eq 'u' ) {
+ $C{displaytxthost} = !$C{displaytxthost};
+ $displayinfo = 'Toggled number/hostname display';
+
+ }
+ elsif ( $key_name eq 'q' ) {
+ $quit = 1;
+ last;
+
+ # Increase and decrease pairs
+ }
+ elsif ( $key_name eq 'a' ) {
+ ++$C{average};
+ $displayinfo = "Set sample average to $C{average}";
+ }
+ elsif ( $key_name eq 'y' or $key_name eq 'z' ) {
+ my $avg = $C{average};
+ --$avg;
+ $C{average} = $avg > 1 ? $avg : 2;
+ $displayinfo = "Set sample average to $C{average}";
+
+ }
+ elsif ( $key_name eq 's' ) {
+ $C{factor} += 0.1;
+ $displayinfo = "Set scale factor to $C{factor}";
+ }
+ elsif ( $key_name eq 'x' or $key_name eq 'z' ) {
+ $C{factor} -= 0.1;
+ $displayinfo = "Set scale factor to $C{factor}";
+
+ }
+ elsif ( $key_name eq 'd' ) {
+ $C{inter} += 0.1;
+ $displayinfo = "Set graph update interval to $C{inter}";
+ }
+ elsif ( $key_name eq 'c' or $key_name eq 'z' ) {
+ my $int = $C{inter};
+ $int -= 0.1;
+ $C{inter} = $int > 0 ? $int : 0.1;
+ $displayinfo = "Set graph update interval to $C{inter}";
+ }
+ }
+ };
+
+ do {
+ my ( $x, $y ) = ( 0, 0 );
+ my %is_host_summary;
+
+ my $new_num_stats = keys %CPUSTATS;
+
+ if ( $new_num_stats != $num_stats ) {
+ %prev_stats = ();
+ %last_loads = ();
+
+ $num_stats = $new_num_stats;
+ $redraw_background = 1;
+ }
+
+ # Avoid division by null
+ # Also substract 1 (each bar is followed by an 1px separator bar)
+ my $width = $C{width} / ( $num_stats ? $num_stats : 1 ) - 1;
+
+ my ( $current_barnum, $current_corenum ) = ( -1, -1 );
+
+ for my $key ( sort keys %CPUSTATS ) {
+ ++$current_barnum;
+ ++$current_corenum;
+ my ( $host, $name ) = split ';', $key;
+
+ next unless defined $CPUSTATS{$key};
+
+ my %stat = map {
+ my ( $k, $v ) = split '=';
+ $k => $v
+
+ } split ';', $CPUSTATS{$key};
+
+ unless ( exists $prev_stats{$key} ) {
+ $prev_stats{$key} = \%stat;
+ next;
+ }
+
+ my $prev_stat = $prev_stats{$key};
+ my %loads =
+ null $stat{TOTAL} == null $prev_stat->{TOTAL}
+ ? %stat
+ : map { $_ => $stat{$_} - $prev_stat->{$_} } keys %stat;
+
+ $prev_stats{$key} = \%stat;
+
+ %loads = normalize_loads %loads;
+ push @{ $last_loads{$key} }, \%loads;
+ shift @{ $last_loads{$key} }
+ while @{ $last_loads{$key} } >= $C{average};
+
+ my ( $cpumax, $cpuaverage ) = get_cpuaverage $C{factor},
+ @{ $last_loads{$key} };
+
+ my %heights = map {
+ $_ => defined $cpuaverage->{$_}
+ ? $cpuaverage->{$_} * ( $C{height} / 100 )
+ : 1
+ } keys %$cpuaverage;
+
+ my $is_host_summary = exists $is_host_summary{$host};
+
+ my $rect_separator = undef;
+
+ my $rect_idle = get_rect $rects, "$key;idle";
+ my $rect_steal = get_rect $rects, "$key;steal";
+ my $rect_guest = get_rect $rects, "$key;guest";
+ my $rect_irq = get_rect $rects, "$key;irq";
+ my $rect_softirq = get_rect $rects, "$key;softirq";
+ my $rect_nice = get_rect $rects, "$key;nice";
+ my $rect_iowait = get_rect $rects, "$key;iowait";
+ my $rect_user = get_rect $rects, "$key;user";
+ my $rect_system = get_rect $rects, "$key;system";
+
+ my $rect_peak;
+
+ unless ( $is_host_summary || !$C{showcores} ) {
+ $current_corenum = 0;
+ $rect_separator = get_rect $rects, "$key;separator";
+ $rect_separator->width(1);
+ $rect_separator->height( $C{height} );
+ $rect_separator->x( $x - 1 );
+ $rect_separator->y(0);
+ $app->fill( $rect_separator, Loadbars::GREY );
+ }
+
+ $y = $C{height} - $heights{system};
+ $rect_system->width($width);
+ $rect_system->height( $heights{system} );
+ $rect_system->x($x);
+ $rect_system->y($y);
+
+ $y -= $heights{user};
+ $rect_user->width($width);
+ $rect_user->height( $heights{user} );
+ $rect_user->x($x);
+ $rect_user->y($y);
+
+ $y -= $heights{nice};
+ $rect_nice->width($width);
+ $rect_nice->height( $heights{nice} );
+ $rect_nice->x($x);
+ $rect_nice->y($y);
+
+ $y -= $heights{idle};
+ $rect_idle->width($width);
+ $rect_idle->height( $heights{idle} );
+ $rect_idle->x($x);
+ $rect_idle->y($y);
+
+ $y -= $heights{iowait};
+ $rect_iowait->width($width);
+ $rect_iowait->height( $heights{iowait} );
+ $rect_iowait->x($x);
+ $rect_iowait->y($y);
+
+ $y -= $heights{irq};
+ $rect_irq->width($width);
+ $rect_irq->height( $heights{irq} );
+ $rect_irq->x($x);
+ $rect_irq->y($y);
+
+ $y -= $heights{softirq};
+ $rect_softirq->width($width);
+ $rect_softirq->height( $heights{softirq} );
+ $rect_softirq->x($x);
+ $rect_softirq->y($y);
+
+ $y -= $heights{guest};
+ $rect_guest->width($width);
+ $rect_guest->height( $heights{guest} );
+ $rect_guest->x($x);
+ $rect_guest->y($y);
+
+ $y -= $heights{steal};
+ $rect_steal->width($width);
+ $rect_steal->height( $heights{steal} );
+ $rect_steal->x($x);
+ $rect_steal->y($y);
+
+ my $all = 100 - $cpuaverage->{idle};
my $max_all = 0;
-
- $app->fill($rect_idle, Loadbars::BLACK);
- $app->fill($rect_steal, Loadbars::RED);
- $app->fill($rect_guest, Loadbars::RED);
- $app->fill($rect_irq, Loadbars::WHITE);
- $app->fill($rect_softirq, Loadbars::WHITE);
- $app->fill($rect_nice, Loadbars::GREEN);
- $app->fill($rect_iowait, Loadbars::PURPLE);
-
- if ($C{extended}) {
+
+ $app->fill( $rect_idle, Loadbars::BLACK );
+ $app->fill( $rect_steal, Loadbars::RED );
+ $app->fill( $rect_guest, Loadbars::RED );
+ $app->fill( $rect_irq, Loadbars::WHITE );
+ $app->fill( $rect_softirq, Loadbars::WHITE );
+ $app->fill( $rect_nice, Loadbars::GREEN );
+ $app->fill( $rect_iowait, Loadbars::PURPLE );
+
+ if ( $C{extended} ) {
my %maxheights = map {
- $_ => defined $cpumax->{$_} ? $cpumax->{$_} * ($C{height}/100) : 1
- } keys %$cpumax;
-
+ $_ => defined $cpumax->{$_}
+ ? $cpumax->{$_} * ( $C{height} / 100 )
+ : 1
+ } keys %$cpumax;
+
$rect_peak = get_rect $rects, "$key;max";
$rect_peak->width($width);
- $rect_peak->height(1);
- $rect_peak->x($x);
- $rect_peak->y($C{height} - $maxheights{system} - $maxheights{user});
-
- $max_all = sum @{$cpumax}{qw(user system iowait irq softirq steal guest)};
-
- $app->fill($rect_peak, $max_all > Loadbars::USER_ORANGE ? Loadbars::ORANGE
- : ($max_all > Loadbars::USER_YELLOW0 ? Loadbars::YELLOW0
- : (Loadbars::YELLOW)));
+ $rect_peak->height(1);
+ $rect_peak->x($x);
+ $rect_peak->y(
+ $C{height} - $maxheights{system} - $maxheights{user} );
+
+ $max_all =
+ sum @{$cpumax}
+ {qw(user system iowait irq softirq steal guest)};
+
+ $app->fill(
+ $rect_peak,
+ $max_all > Loadbars::USER_ORANGE ? Loadbars::ORANGE
+ : (
+ $max_all > Loadbars::USER_YELLOW0 ? Loadbars::YELLOW0
+ : (Loadbars::YELLOW)
+ )
+ );
}
- $app->fill($rect_user, $all > Loadbars::USER_ORANGE ? Loadbars::ORANGE
- : ($all > Loadbars::USER_YELLOW0 ? Loadbars::YELLOW0
- : (Loadbars::YELLOW)));
- $app->fill($rect_system, $cpuaverage->{system} > Loadbars::SYSTEM_BLUE0 ? Loadbars::BLUE0
- : Loadbars::BLUE);
-
- my ($y, $space) = (5, $font_height);
- my @loadavg = split ';', $AVGSTATS{$host};
- $is_host_summary{$host} = 1 if defined $loadavg[0];
-
- if ($C{displaytxt}) {
- if ($C{displaytxthost} && not $is_host_summary) {
- # If hostname is printed don't use FQDN
- # because of its length.
- $host =~ /([^\.]*)/;
- $app->print($x, $y, sprintf '%s:', $1);
-
- } else {
- $app->print($x, $y, sprintf '%i:',
- $C{showcores} ? $current_corenum : $current_barnum + 1);
- }
-
-
- if ($C{extended}) {
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{steal}, 'st');
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{guest}, 'gt');
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{softirq}, 'sr');
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{irq}, 'ir');
- }
+ $app->fill(
+ $rect_user,
+ $all > Loadbars::USER_ORANGE ? Loadbars::ORANGE
+ : (
+ $all > Loadbars::USER_YELLOW0 ? Loadbars::YELLOW0
+ : (Loadbars::YELLOW)
+ )
+ );
+ $app->fill( $rect_system,
+ $cpuaverage->{system} > Loadbars::SYSTEM_BLUE0
+ ? Loadbars::BLUE0
+ : Loadbars::BLUE );
+
+ my ( $y, $space ) = ( 5, $font_height );
+ my @loadavg = split ';', $AVGSTATS{$host};
+ $is_host_summary{$host} = 1 if defined $loadavg[0];
+
+ if ( $C{displaytxt} ) {
+ if ( $C{displaytxthost} && not $is_host_summary ) {
+
+ # If hostname is printed don't use FQDN
+ # because of its length.
+ $host =~ /([^\.]*)/;
+ $app->print( $x, $y, sprintf '%s:', $1 );
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{iowait}, 'io');
+ }
+ else {
+ $app->print( $x, $y, sprintf '%i:',
+ $C{showcores}
+ ? $current_corenum
+ : $current_barnum + 1 );
+ }
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{idle}, 'id') if $C{extended};
+ if ( $C{extended} ) {
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{steal}, 'st'
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{guest}, 'gt'
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{softirq}, 'sr'
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{irq}, 'ir'
+ );
+ }
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{nice}, 'ni');
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{user}, 'us');
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $cpuaverage->{system}, 'sy');
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $all, 'to');
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{iowait}, 'io'
+ );
+
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{idle}, 'id'
+ ) if $C{extended};
+
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{nice}, 'ni'
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{user}, 'us'
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $cpuaverage->{system}, 'sy'
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $all, 'to'
+ );
+
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf '%02d%s',
+ norm $max_all, 'pk'
+ ) if $C{extended};
- $app->print($x, $y+=$space, sprintf '%02d%s', norm $max_all, 'pk') if $C{extended};
-
unless ($is_host_summary) {
- if (defined $loadavg[0]) {
- $app->print($x, $y+=$space, 'avg:');
- $app->print($x, $y+=$space, sprintf "%.2f", $loadavg[0]);
- $app->print($x, $y+=$space, sprintf "%.2f", $loadavg[1]);
- $app->print($x, $y+=$space, sprintf "%.2f", $loadavg[2]);
- }
+ if ( defined $loadavg[0] ) {
+ $app->print( $x, $y += $space, 'avg:' );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf "%.2f",
+ $loadavg[0]
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf "%.2f",
+ $loadavg[1]
+ );
+ $app->print(
+ $x,
+ $y += $space,
+ sprintf "%.2f",
+ $loadavg[2]
+ );
+ }
}
- }
+ }
- # Display an informational text message if any
+ # Display an informational text message if any
#$app->print(0, $y+=$space, $displayinfo) if length $displayinfo;
- if (length $displayinfo) {
+ if ( length $displayinfo ) {
say $displayinfo;
$displayinfo = '';
}
-
- $app->update(
- $rect_idle,
- $rect_iowait,
- $rect_irq,
- $rect_nice,
- $rect_softirq,
- $rect_steal,
- $rect_guest,
- $rect_system,
- $rect_user,
- );
- $app->update($rect_separator) if defined $rect_separator;
+ $app->update(
+ $rect_idle, $rect_iowait, $rect_irq,
+ $rect_nice, $rect_softirq, $rect_steal,
+ $rect_guest, $rect_system, $rect_user,
+ );
- $x += $width + 1;
- }
+ $app->update($rect_separator) if defined $rect_separator;
-TIMEKEEPER:
- $t2 = Time::HiRes::time();
+ $x += $width + 1;
+ }
- if (length $displayinfo) {
- if ($displayinfo_start == 0) {
- $displayinfo_start = $t2;
+ TIMEKEEPER:
+ $t2 = Time::HiRes::time();
- } else {
- if ($displayinfo_time < $t2 - $displayinfo_start) {
- $displayinfo = '';