diff options
| -rw-r--r-- | lib/Loadbars/Config.pm | 140 | ||||
| -rw-r--r-- | lib/Loadbars/Utils.pm | 41 |
2 files changed, 181 insertions, 0 deletions
diff --git a/lib/Loadbars/Config.pm b/lib/Loadbars/Config.pm new file mode 100644 index 0000000..430c31d --- /dev/null +++ b/lib/Loadbars/Config.pm @@ -0,0 +1,140 @@ +package Loadbars::Config; + +use strict; +use warnings; + +use Loadbars::Utils; + +use Exporter; + +use base 'Exporter'; + +our @EXPORT = qw ( %C %I ); + +# Global configuration hash +our %C : shared; + +# Global configuration hash for internal settings (not configurable) +our %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, +); + +sub read () { + 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 () { + 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; +} + +# Recursuve function +sub get_cluster_hosts ($;$); + +sub get_cluster_hosts ($;$) { + my ( $cluster, $recursion ) = @_; + + unless ( defined $recursion ) { + $recursion = 1; + + } + elsif ( $recursion > Loadbars::Constants->CSSH_MAX_RECURSION ) { + 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 ); + my $hosts; + + while (<$fh>) { + if (/^$cluster\s*(.*)/) { + $hosts = $1; + last; + } + } + + close $fh; + + unless ( defined $hosts ) { + error( "No such cluster in " + . Loadbars::Constants->CSSH_CONFFILE + . ": $cluster" ) + unless defined $recursion; + + return ($cluster); + } + + my @hosts; + push @hosts, get_cluster_hosts $_, ( $recursion + 1 ) + for ( split /\s+/, $hosts ); + + return @hosts; +} + +1; diff --git a/lib/Loadbars/Utils.pm b/lib/Loadbars/Utils.pm new file mode 100644 index 0000000..bfb8027 --- /dev/null +++ b/lib/Loadbars/Utils.pm @@ -0,0 +1,41 @@ +package Loadbars::Utils; + +use strict; +use warnings; + +use Exporter; + +use base 'Exporter'; + +our @EXPORT = qw ( + debugsay + display_info + display_info_no_nl + display_warn + newline + notnull + null + say + sum + trim +); + +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 error ($) { die shift, "\n" } + +sub trim (\$) { + my $str = shift; + $$str =~ s/^[\s\t]+//; + $$str =~ s/[\s\t]+$//; + return undef; +} +sub display_info_no_nl ($) { print "==> " . (shift) . ' ' } +sub display_info ($) { say "==> " . shift } +sub display_warn ($) { say "!!! " . shift } + +1; |
