Subversion Repositories camp_sysinfo_client_3

Rev

Rev 223 | Rev 228 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/env perl

# v1.2.0 20161022 RWR
# moved makeConfig here so it is usable by install and configure
#
# v1.3.0 20190108 RWR
# added UUID and set defaults for config file
#
# v1.4.0 20190204 RWR
# added autoconvert from v1.2 configuration file to v1.3
# using dumper to set configuration file
#
# version 2.0 20190330 RWR
# changed it so all configs are YAML
#
# version 2.1 20191101 RWR
# changed --name-- tag to name
#
# version 2.2 20191105 RWR
# fixed where dmidecode missing caused exception
#
# version 2.2.1 20191112 RWR
# added timeStamp
#
# version 2.3.0 20220609 RWR
# moved loadConfigurationFile and timeStamp here

package sysinfoconf;


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

#use Data::Dumper;
use YAML::Tiny;
use File::Basename;
use Data::Dumper;

use Exporter;

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

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

my %serialNumberTranslation = ( 
   'notspecified' => '', # not set in DMI
   '0123456789' => '' # used by some SuperMicro computers
);

my %UUIDTranslation = ( 
   '03000200-0400-0500-0006-000700080009' => '', # some routers
   'sot settable' => '' # not set in DMI
);

# paths to search for configuration file
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
my $serverInfoFileName = '/etc/server.info';

my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file


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

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

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.yaml';

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'
                                      ]
                                    },
                  'Save Local' =>   { 'sendScript' => 'save_local',
                                      'keys' =>
                                      [
                                         'output directory'
                                      ]
                                   }
                );


#######################################################
#
# timeStamp
#
# return current system date as YYYY-MM-DD HH:MM:SS
#
#######################################################
sub timeStamp {
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
}
   

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;
   $configuration = Dump($configuration);
   return $configuration;
}

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 readServerInfo {
   my $serverInfoName = shift;
   my %return;

   print "Reading server info from $serverInfoName\n";
   
   if ( -f $serverInfoName ) {
      print "found $serverInfoName\n";
      open DATA, $serverInfoName or die "Could not read $serverInfoName: $!\n";
      print "opened $serverInfoName\n";
      while ( my $line = <DATA> ) {
         next if $line =~ m/^#/;
         chomp $line;
         my ($key,$value) = split( ':', $line );
         $key =~ s/^\s+|\s+$//g;
         $value =~ s/^\s+|\s+$//g;
         # note, we make the key lower case so we can find it
         $return{ lc $key} = $value;
      }
   }
   return \%return;
}


sub makeConfig {
   my $configFile = shift;
   my $config = {}; # make sure it is a ref to a hash so mergeHash can recognize it
   my $serverInfo = readServerInfo( $serverInfoFileName );
   # die Dumper( $serverInfo );
   if ( -f $configFile ) {
      print "Processing config file $configFile\n";
      my $yaml = YAML::Tiny->read( $configFile );
      $config = $yaml->[0];
   }

   # Fill in any values we are missing but which are found in $serverInfo (/etc/server.info)
   $config->{'clientName'} = defined( $serverInfo->{'owner'} ) ? $serverInfo->{'owner'} : '' unless $config->{'clientName'};
   $config->{'hostname'} = defined( $serverInfo->{'hostname'} ) ? $serverInfo->{'hostname'} : '' unless $config->{'hostname'};
   $config->{'serialNumber'} = defined( $serverInfo->{'serial'} ) ? $serverInfo->{'serial'} : '' unless $config->{'serialNumber'};
   $config->{'UUID'} = defined( $serverInfo->{'uuid'} ) ? $serverInfo->{'uuid'} : '' unless $config->{'UUID'};
   $config->{'location'} = defined( $serverInfo->{'location'} ) ? $serverInfo->{'location'} : '' unless $config->{'location'};
   $config->{'os_type'} = defined( $serverInfo->{'os_type'} ) ? $serverInfo->{'os_type'} : '' unless $config->{'os_type'};
   $config->{'tags'} = defined( $serverInfo->{'tags'} ) ? [ split ',', $serverInfo->{'tags'} ] : [] unless $config->{'tags'};
   
   # if these are still missing, we can try to run a program to get the values
   unless ( $config->{'hostname'} ) {
      $config->{'hostname'} = `hostname -f`;
      chomp $config->{'hostname'};
   }
   unless ( $config->{'serialNumber'} ) {
      $config->{'serialNumber'} = `dmidecode -s system-serial-number` if `which dmidecode`;
      chomp $config->{'serialNumber'};
      $config->{'serialNumber'} =~ s/\s//gi;
      $config->{'serialNumber'} = $serialNumberTranslation{lc($config->{'serialNumber'})} if defined( $serialNumberTranslation{lc($config->{'serialNumber'})} );
   }
   unless ( $config->{'UUID'} ) {
      $config->{'UUID'} = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '' if `which dmidecode`;
      $config->{'UUID'} =~ s/\s//gi;
      $config->{'UUID'} = $UUIDTranslation{lc($config->{'UUID'})} if defined( $UUIDTranslation{lc($config->{'UUID'})} );
   }
   unless ( $config->{'os_type'} ) {
      $config->{'os_type'} = `installer/determineOS` if -x 'installer/determineOS';
   }
   
   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';
}

