Subversion Repositories camp_sysinfo_client_3

Rev

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

#!/usr/bin/env perl

my $seedFile = 'sysinfo-client.seed';
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
my $sysinfo3 = '/etc/camp/sysinfo-client.conf';
my $configPath = '/etc/camp';
my $configFile = 'sysinfo-client.conf';

my $clientName = '';
my $serialNumber = '';
my $hostname;
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
my $iSendReports = {};
# the following are keys which are specific to the different transport channels
my @sendEmailScriptKeys = ( 'mailTo', 'mailSubject','mailCC','mailBCC','mailServer','mailFrom','logFile','otherCLParams','tls','smtpUser','smtpPass', 'sendEmailScriptLocation' );
my @sendHTTPKeys = ( 'URL', 'urlVarName' );

sub showConf {
   my $conf;
   $conf .= "\$clientName = '" . $clientName . "';\n";
   $conf .= "\$serialNumber = '" . $serialNumber . "';\n";
   $conf .= "\$hostname = '" . $hostname . "';\n";
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
   foreach my $key ( keys %$iSendReports ) {
      $conf .= "\$\$iSendReports{'$key'} = '" . $$iSendReports{$key} . "';\n";
   }
   return $conf;
}

# 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 ) = @_;
   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 findSendEmail {
   my $currentLocation = shift;
   my @possibles = ( '/opt/sendEmail/sendEmail', '/opt/sendEmail/sendEmail.pl' );
   return $currentLocation if ( $currentLocation && -x $currentLocation );
   # well, we didn't find it, so look around some more
   # we install it in /opt, so try there
   foreach my $current ( @possibles ) {
      return $current if -x $current;
   }
   if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
      $path = `perl getSendEmail.pl`;
      chomp $path;
      return $path;
   }
   return '';
}

sub userConfirmation {
   my $temp;
   
   $clientName = &getAnswer( "Client Name ", ($clientName) );
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
   $hostname = &getAnswer( "Host Name ", ($hostname) );

   $temp = join( ',',@moduleDirs );
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
   @moduleDirs = split( ',', $temp );

   $temp = join( ',',@scriptDirs );
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
   @scriptDirs = split( ',', $temp );

   $temp = &getAnswer( "How do you want to send the reports ", ('sendEmailScript', 'http', 'sendmail' ) );

   if ( $temp eq 'sendEmailScript' ) {
      $$iSendReports{'sendScript'} = 'sendEmailScript';
      $$iSendReports{'sendEmailScriptLocation'} = &findSendEmail( $$iSendReports{'sendEmailScriptLocation'} );
           
      # get rid of http keys
      delete @{$iSendReports} {@sendHTTPKeys};
      for $key ( @sendEmailScriptKeys ) {
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
      }
   } elsif ( $temp eq 'http' ) {
      $$iSendReports{'sendScript'} = 'upload_http.pl';
      # get rid of mail keys
      delete @{$iSendReports} { @sendEmailScriptKeys };
      for $key ( @sendHTTPKeys ) {
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
      }
   } elsif ( $temp eq 'sendmail' ) {
      $iSendReports = {}; # null out the script
   } else {
      $$iSendReports{'sendScript'} = $temp;
      delete @{$iSendReports} { @sendEmailScriptKeys, @sendHTTPKeys };
      print "Unknown script, please manually configure\n";
   }
}

# simply attempts to detect the operating system so we can do OS
# specific actions at the end.   
sub getOperatingSystem {
   my @OSTypes = ( 'ipfire','debian' );
   my $OS = `uname -a`;
   foreach $osType ( @OSTypes ) {
      return $osType if $OS =~ m/$osType/i;
   }
   return '';
} # getOperatingSystem

# special purpose for IPFire routers
sub ipFire {
   my @BACKUP_DIRS = ( '/etc/camp' );
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/fcron.daily/sysinfo.cron` if &yesno( 'Add link to fcron.daily' );
   # now, check for backup directories not in include.user
   open BACKUP, '</var/ipfire/backup/include.user';
   while ( $line = <BACKUP> ) {
      chomp $line;
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
         if ( $BACKUP_DIRS[$i] eq $line ) {
            $BACKUP_DIRS[$i] = '';
            last;
         } # if
      }# for
   } # while
   close BACKUP;

   # if any remain, append them to include.user
   open BACKUP, '>>/var/ipfire/backup/include.user';
   foreach my $backupDir ( @BACKUP_DIRS ) {
      print BACKUP "$backupDir\n" if $backupDir;
   }
   close BACKUP;

   # set all modules with ipfire or unix in the name to run
   opendir $moduleDir, $moduleDirs[0];
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
   closedir $moduleDir;
   foreach my $module (@modules) {
      `chmod 0700 $MODULES_DIR/$module`;
   }
   print "All IPFire specific modules have been enabled\n";
}

# some debian specific things
sub debian {
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo.cron` 
      if &yesno( 'Add link to cron.daily' );
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
      if ( &yesno( 'Back up old configuration? ') ) {
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
         print "Old configuration copied to /root/sysinfo.2.config.tgz\n";
      }
      `apt-get -y --purge remove sysinfo-client`;
      print "There may be some unused packages now. You can remove them with\napt-get autoremove\n\n";
   }
   `ln -s  /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client` 
      if &yesno( 'Would you like a link to /usr/local/bin/sysinfo-client' );
   # set all modules with debian or unix in the name to run
   opendir  $moduleDirs, $moduleDirs[0];
   my @modules = grep { /^((dpkg)|(unix))/ } readdir $moduleDir;
   closedir $moduleDir;
   foreach my $module (@modules) {
      `chmod 0700 $MODULES_DIR/$module`;
   }
   print "All debian specific modules have been enabled\n";
}

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;
   $return .= "Configuration successfully written to $filename\n";
   return $return;
}

# See if existing configuration. If so, offer to load it
if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
   print "Loading defaults from existing config $sysinfo3\n";
   my $client_name;
   open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
   my $temp = join( '', <SEED> );
   close SEED;
   eval( $temp );
} else { # nope, so see if an old sysinfo2 or a seed file exists
   if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
      print "Loading defaults from sysinfo2 config $sysinfo2\n";
      my $client_name;
      open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
      my $temp = join( '', <SEED> );
      close SEED;
      eval( $temp );
      $clientName = $client_name if ( $client_name );
   }

   if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
      print "Loading seed file $seedFile\n";
      open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
      my $temp = join( '', <SEED> );
      close SEED;
      eval( $temp );
   }
}

unless ( $hostname ) {
   $hostname = `hostname -f`;
   chomp $hostname;
}

&userConfirmation();

print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
print &showConf(  );
print '-'x40 . "\n";
print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );


my $os = &getOperatingSystem();
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
   &ipFire() if $os eq 'ipfire';
   &debian() if $os eq 'debian';
}

print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";

1;