Rev 14 | Blame | Last modification | View Log | RSS feed
#!/usr/bin/env perl
use warnings;
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 $transports = {}; # holds transportation mechanisms
# the following are keys which are specific to the different transport channels
my %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 ) {
# if they are wanting variable substitution, surround with double quote
# otherwise, use single quote
if ( $$thisOne{$key} =~ m/(\$hostname)|(\$reportDate)|(\$clientName)|(\$serialNumber)/ ) {
$config .= "\$\$transports{$priority}{'$key'} = \"$$thisOne{$key}\";\n";
} else {
$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 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 );
}
# 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
my $moduleDir = $moduleDirs[0];
opendir $moduleDir, $moduleDir;
my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
closedir $moduleDir;
foreach my $module ( @modules) {
`chmod 0700 $moduleDir/$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`
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
foreach my $directory ( @moduleDirs ) {
opendir ( $dh, $directory ) or die "Could not open directory [$directory]\n";
my @modules = grep { /^((dpkg)|(unix))/ } readdir $dh;
closedir $dh;
foreach my $module (@modules) {
`chmod 0700 $directory/$module`;
`chown root:root $directory/$module`;
}
}
print "All debian specific modules have been enabled\n";
}
sub setUpTransport {
my ($priority, $transport, $fields, $type ) = @_;
$priority = getAnswer( 'Priority', $priority );
$$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
$$transport{'-name-'} = $type unless $$transport{'-name-'};
my $allKeys = $$fields{'keys'};
foreach my $key ( @$allKeys ) {
if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
$temp = &findSendEmail( $$transport{$key} );
$$transport{$key} = $temp if $temp;
}
$$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
}
return ( $priority, $transport );
}
sub showAllTransports {
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
print '='x40 . "\n";
&showTransports( $priority, $$transports{ $priority } );
print '='x40 . "\n";
}
}
sub showTransports {
my ( $priority,$thisOne ) = @_;
print $$thisOne{'-name-'} . " has priority $priority\n";
foreach my $key ( sort keys %$thisOne ) {
next if $key =~ m/^-.*-$/;
print "$key = $$thisOne{$key}\n";
}
}
sub doTransports {
my ( $transports ) = @_;
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
if ( &yesno( $$transports{$priority}{'-name-'} . " has a priority of $priority, edit it?") ) {
#print Dumper( $sendTypes{$$transports{$priority}{'-name-'}} );
#die;
my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'-name-'}} );
if ( $newpriority != $priority ) {
delete $$transports{$priority};
$priority = $newpriority;
}
$$transports{$priority} = $temp;
} # if
}
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
print '='x40 . "\n";
&showTransports( $priority, $$transports{ $priority } );
print '='x40 . "\n";
}
while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
$newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
$$transports{$priority} = $temp;
}
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
print '='x40 . "\n";
&showTransports( $priority, $$transports{ $priority } );
print '='x40 . "\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;
}
sub convertSysinfo2 {
my $client_name;
my $iMailResults;
my $mailTo;
my $mailSubject;
my $mailCC;
my $mailBCC;
my $mailServer;
my $mailServerPort;
my $mailFrom;
my $SENDMAIL;
my $clientName;
my $serialNumber;
my $hostname;
my $transports = {}; # holds transportation mechanisms
open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
my $temp = join( '', <SEED> );
close SEED;
eval( $temp );
if ( $iMailResults ) {
$temp = {};
$$temp{'-name-'} = 'SendEmail';
$$temp{'mailTo'} = $mailTo if $mailTo;
$$temp{'$mailFrom'} = $mailFrom if $mailFrom;
$$temp{'mailCC'} = $mailCC if $mailCC;
$$temp{'mailServer'} = $mailServer if $mailServer;
$$temp{'mailServer'} .= ":$mailServerPort" if $mailServerPort;
$$transports{'1'} = $temp;
}
$clientName = $client_name if ( $client_name );
return ( $clientName,$hostname,$serialNumber,$transports );
}
sub loadConfig {
# 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";
# NOTE: the return values are all placed into globals!!!!
( $clientName,$hostname,$serialNumber,$transports ) = &convertSysinfo2();
}
# seed files are expected to be in the correct format already
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 );
}
}
}
&loadConfig();
unless ( $hostname ) {
$hostname = `hostname -f`;
chomp $hostname;
}
&userConfirmation();
&doTransports( $transports );
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;