summaryrefslogtreecommitdiff
path: root/lib/Loadbars/Config.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Loadbars/Config.pm')
-rw-r--r--lib/Loadbars/Config.pm142
1 files changed, 142 insertions, 0 deletions
diff --git a/lib/Loadbars/Config.pm b/lib/Loadbars/Config.pm
new file mode 100644
index 0000000..57fad2f
--- /dev/null
+++ b/lib/Loadbars/Config.pm
@@ -0,0 +1,142 @@
+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;