From 87baf9f357728a69c8b84cd0154ad3f3e4dac2b6 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 19 Apr 2012 22:53:12 +0200 Subject: some restructuring --- Loadbars/Constants.pm | 0 Loadbars/HelpDispatch.pm | 12 ++-- Loadbars/Main.pm | 145 ++++++++--------------------------------------- Loadbars/Shared.pm | 28 ++++++--- Makefile | 1 + loadbars | 2 +- 6 files changed, 54 insertions(+), 134 deletions(-) mode change 100755 => 100644 Loadbars/Constants.pm diff --git a/Loadbars/Constants.pm b/Loadbars/Constants.pm old mode 100755 new mode 100644 diff --git a/Loadbars/HelpDispatch.pm b/Loadbars/HelpDispatch.pm index 1c28c29..dfee7f5 100644 --- a/Loadbars/HelpDispatch.pm +++ b/Loadbars/HelpDispatch.pm @@ -4,6 +4,7 @@ use strict; use warnings; use Loadbars::Constants; +use Loadbars::Shared; sub create () { my $hosts = ''; @@ -111,7 +112,7 @@ END cluster => { menupos => 6, help => 'Cluster name from /etc/clusters', - var => \$Loadbars::Main::C{cluster}, + var => \$C{cluster}, mode => 6, type => 's' }, @@ -316,7 +317,8 @@ END . $d{$_}{type} => ( defined $d{$_}{var} ? $d{$_}{var} - : \$Loadbars::Main::C{$_} ); + : \$C{$_} + ); } grep { $d{$_}{mode} & 4 and exists $d{$_}{type}; @@ -333,9 +335,11 @@ END defined $_->[1] } map { - [ $_ => exists $d{$_}{var} + [ + $_ => exists $d{$_}{var} ? ${ $d{$_}{var} } - : $Loadbars::Main::C{$_} ] + : $C{$_} + ] } keys %d; }; diff --git a/Loadbars/Main.pm b/Loadbars/Main.pm index 10b039e..210f07a 100644 --- a/Loadbars/Main.pm +++ b/Loadbars/Main.pm @@ -4,6 +4,8 @@ package Loadbars::Main; use strict; use warnings; +use Loadbars::Config; + use SDL; use SDL::App; use SDL::Rect; @@ -20,65 +22,13 @@ use threads; use threads::shared; use Loadbars::Constants; +use Loadbars::Utils; +use Loadbars::Shared; $| = 1; -my %PIDS : shared; -my %AVGSTATS : shared; -my %CPUSTATS : shared; -my %MEMSTATS : shared; -my %MEMSTATS_HAS : shared; - -#my %NETSTATS : shared; -#my %NETSTATS_HAS : shared; - -# Global configuration hash -my %C : shared; - -# Global configuration hash for internal settings (not configurable) -my %I : shared; - -# Setting defaults -%C = ( - average => 15, - barwidth => 35, - extended => 0, - factor => 1, - height => 230, - maxwidth => 1280, - samples => 1000, - showcores => 0, - showmem => 0, - showtext => 1, - showtexthost => 0, - sshopts => '', -); - -%I = ( - cpuregexp => 'cpu', - showtextoff => 0, -); - -# Quick n dirty helpers -sub say (@) { print "$_\n" for @_; return undef } -sub newline () { say ''; return undef } -sub debugsay (@) { say "Loadbars::DEBUG: $_" for @_; return undef } -sub sum (@) { my $sum = 0; $sum += $_ for @_; return $sum } -sub null ($) { defined $_[0] ? $_[0] : 0 } -sub notnull ($) { $_[0] != 0 ? $_[0] : 1 } -sub set_showcores_regexp () { $I{cpuregexp} = $C{showcores} ? 'cpu' : 'cpu ' } -sub error ($) { die shift, "\n" } -sub display_info_no_nl ($) { print "==> " . (shift) . ' ' } -sub display_info ($) { say "==> " . shift } -sub display_warn ($) { say "!!! " . shift } - -sub trim (\$) { - my $str = shift; - - $$str =~ s/^[\s\t]+//; - $$str =~ s/[\s\t]+$//; - - return undef; +sub set_showcores_regexp () { + $I{cpuregexp} = $C{showcores} ? 'cpu' : 'cpu '; } sub percentage ($$) { @@ -106,65 +56,11 @@ sub parse_cpu_line ($) { $load{guest} = 0 unless defined $load{guest}; $load{TOTAL} = - sum @load{qw(user nice system idle iowait irq softirq steal guest)}; + sum( @load{qw(user nice system idle iowait irq softirq steal guest)} ); return ( $name, \%load ); } -sub read_config () { - return unless -f Loadbars::Constants->CONFFILE; - - display_info "Reading configuration from " . Loadbars::Constants->CONFFILE; - open my $conffile, Loadbars::Constants->CONFFILE - or die "$!: " . Loadbars::Constants->CONFFILE . "\n"; - - while (<$conffile>) { - chomp; - s/[\t\s]*?#.*//; - - next unless length; - - my ( $key, $val ) = split '='; - - unless ( defined $val ) { - display_warn "Could not parse config line: $_"; - next; - } - - trim $key; - trim $val; - - if ( not exists $C{$key} ) { - display_warn "There is no such config key: $key, ignoring"; - - } - else { - display_info -"Setting $key=$val, it might be overwritten by command line params."; - $C{$key} = $val; - } - } - - close $conffile; -} - -sub write_config () { - display_warn "Overwriting config file " . Loadbars::Constants->CONFFILE - if -f Loadbars::Constants->CONFFILE; - - open my $conffile, '>', Loadbars::Constants->CONFFILE or do { - display_warn "$!: " . Loadbars::Constants->CONFFILE; - - return undef; - }; - - for ( keys %C ) { - print $conffile "$_=$C{$_}\n"; - } - - close $conffile; -} - sub terminate_pids (@) { my @threads = @_; @@ -480,7 +376,7 @@ sub main_loop ($@) { } elsif ( $key_name eq 'w' ) { - write_config; + Loadbars::Config::write; } elsif ( $key_name eq 'a' ) { @@ -751,9 +647,11 @@ sub main_loop ($@) { $rect_peak, $max_all > Loadbars::Constants->USER_ORANGE ? Loadbars::Constants->ORANGE - : ( $max_all > Loadbars::Constants->USER_YELLOW0 + : ( + $max_all > Loadbars::Constants->USER_YELLOW0 ? Loadbars::Constants->YELLOW0 - : ( Loadbars::Constants->YELLOW ) ) + : ( Loadbars::Constants->YELLOW ) + ) ); } @@ -761,9 +659,11 @@ sub main_loop ($@) { $rect_user, $all > Loadbars::Constants->USER_ORANGE ? Loadbars::Constants->ORANGE - : ( $all > Loadbars::Constants->USER_YELLOW0 + : ( + $all > Loadbars::Constants->USER_YELLOW0 ? Loadbars::Constants->YELLOW0 - : ( Loadbars::Constants->YELLOW ) ) + : ( Loadbars::Constants->YELLOW ) + ) ); $app->fill( $rect_system, $cpuaverage->{system} > Loadbars::Constants->SYSTEM_BLUE0 @@ -985,12 +885,13 @@ sub get_cluster_hosts ($;$) { } elsif ( $recursion > Loadbars::Constants->CSSH_MAX_RECURSION ) { - error "CSSH_MAX_RECURSION reached. Infinite circle loop in " - . Loadbars::Constants->CSSH_CONFFILE . "?"; + error( "CSSH_MAX_RECURSION reached. Infinite circle loop in " + . Loadbars::Constants->CSSH_CONFFILE + . "?" ); } open my $fh, Loadbars::Constants->CSSH_CONFFILE - or error "$!: " . Loadbars::Constants->CSSH_CONFFILE; + or error( "$!: " . Loadbars::Constants->CSSH_CONFFILE ); my $hosts; while (<$fh>) { @@ -1003,9 +904,9 @@ sub get_cluster_hosts ($;$) { close $fh; unless ( defined $hosts ) { - error "No such cluster in " - . Loadbars::Constants->CSSH_CONFFILE - . ": $cluster" + error( "No such cluster in " + . Loadbars::Constants->CSSH_CONFFILE + . ": $cluster" ) unless defined $recursion; return ($cluster); diff --git a/Loadbars/Shared.pm b/Loadbars/Shared.pm index df2abf6..0f02b7e 100644 --- a/Loadbars/Shared.pm +++ b/Loadbars/Shared.pm @@ -1,19 +1,33 @@ package Loadbars::Shared; -my %PIDS : shared; -my %AVGSTATS : shared; -my %CPUSTATS : shared; -my %MEMSTATS : shared; -my %MEMSTATS_HAS : shared; +use Exporter; + +use base 'Exporter'; + +our @EXPORT = qw( + %PIDS + %AVGSTATS + %CPUSTATS + %MEMSTATS + %MEMSTATS_HAS + %C + %I +); + +our %PIDS : shared; +our %AVGSTATS : shared; +our %CPUSTATS : shared; +our %MEMSTATS : shared; +our %MEMSTATS_HAS : shared; #my %NETSTATS : shared; #my %NETSTATS_HAS : shared; # Global configuration hash -my %C : shared; +our %C : shared; # Global configuration hash for internal settings (not configurable) -my %I : shared; +our %I : shared; # Setting defaults %C = ( diff --git a/Makefile b/Makefile index 813e4f1..ac6170f 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ perltidy: find . -name \*.pm | xargs perltidy -b find . -name \*.pl | xargs perltidy -b + find . -name \*.bak -delete diff --git a/loadbars b/loadbars index e004e86..870fd78 100755 --- a/loadbars +++ b/loadbars @@ -19,7 +19,7 @@ sub main () { Loadbars::Main::say(Loadbars::Constants->VERSION . ' ' . Loadbars::Constants->COPYRIGHT); - Loadbars::Main::read_config; + Loadbars::Config::read; GetOptions( 'help|?' => \$usage, $dispatch->('options') ); -- cgit v1.2.3