Subversion Repositories camp_sysinfo_client_3

Rev

Rev 53 | Blame | Last modification | View Log | RSS feed

#!/usr/bin/env perl

# v1.2.0 20161022 RWR
# moved makeConfig here so it is usable by install and configure

package sysinfoconf;


our $VERSION = '1.2.0';
use warnings;
use strict;  

use Data::Dumper;
use File::Basename;

use Exporter;

our @ISA = qw( Exporter );
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
                  $scriptDir $confDir $binName $confName
                  &showConf &transportsToConfig &getAnswer &yesno  
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
                  &runCommand &setDryRun &enableModules &makeConfig &findConf
                );

our $TESTING = 0;
our $dryRun;
our $clientName = '';
our $serialNumber = '';
our $hostname = '';

my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );


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

our $sysinfo3 = 'sysinfo-client.conf';

our $binDir = '/opt/camp/sysinfo-client';
our $moduleDir = $binDir . '/modules';
our $scriptDir = $binDir . '/scripts';
our $confDir = '/etc/camp/sysinfo-client';

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

sub setDryRun {
   $dryRun = shift;
}

our %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 enableModules {
   my $moduleDir = shift;
   my %modules;
   if ( opendir( my $dh, "$moduleDir" ) ) {
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
                 grep { ! /^\./ } 
                 readdir $dh;
      closedir( $dh );
      foreach my $filename ( keys %modules ) {
         if ( open FILE,"<$moduleDir/$_" ) {
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
            close FILE;
            chomp @descript;
            $modules{$filename}{'description'} = $descript[0];
         } # if
      } # foreach
   } # if
} # enableModules

sub showConf {
   my $configuration = shift;
#   print Dumper( $configuration ); die;
   my $conf;
   $conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
   $conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
   $conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
   $conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
   $conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
   $conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
   return $conf;
}

sub transportsToConfig {
   my $transports = shift;
   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

sub makeConfig {
   my @configFileNames = @_;
   my %config;
   my $clientName;
   my $serialNumber;
   my $hostname;
   my @moduleDirs;
   my @scriptDirs;
   my $transports = {};

   foreach my $config ( @configFileNames ) {
      open CONF,"<$config" or die "could not open $config: $!\n";
      my $contents = join( '', <CONF> );
      close CONF;
      # now, eval the information we just read.
      # NOTE: we must turn off strict while doing this, and we die if something breaks.
      no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
   }
   unless ( $hostname ) {
      $hostname = `hostname -f`;
      chomp $hostname;
   }
   unless ( $serialNumber ) {
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
      chomp $serialNumber;
      $serialNumber =~ s/\s//gi;
   }


   $config{'clientName'} = $clientName;
   $config{'serialNumber'} = $serialNumber;
   $config{'hostname'} = $hostname;
   $config{'moduleDirs'} = [ @moduleDirs ];
   $config{'scriptDirs'} = [ @scriptDirs ];
   $config{'transports'} = $transports;
   return \%config;
}

# 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 ) . ' ]: ';
   my $thisAnswer = <>;
   chomp $thisAnswer;
   $thisAnswer = $default unless $thisAnswer;
   return $thisAnswer;
}

sub yesno {
   my ( $prompt, $default ) = @_;
   $default = 'yes' unless $default;
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
}

# runs a system command. Also, if in testing mode, simply shows what
# would have been done.
sub runCommand {
   while ( my $command = shift ) {
      if ( $dryRun ) {
         print "$command\n";
      } else {
         `$command`;
      }
   }
   return 1;
} # runCommand

# checks if a directory exists and, if not, creates it
my %directories; # holds list of directories already created so no need to do an I/O

sub checkDirectoryExists {
   my ( $filename,$mod,$owner ) = @_;
   $mod = "0700" unless $mod;
   $owner = "root:root" unless $owner;
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
   my ($fn, $dirname) = fileparse( $filename );
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
   return '' if exists $directories{$dirname};
   if ( -d $dirname ) {
      $directories{$dirname} = 1;
      return '';
   }
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
      $directories{$dirname} = 1;
   }
   return '';   
}

sub writeConfig {
   my ( $filename,$content ) = @_;
   my $path;
   ($filename, $path ) = fileparse( $filename );

   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";
   }
   if ( $dryRun ) {
      $return .= "Not writing to configuration file\n";
   } else {
      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 "$VERSION\n";
         exit;
      }
   } # while
}

sub findConf {
   my $confName = shift;
   my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
   for ( my $i = 0; $i < @confDirs; $i++ ) {
      if ( -d $confDirs[ $i ] ) {
         return ( $confDirs[$i],  $confName );
      }
   }
}
   

1;