sub writeConfig {
   use File::Temp qw / tempfile /;
   my ( $filename,$content ) = @_;
   if ( $filename ) { # they sent us a filename
      my $path;
      ($filename, $path ) = fileparse( $filename );
      `mkdir -p $path` unless -d $path;
      $filename = $path . '/' . $filename;
      `cp $filename $filename.bak` if ( -e $filename );
      unless ( $dryRun ) {
         open CONF,">$filename" or die "Could not write to $filename: $!\n";
         print CONF $content;
         close CONF;
         `chmod 600 $filename`;
      }
   } else { # no path provided, so just create a temp file
      # we will create a temporary file and return the name
      # it is the calling programs responsiblity to remove the file
      my $fh;
      ($fh,$filename) = tempfile( UNLINK => 0 );
      print $fh $content;
      close $fh
   }
   return $filename;
}

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

sub findConf {
   my $confName = shift;
   for ( my $i = 0; $i < @confDirs; $i++ ) {
      if ( -d $confDirs[ $i ] ) {
         return ( $confDirs[$i],  $confName );
      }
   }
   return ( '', $confName );
}

#######################################################
#
# findFile( $filename, @directories )
#
# Locates a file by searching sequentially in one or more
# directories, returning the first one found
# 
# Returns '' if not found
#
#######################################################

sub findFile {
   my ( $filename, $directories ) = @_;
   &logIt( 3, "Looking for $filename in findFile" );
   for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
      my $confFile = $$directories[$i] . '/' . $filename;
      &logIt( 4, "Looking for $filename in $confFile" );
      return $confFile if ( -f $confFile );
   }
   return '';
}
   

#######################################################
#
# loadConfigurationFile($confFile)
#
# Loads configuration file defined by $configurationFile, and dies if not available
# This is a YAML file containing serialized contents of 
# Parameters:
#    $$fileName - name of file to look for (reference)
#    @searchPath - array of paths to find $filename
#
#######################################################

sub loadConfigurationFile {   
   my ( $fileName, @searchPath ) = @_;
   $$fileName = $configurationFile unless $$fileName;
   @searchPath = @confFileSearchPath unless @searchPath;
   &logIt( 2, "Looking for config file $$fileName in " . join( ', ', @searchPath ) );
   my $confFile;
   if ( $confFile = &findFile( $$fileName, \@searchPath ) ) {
      &logIt( 3, "Opening configuration from $confFile" );
      my $yaml = YAML::Tiny->read( $confFile );
      &logIt( 4, "Configuration file contents\n$yaml" );
      $$fileName = $confFile;
      return $yaml->[0];
   }
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
}


#######################################################
# function to simply log things
# first parameter is the priority, if <= $logDef->{'log level'} will print
# all subsequent parameters assumed to be strings to sent to the log
# returns 0 on failure
#         1 on success
#         2 if priority > log level
#        -1 if $logDef is unset
# currently, only logs to a file
#######################################################
sub logIt {
   my $priority = shift;

   # turn off variable checking so it doesn't blow up on lack of %configuration file
   no strict 'vars';
   
   return -1 unless exists $configuration{'logging'};
   return 2 unless $priority <= $configuration{'logging'}{'log level'};
   if ( $configuration{'logging'}{'log type'} eq 'cache' ) {
      push @{ $configuration{'logging'}{'cache'} }, @_;
      return;
   } elsif ( defined( $configuration{'logging'}{'cache'} ) ) {
      unshift @_, @{ $configuration{'logging'}{'cache'} };
      delete $configuration{'logging'}{'cache'};
   }
   if ( $configuration{'logging'}{'log type'} eq 'file' ) {
      if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
         while ( my $t = shift ) {
            print LOG &timeStamp() . "\t$t\n";
         }
         close LOG;
      }
   } elsif ( $configuration{'logging'}{'log type'} eq 'syslog' ) {
      use Sys::Syslog;                        # all except setlogsock()
      use Sys::Syslog qw(:standard :macros);  # standard functions & macros

      my $syslogName = 'sysinfo-client';
      my $logopt = 'nofatal';
      my $facility = 'LOG_LOCAL0';
      my $priority = 'LOG_NOTICE';

      openlog( $syslogName, $logopt, $facility);
      syslog($priority, '%s', @_ );
      closelog();
   } else {
      warn "Log type $configuration{'logging'} incorrectly configured\n";
      return 0;
   }
   return 1;
}



1;