summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2011-08-06 20:09:18 +0000
committerPaul Buetow <paul@buetow.org>2011-08-06 20:09:18 +0000
commit975cda6a198dba00b11eb7ca71638ef4451d1ff2 (patch)
tree29ef357a37f7ba893af9e4bb88c4ba02bec679c3
parent05b9e09515438509e5c8af870d584962702fd859 (diff)
Tagged v0.2.0
-rw-r--r--BUGS3
-rw-r--r--CHANGELOG7
-rw-r--r--WISHLIST4
-rwxr-xr-xloadbars.pl452
4 files changed, 236 insertions, 230 deletions
diff --git a/BUGS b/BUGS
index f5efa54..6dae86e 100644
--- a/BUGS
+++ b/BUGS
@@ -1 +1,2 @@
-Sometimes Loadbars hangs using the toggle options having many bars loaded.
+* Status messages sometimes have black vertical lines in between
+* After quit ssh processes dont shut down instantly but later
diff --git a/CHANGELOG b/CHANGELOG
index a138e8d..8c5abb8 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,10 @@
+Sa 6. Aug 22:04:15 CEST 2011
+* Released v0.2.0 (new major version)
+* No interactive CLI shell anymore but instead hotkeys for the
+ SDL interface (press h and see).
+* Bugfixes (E.g. Loadbars does not hang anymore after typing commands)
+* Major code refactoring
+
Fr 5. Aug 23:52:49 CEST 2011
* Released v0.1.3.1
* Some more minor bugfixes
diff --git a/WISHLIST b/WISHLIST
index 3347815..0ff20b6 100644
--- a/WISHLIST
+++ b/WISHLIST
@@ -1 +1,3 @@
-- Stats for other stuff (e.g. memory, inodes..., disk usage)
+* Stats for other stuff (e.g. memory, inodes..., disk usage)
+* Dynamic/online resizing of the window
+* Rudimentary support for /etc/clusters, the Cluster-SSH configuration file
diff --git a/loadbars.pl b/loadbars.pl
index aa1b46d..c8ac5a1 100755
--- a/loadbars.pl
+++ b/loadbars.pl
@@ -33,7 +33,6 @@ use strict;
use warnings;
use Getopt::Long;
-use Term::ReadLine;
use SDL::App;
use SDL::Rect;
@@ -48,15 +47,12 @@ use Time::HiRes qw(usleep gettimeofday);
use threads;
use threads::shared;
+use IO::Socket;
+
use constant {
DEPTH => 8,
- PROMPT => 'loadbars> ',
- VERSION => 'loadbars v0.1.3.1',
+ VERSION => 'loadbars v0.2.0',
COPYRIGHT => '2010-2011 (c) Paul Buetow <loadbars@mx.buetow.org>',
- NULL => 0,
- MSG_TOGGLE_TXT => 1,
- MSG_TOGGLE_TXT_HOST => 2,
- MSG_SET_FACTOR => 5,
BLACK => SDL::Color->new(-r => 0x00, -g => 0x00, -b => 0x00),
BLUE => SDL::Color->new(-r => 0x00, -g => 0x00, -b => 0xff),
GREEN => SDL::Color->new(-r => 0x00, -g => 0x90, -b => 0x00),
@@ -71,43 +67,39 @@ use constant {
USER_RED => 90,
USER_ORANGE => 70,
USER_YELLOW0 => 50,
-
- # For developing or other debugging purporses
- DEBUG => 0,
+ NULL => 0,
+ DEBUG => 1,
};
$| = 1;
-my %AVGSTATS :shared;
-my %CPUSTATS :shared;
-my %CONF :shared;
-my $MSG :shared;
-my @HOSTS :shared;
+my %AVGSTATS : shared;
+my %CPUSTATS : shared;
+my %CONF : shared;
+# Setting defaults
%CONF = (
title => VERSION,
average => 30,
+ togglecpu => 1,
cpuregexp => 'cpu',
factor => 1,
+ displaytxt => 1,
+ displaytxthost => 0,
inter => 0.1,
samples => 1000,
sshopts => '',
- togglecpu => 1,
- toggletxt => 1,
- toggletxthost => 0,
width => 1200,
height => 200,
);
+# Quick n dirty helpers
sub say (@) { print "$_\n" for @_; return undef }
sub newline () { say ''; return undef }
sub debugsay (@) { say "DEBUG: $_" for @_; return undef }
-
-sub sum (@) {
- my $sum = 0;
- $sum += $_ for @_;
- return $sum;
-}
+sub sum (@) { my $sum = 0; $sum += $_ for @_; return $sum }
+sub null ($) { my $arg = shift; return defined $arg ? $arg : 0 }
+sub set_togglecpu_regexp () { $CONF{cpuregexp} = $CONF{togglecpu} ? 'cpu ' : 'cpu' }
sub parse_cpu_line ($) {
my ($name, %load);
@@ -118,7 +110,7 @@ sub parse_cpu_line ($) {
return ($name, \%load);
}
-sub thr_get_stat ($) {
+sub thread_get_stats ($) {
my $host = shift;
my $sigusr1 = 0;
@@ -148,18 +140,8 @@ BASH
next;
};
- $SIG{STOP} = sub {
- say "Terminating get_stat($host) [SSH PID $pid]";
- kill 1, $pid;
- close $pipe;
- threads->exit();
- };
-
# Toggle CPUs
- $SIG{USR1} = sub {
- $sigusr1 = 1;
- };
-
+ $SIG{USR1} = sub { $sigusr1 = 1 };
my $cpuregexp = qr/$CONF{cpuregexp}/;
while (<$pipe>) {
@@ -213,11 +195,6 @@ sub get_cpuaverage ($@) {
return %cpuaverage;
}
-sub wait_for_stats () {
- sleep 1 until %CPUSTATS;
- return undef;
-}
-
sub draw_background ($$) {
my ($app, $rects) = @_;
my $rect = get_rect $rects, 'background';
@@ -230,62 +207,138 @@ sub draw_background ($$) {
return undef;
}
-sub null ($) {
- my $arg = shift;
- return defined $arg ? $arg : 0;
+sub create_threads (@) {
+ return map { $_->detach(); $_ } map { threads->create('thread_get_stats', $_) } @_;
}
-sub thr_display_stats () {
+sub main_loop ($@) {
+ my ($dispatch, @threads) = @_;
+
+ # Planned for the future
+ my $statusbar_height = 0;
+
my $app = SDL::App->new(
-title => $CONF{title},
-icon_title => $CONF{title},
-width => $CONF{width},
- -height => $CONF{height}.
+ -height => $CONF{height}+$statusbar_height,
-depth => DEPTH,
-resizeable => 0,
);
SDL::Font->new('font.png')->use();
- wait_for_stats;
my $num_stats = keys %CPUSTATS;
- my $factor = $CONF{factor};
my $rects = {};
my %prev_stats;
my %last_loads;
- my $displaytxt = $CONF{toggletxt};
- my $displaytxthost = $CONF{toggletxthost};
- my $sigstop = 0;
my $redraw_background = 0;
+ my $font_height = 14;
- $SIG{STOP} = sub {
- say "Shutting down display_stats";
- $sigstop = 1;
- };
+ my $displayinfo_time = 5;
+ my $displayinfo_start = 0;
+ my $displayinfo : shared = '';
+ my $infotxt : shared = '';
+ my $quit : shared = 0;
- # Toggle CPU
- $SIG{USR1} = sub { wait_for_stats };
+ $SIG{STOP} = sub { $quit = 1 };
- # Diverse messages
- $SIG{USR2} = sub {
- if ($MSG == MSG_TOGGLE_TXT) {
- $displaytxt = $CONF{toggletxt};
-
- } elsif ($MSG == MSG_TOGGLE_TXT_HOST) {
- $displaytxthost = $CONF{toggletxthost};
-
- } elsif ($MSG == MSG_SET_FACTOR) {
- $factor = $CONF{factor};
+ 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 DEBUG;
+ next if $type != 2;
+
+ if ($key_name eq '1') {
+ $CONF{togglecpu} = !$CONF{togglecpu};
+ set_togglecpu_regexp;
+ $_->kill('USR1') for @threads;
+ %AVGSTATS = ();
+ %CPUSTATS = ();
+ $displayinfo = 'Toggled CPUs';
+
+ } 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') {
+ $CONF{displaytxt} = !$CONF{displaytxt};
+ $displayinfo = 'Toggled text display';
+
+ } elsif ($key_name eq 'u') {
+ $CONF{displaytxthost} = !$CONF{displaytxthost};
+ $displayinfo = 'Toggled number/hostname display';
+
+ } elsif ($key_name eq 'q') {
+ $quit = 1;
+ last;
+
+ # Increase and decrease pairs
+ } elsif ($key_name eq 'a') {
+ ++$CONF{average};
+ $displayinfo = "Set sample average to $CONF{average}";
+ } elsif ($key_name eq 'y' or $key_name eq 'z') {
+ my $avg = $CONF{average};
+ --$avg;
+ $CONF{average} = $avg > 1 ? $avg : 2;
+ $displayinfo = "Set sample average to $CONF{average}";
+
+ } elsif ($key_name eq 's') {
+ $CONF{factor} += 0.1;
+ $displayinfo = "Set scale factor to $CONF{factor}";
+ } elsif ($key_name eq 'x' or $key_name eq 'z') {
+ $CONF{factor} -= 0.1;
+ $displayinfo = "Set scale factor to $CONF{factor}";
+
+ } elsif ($key_name eq 'd') {
+ $CONF{inter} += 0.1;
+ $displayinfo = "Set graph update interval to $CONF{inter}";
+ } elsif ($key_name eq 'c' or $key_name eq 'z') {
+ my $int = $CONF{inter};
+ $int -= 0.1;
+ $CONF{inter} = $int > 0 ? $int : 0.1;
+ $displayinfo = "Set graph update interval to $CONF{inter}";
+
+=cut
+ } elsif ($key_name eq 'down') {
+ my $height = $CONF{height} + 10;
+ $app->resize($CONF{width},$height);
+ $CONF{height} = $height;
+ $displayinfo = "Set graph height to $CONF{height}";
+ } elsif ($key_name eq 'up') {
+ my $height = $CONF{height};
+ $height -= 10;
+ $CONF{height} = $height > 1 ? $height : 1;
+ $app->resize($CONF{width},$CONF{height});
+ $displayinfo = "Set graph height to $CONF{height}";
+
+ } elsif ($key_name eq 'right') {
+ $CONF{width} += 10;
+ $app->resize($CONF{width},$CONF{height});
+ $displayinfo = "Set graph width to $CONF{width}";
+ } elsif ($key_name eq 'left') {
+ my $width = $CONF{width};
+ $width -= 10;
+ $CONF{width} = $width > 1 ? $width : 1;
+ $app->resize($CONF{width},$CONF{height});
+ $displayinfo = "Set graph width to $CONF{width}";
+=cut
+ }
}
-
- $redraw_background = 1;
- $MSG = NULL;
};
- my ($t1, $t2) = (Time::HiRes::time(), undef);
-
do {
my ($x, $y) = (0, 0);
my %is_host_summary;
@@ -297,14 +350,15 @@ sub thr_display_stats () {
$num_stats = $new_num_stats;
$redraw_background = 1;
- #draw_background $app, $rects;
}
- my $width = $CONF{width} / $num_stats - 1;
+ # Avoid division by null
+ my $div = $num_stats - 1;
+ my $width = $CONF{width} / ($div ? $div : 1);
- my $counter = -1;
+ my $barnum = -1;
for my $key (sort keys %CPUSTATS) {
- ++$counter;
+ ++$barnum;
my ($host, $name) = split ';', $key;
next unless defined $CPUSTATS{$key};
@@ -331,7 +385,7 @@ sub thr_display_stats () {
push @{$last_loads{$key}}, \%loads;
shift @{$last_loads{$key}} while @{$last_loads{$key}} >= $CONF{average};
- my %cpuaverage = get_cpuaverage $factor, @{$last_loads{$key}};
+ my %cpuaverage = get_cpuaverage $CONF{factor}, @{$last_loads{$key}};
my %heights = map {
$_ => defined $cpuaverage{$_} ? $cpuaverage{$_} * ($CONF{height}/100) : 1
@@ -381,17 +435,19 @@ sub thr_display_stats () {
: ($system_n_user > USER_YELLOW0 ? YELLOW0
: (YELLOW)))));
-
- if ($displaytxt) {
- my ($y, $space) = (5, 15);
+
+ my ($y, $space) = (5, $font_height);
+ if ($CONF{displaytxt}) {
my $is_host_summary = exists $is_host_summary{$host};
- if ($displaytxthost && not $is_host_summary) {
+ if ($CONF{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:', $counter);
+ $app->print($x, $y, sprintf '%i:', $barnum);
}
$app->print($x, $y+=$space, sprintf '%d%s', $cpuaverage{nice}, 'ni');
@@ -401,16 +457,22 @@ sub thr_display_stats () {
unless ($is_host_summary) {
my @loadavg = split ';', $AVGSTATS{$host};
-
- $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]);
+ }
$is_host_summary{$host} = 1;
}
}
-
+
+ # Display an informational text message if any
+ $app->print(0, $y+=$space, $displayinfo) if length $displayinfo;
+
+
$app->update($_) for $rect_nice, $rect_iowait, $rect_system, $rect_user;
$x += $width + 1;
}
@@ -418,8 +480,21 @@ sub thr_display_stats () {
TIMEKEEPER:
$t2 = Time::HiRes::time();
+ if (length $displayinfo) {
+ if ($displayinfo_start == 0) {
+ $displayinfo_start = $t2;
+
+ } else {
+ if ($displayinfo_time < $t2 - $displayinfo_start) {
+ $displayinfo = '';
+ $displayinfo_start = 0;
+ }
+ }
+ }
+
if ($CONF{inter} > $t2 - $t1) {
usleep 10000;
+ # Goto is OK if you don't produce spaghetti code with it
goto TIMEKEEPER;
}
@@ -430,69 +505,13 @@ TIMEKEEPER:
$redraw_background = 0;
}
- } until $sigstop;
-
- return undef;
-}
-
-sub send_message ($$) {
- my ($thread, $message) = @_;
-
- $MSG = $message;
- $thread->kill('USR2');
-
- return undef;
-}
-
-sub set_togglecpu_regexp () {
- $CONF{cpuregexp} = $CONF{togglecpu} ? 'cpu ' : 'cpu';
- return undef;
-}
-
-sub toggle ($$$@) {
- my ($display, $key, $msg, @threads) = @_;
-
- $CONF{$key} = $CONF{$key} == 0 ? 1 : 0;
+ } until $quit;
- $MSG = $msg;
- $display->kill('USR2');
-
- return undef;
-}
-
-sub toggletxt ($$) {
- my ($text, $msg) = @_;
-
- return sub ($@) {
- my ($display, @threads) = @_;
- toggle $display, $text, $msg, @threads;
- };
-}
-
-sub togglecpu ($@) {
- my ($display, @threads) = @_;
-
- $CONF{togglecpu} = $CONF{togglecpu} == 0 ? 1 : 0;
- set_togglecpu_regexp;
-
- $_->kill('USR1') for @threads;
- %AVGSTATS = ();
- %CPUSTATS = ();
- $display->kill('USR1');
-
- return undef;
+ say "Good bye";
+ $event_thread->join();
+ exit 0;
}
-sub set_value (*;*) {
- my ($key, $type) = @_;
-
- print "Please enter new value for $key (old value: $CONF{$key}): ";
- chomp ($CONF{$key} = <STDIN>);
-
- $CONF{$key} = int $CONF{$key} if defined $type and $type eq 'int';
-
- return undef;
-}
sub dispatch_table () {
my $hosts = '';
@@ -514,29 +533,48 @@ Explanation text display:
avg = System load average (desc. order: 1, 5 and 15 min. avg.)
END
- # mode 1: Option is shown in the online help menu
+ # mode 1: Option is shown in the online help menu (stdout not sdl)
# mode 2: Option is shown in the 'usage' screen from the command line
# mode 4: Option is used to generate the GetOptions parameters for Getopt::Long
# Combinations: Like chmod(1)
my %d = (
- average => { menupos => 4, cmd => 'a', help => 'Set number of samples for calculating average loads', mode => 7, type => 'i' },
- configuration => { menupos => 4, cmd => 'c', help => 'Show current configuration', mode => 5 },
- factor => { menupos => 4, cmd => 'f', help => 'Set scale factor (1.0 means 100%)', mode => 7, type => 's' },
- height => { menupos => 3, help => 'Set windows height', mode => 6, type => 'i' },
- help => { menupos => 1, cmd => 'h', help => 'Print this help screen', mode => 3 },
- help2 => { menupos => 2, cmd => 'H', help => 'Print more help text', mode => 1, cb => sub { say $textdesc } },
- hosts => { menupos => 4, help => 'Comma separated list of hosts', var => \$hosts, mode => 6, type => 's' },
- title => { menupos => 4, help => 'Set the window title', var => \$CONF{title}, mode => 6, type => 's' },
- inter => { menupos => 4, cmd => 'i', help => 'Set update interval in seconds (default 0.1)', mode => 7, type => 's' },
- quit => { menupos => 5, cmd => 'q', help => 'Quit', mode => 1, cb => sub { -1 } },
- samples => { menupos => 4, cmd => 's', help => 'Set number of samples until ssh reconnects', mode => 7, type => 'i' },
- sshopts => { menupos => 7, cmd => 'o', help => 'Set SSH options', mode => 7, type => 's' },
- togglecpu => { menupos => 4, cmd => '1', help => 'Toggle CPUs (0 or 1)', mode => 7, type => 'i', cb => \&togglecpu },
- toggletxt => { menupos => 4, cmd => '2', help => 'Toggle all text display (0 or 1)', mode => 7, type => 'i', cb => toggletxt 'toggletxt', MSG_TOGGLE_TXT },
- toggletxthost => { menupos => 4, cmd => '3', help => 'Toggle hostname/num text display (0 or 1)', mode => 7, type => 'i', cb => toggletxt 'toggletxthost', MSG_TOGGLE_TXT_HOST },
- version => { menupos => 3, cmd => 'v', help => 'Print version', mode => 1, cb => sub { say VERSION . ' ' . COPYRIGHT } },
- width => { menupos => 2, help => 'Set windows width', mode => 6, type => 'i' },
+ togglecpu => { menupos => 1, help => 'Toggle CPUs (0 or 1)', mode => 7, type => 'i' },
+ togglecpu_hot => { menupos => 2, cmd => '1', help => 'Toggle CPUs', mode => 1 },
+
+ average => { menupos => 3, help => 'Set number of samples for calculating avg.', mode => 6, type => 'i' },
+ average_hot_up => { menupos => 4, cmd => 'a', help => 'Increases number of samples for calculating avg. by 1', mode => 1 },
+ average_hot_dn => { menupos => 5, cmd => 'y', help => 'Decreases number of samples for calculating avg. by 1', mode => 1 },
+
+ configuration => { menupos => 6, cmd => 'c', help => 'Show current configuration', mode => 4 },
+
+ factor => { menupos => 7, help => 'Set graph scale factor (1.0 means 100%)', mode => 6, type => 's' },
+ factor_hot_up => { menupos => 8, cmd => 's', help => 'Increases graph scale factor by 0.1', mode => 1 },
+ factor_hot_dn => { menupos => 9, cmd => 'x', help => 'Decreases graph scale factor by 0.1', mode => 1 },
+
+ height => { menupos => 10, help => 'Set windows height', mode => 6, type => 'i' },
+
+ help_hot => { menupos => 11, cmd => 'h', help => 'Prints this help screen', mode => 1 },
+
+ hosts => { menupos => 12, help => 'Comma separated list of hosts', var => \$hosts, mode => 6, type => 's' },
+
+ inter => { menupos => 13, help => 'Set update interval in seconds (default 0.1)', mode => 7, type => 's' },
+ inter_hot_up => { menupos => 14, cmd => 'd', help => 'Increases update interval in seconds by 0.1', mode => 1 },
+ inter_hot_dn => { menupos => 15, cmd => 'c', help => 'Decreases update interval in seconds by 0.1', mode => 1 },
+
+ quit_hot => { menupos => 16, cmd => 'q', help => 'Quits', mode => 1 },
+
+ samples => { menupos => 17, help => 'Set number of samples until ssh reconnects', mode => 6, type => 'i' },
+ sshopts => { menupos => 18, help => 'Set SSH options', mode => 6, type => 's' },
+ title => { menupos => 19, help => 'Set the window title', var => \$CONF{title}, mode => 6, type => 's' },
+
+ toggletxthost => { menupos => 20, help => 'Toggle hostname/num text display (0 or 1)', mode => 7, type => 'i' },
+ toggletxthost_hot => { menupos => 21, cmd => 'u', help => 'Toggle hostname/num text display', mode => 1 },
+
+ toggletxt => { menupos => 22, help => 'Toggle text display (0 or 1)', mode => 7, type => 'i' },
+ toggletxt_hot => { menupos => 23, cmd => 't', help => 'Toggle text display', mode => 1 },
+
+ width => { menupos => 24, help => 'Set windows width', mode => 6, type => 'i' },
);
my %d_by_short = map {
@@ -563,27 +601,22 @@ END
if (length $cmd == 1) {
for my $key (grep { exists $d{$_}{cmd} } keys %d) {
- do { $cmd = $key; last } if $d{$key}{cmd} eq $cmd
+ do { $cmd = $key; last } if $d{$key}{cmd} eq $cmd;
}
}
- (exists $cb->{cb} ? $cb->{cb} : sub {
- my $display = shift;
- set_value $cmd;
- send_message $display, MSG_SET_FACTOR if $cmd eq 'factor';
- })->(@args);
-
- } elsif ($arg eq 'help') {
+ } elsif ($arg eq 'hotkeys') {
(join "\n", map {
"$_\t- $d_by_short{$_}{help}"
} grep {
- $d_by_short{$_}{mode} & 1 and exists $d_by_short{$_}{help}
+ $d_by_short{$_}{mode} & 1 and exists $d_by_short{$_}{help};
- } sort { $d_by_short{$a}{menupos} <=> $d_by_short{$b}{menupos} } sort keys %d_by_short);
+ } sort { $d_by_short{$a}{menupos} <=> $d_by_short{$b}{menupos} } sort keys %d_by_short)
+ . "\n$textdesc";
} elsif ($arg eq 'usage') {
- join "\n", map {
+ (join "\n", map {
if ($_ eq 'help') {
"--$_\t\t- $d{$_}{help}"
} else {
@@ -593,20 +626,20 @@ END
} grep {
$d{$_}{mode} & 2 and exists $d{$_}{help}
- } sort { $d{$a}{menupos} <=> $d{$b}{menupos} } sort keys %d;
+ } sort { $d{$a}{menupos} <=> $d{$b}{menupos} } sort keys %d)
+ . "\n$textdesc";
} elsif ($arg eq 'options') {
map {
- "$_=".$d{$_}{type} => (defined $d{$_}{var} ? $d{$_}{var} : \$CONF{$_})
+ "$_=".$d{$_}{type} => (defined $d{$_}{var} ? $d{$_}{var} : \$CONF{$_});
} grep {
- $d{$_}{mode} & 4 and exists $d{$_}{type}
+ $d{$_}{mode} & 4 and exists $d{$_}{type};
} sort keys %d;
}
};
- $d{help}{cb} = sub { say $closure->('help') };
$d{configuration}{cb} = sub {
say sort map {
"$_->[0] = $_->[1]"
@@ -623,67 +656,30 @@ END
return (\$hosts, $closure);
}
-sub create_threads (\@) {
- my ($hosts) = @_;
-
- my @threads;
- push @threads, threads->create('thr_get_stat', $_) for @$hosts;
-
- return (threads->create('thr_display_stats'), @threads);
-}
-
-sub stop_threads (@) {
- for (@_) {
- $_->kill('STOP');
- $_->join();
- }
-
- return undef;
-}
-
sub main () {
my ($hosts, $dispatch) = dispatch_table;
- my $help;
- GetOptions ('help|?' => \$help, $dispatch->('options'));
+ my $usage;
+
+ GetOptions ('help|?' => \$usage, $dispatch->('options'));
- if (defined $help) {
+ if (defined $usage) {
say $dispatch->('usage');
exit 0;
}
set_togglecpu_regexp;
- @HOSTS = split ',', $$hosts;
+ my @hosts = split ',', $$hosts;
- if (@HOSTS) {
+ if (@hosts) {
system 'ssh-add';
} else {
- @HOSTS = 'localhost';
+ @hosts = 'localhost';
}
- my ($display, @threads) = create_threads @HOSTS;
- my $term = new Term::ReadLine VERSION;
-
- say VERSION . ' ' . COPYRIGHT;
- say 'Type \'h\' for h help menu. Or start program with --help for startup options.';
-
- while ( defined( $_ = $term->readline(PROMPT) ) ) {
- $term->addhistory($_);
- chomp;
-
- my ($cmd, @args) = split /\s+/;
- next unless defined $cmd;
- $_ = shift @args if $cmd eq '';
-
- last if $dispatch->('command', $_, $display, @threads);
- }
-
- stop_threads $display, @threads;
-
- say "Good bye";
-
- return 0;
+ my @threads = create_threads @hosts;
+ main_loop $dispatch, @threads;
}
-exit main;
+main;