Subversion Repositories camp_sysinfo_client_3

Rev

Blame | Last modification | View Log | RSS feed

#!/usr/bin/env perl

package sysinfoconf;

use warnings;
use Exporter;

our @ISA = qw( Exporter );
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
                  $transports $sysinfo3 %sendTypes $binDir $modulesDir
                  $scriptsDir $confDir $binName $confName
                  &showConf &transportsToConfig &getAnswer &yesno  
                  &writeConfig &processParameters
                );


$clientName = '';
$serialNumber = '';
$hostname = '';
@moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
@scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
$transports = {}; # holds transportation mechanisms
# the following are keys which are specific to the different transport channels

$sysinfo3 = '/etc/camp/sysinfo-client/sysinfo-client.conf';

$binDir = '/opt/camp/sysinfo-client';
$modulesDir = $binDir . '/modules';
$scriptsDir = $binDir . '/scripts';
$confDir = '/etc/camp/sysinfo-client';

$binName = 'sysinfo-client';
$confName = 'sysinfo-client.conf';


%sendTypes = ( 
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
                                      'keys' => 
                                      [
                                        'mailTo',
                                        'mailSubject',
                                        'mailCC',
                                        'mailBCC',
                                        'mailServer',
                                        'mailFrom',
                                        'logFile',
                                        'otherCLParams',
                                        'tls',
                                        'smtpUser',
                                        'smtpPass',
                                        'sendEmailScriptLocation'
                                      ],
                                    },
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
                                      'keys' => 
                                      [
                                        'URL',
                                        'key for report',
                                        'key for date',
                                        'key for hostname',
                                        'key for client',
                                        'key for serial number'
                                      ]
                                    }
                );


sub showConf {
   my $conf;
   $conf .= "\$clientName = '" . $clientName . "';\n";
   $conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
   $conf .= "\$hostname = '" . $hostname . "';\n";
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
   $conf .= &transportsToConfig();
   return $conf;
}

sub transportsToConfig {
   my $config;
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
      my $name = $$transports{ $priority }{'-name-'};
      $config .= "# Tranport $name at priority $priority\n";
      my $thisOne = $$transports{ $priority };
      foreach my $key ( sort keys %$thisOne ) {
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
      } # foreach
   } # foreach
   return $config;
} # transportsToConfig

# prompt the user for a response, then allow them to enter it
# the first response is considered the default and is printed
# in all caps if more than one exists
# first answer is used if they simply press the Enter
# key. The response is returned all lower case if more than one
# exists.
# it is assumed 
sub getAnswer {
   my ( $prompt, @answers ) = @_;
   $answers[0] = '' unless defined( $answers[0] );
   my $default = $answers[0];
   my $onlyOneAnswer = scalar( @answers ) == 1;
   print $prompt . '[ ';
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
   print join( ' | ', @answers ) . ' ]: ';
   $thisAnswer = <>;
   chomp $thisAnswer;
   $thisAnswer = $default unless $thisAnswer;
   return $thisAnswer;
}

sub yesno {
   my $prompt = shift;
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
}

sub writeConfig {
   my ($path,$filename,$content) = @_;
   my $return;
   `mkdir -p $path` unless -d $path;
   $filename = $path . '/' . $filename;
   if ( -e $filename ) {
      `cp $filename $filename.bak` if ( -e $filename );
      $return .= "Old config copied to $filename.bak\n";
   }
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
   print CONF $content;
   close CONF;
   `chmod 600 $filename`;
   $return .= "Configuration successfully written to $filename\n";
   return $return;
}

sub processParameters {
   while ( my $parameter = shift ) {
      if ( $parameter eq '-v' ) {
         print "$main::VERSION\n";
         exit;
      }
   } # while
}



1;