Subversion Repositories camp_sysinfo_client_3

Rev

Rev 161 | Rev 197 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 161 Rev 194
Line 19... Line 19...
19
# version 2.2 20191105 RWR
19
# version 2.2 20191105 RWR
20
# fixed where dmidecode missing caused exception
20
# fixed where dmidecode missing caused exception
21
#
21
#
22
# version 2.2.1 20191112 RWR
22
# version 2.2.1 20191112 RWR
23
# added timeStamp
23
# added timeStamp
-
 
24
#
-
 
25
# version 2.3.0 20220609 RWR
-
 
26
# moved loadConfigurationFile and timeStamp here
24
 
27
 
25
package sysinfoconf;
28
package sysinfoconf;
26
 
29
 
27
 
30
 
28
our $VERSION = '2.2.1';
31
our $VERSION = '2.3.0';
29
use warnings;
32
use warnings;
30
use strict;  
33
use strict;  
31
 
34
 
32
#use Data::Dumper;
35
#use Data::Dumper;
33
use YAML::Tiny;
36
use YAML::Tiny;
34
use File::Basename;
37
use File::Basename;
35
 
38
 
36
use Exporter;
39
use Exporter;
37
 
40
 
38
our @ISA = qw( Exporter );
41
our @ISA = qw( Exporter );
39
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
42
our @EXPORT = qw( $clientName $serialNumber $hostname 
40
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
43
                  $transports $sysinfo3 $binDir $moduleDir
41
                  $scriptDir $confDir $binName $confName
44
                  $scriptDir $confDir $binName $confName $configurationFile
-
 
45
                  @confFileSearchPath  @moduleDirs @scriptDirs
-
 
46
                  %sendTypes
42
                  &showConf &transportsToConfig &getAnswer &yesno  
47
                  &showConf &transportsToConfig &getAnswer &yesno  
43
                  &writeConfig &processParameters $TESTING
48
                  &writeConfig &processParameters $TESTING
44
                  &setDryRun &enableModules &makeConfig &findConf &timeStamp
49
                  &setDryRun &enableModules &makeConfig &findConf &timeStamp
-
 
50
                  &loadConfigurationFile &logIt
45
                );
51
                );
46
 
52
 
47
our $TESTING = 0;
53
our $TESTING = 0;
48
our $dryRun;
54
our $dryRun;
49
our $clientName = '';
55
our $clientName = '';
50
our $serialNumber = '';
56
our $serialNumber = '';
51
our $hostname = '';
57
our $hostname = '';
52
 
58
 
-
 
59
# paths to search for configuration file
-
 
60
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
-
 
61
 
-
 
62
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
53
 
63
 
54
 
64
 
55
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
65
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
56
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
66
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
57
 
67
 
Line 329... Line 339...
329
      }
339
      }
330
   }
340
   }
331
   return ( '', $confName );
341
   return ( '', $confName );
332
}
342
}
333
 
343
 
-
 
344
#######################################################
-
 
345
#
-
 
346
# findFile( $filename, @directories )
-
 
347
#
-
 
348
# Locates a file by searching sequentially in one or more
-
 
349
# directories, returning the first one found
-
 
350
# 
-
 
351
# Returns '' if not found
-
 
352
#
-
 
353
#######################################################
-
 
354
 
-
 
355
sub findFile {
-
 
356
   my ( $filename, $directories ) = @_;
-
 
357
   &logIt( 3, "Looking for $filename in findFile" );
-
 
358
   for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
-
 
359
      my $confFile = $$directories[$i] . '/' . $filename;
-
 
360
      &logIt( 4, "Looking for $filename in $confFile" );
-
 
361
      return $confFile if ( -f $confFile );
-
 
362
   }
-
 
363
   return '';
-
 
364
}
-
 
365
   
-
 
366
 
-
 
367
#######################################################
-
 
368
#
-
 
369
# loadConfigurationFile($confFile)
-
 
370
#
-
 
371
# Loads configuration file defined by $configurationFile, and dies if not available
-
 
372
# This is a YAML file containing serialized contents of 
-
 
373
# Parameters:
-
 
374
#    $$fileName - name of file to look for (reference)
-
 
375
#    @searchPath - array of paths to find $filename
-
 
376
#
-
 
377
#######################################################
-
 
378
 
-
 
379
sub loadConfigurationFile {   
-
 
380
   my ( $fileName, @searchPath ) = @_;
-
 
381
   $$fileName = $configurationFile unless $$fileName;
-
 
382
   @searchPath = @confFileSearchPath unless @searchPath;
-
 
383
   &logIt( 2, "Looking for config file $$fileName in " . join( ', ', @searchPath ) );
-
 
384
   my $confFile;
-
 
385
   if ( $confFile = &findFile( $$fileName, \@searchPath ) ) {
-
 
386
      &logIt( 3, "Opening configuration from $confFile" );
-
 
387
      my $yaml = YAML::Tiny->read( $confFile );
-
 
388
      &logIt( 4, "Configuration file contents\n$yaml" );
-
 
389
      $$fileName = $confFile;
-
 
390
      return $yaml->[0];
-
 
391
   }
-
 
392
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
-
 
393
}
-
 
394
 
-
 
395
 
-
 
396
#######################################################
-
 
397
# function to simply log things
-
 
398
# first parameter is the priority, if <= $logDef->{'log level'} will print
-
 
399
# all subsequent parameters assumed to be strings to sent to the log
-
 
400
# returns 0 on failure
-
 
401
#         1 on success
-
 
402
#         2 if priority > log level
-
 
403
#        -1 if $logDef is unset
-
 
404
# currently, only logs to a file
-
 
405
#######################################################
-
 
406
sub logIt {
-
 
407
   my $priority = shift;
-
 
408
 
-
 
409
   # turn off variable checking so it doesn't blow up on lack of %configuration file
-
 
410
   no strict 'vars';
-
 
411
   
-
 
412
   return -1 unless exists $configuration{'logging'};
-
 
413
   return 2 unless $priority <= $configuration{'logging'}{'log level'};
-
 
414
   if ( $configuration{'logging'}{'log type'} eq 'cache' ) {
-
 
415
      push @{ $configuration{'logging'}{'cache'} }, @_;
-
 
416
      return;
-
 
417
   } elsif ( defined( $configuration{'logging'}{'cache'} ) ) {
-
 
418
      unshift @_, @{ $configuration{'logging'}{'cache'} };
-
 
419
      delete $configuration{'logging'}{'cache'};
-
 
420
   }
-
 
421
   if ( $configuration{'logging'}{'log type'} eq 'file' ) {
-
 
422
      if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
-
 
423
         while ( my $t = shift ) {
-
 
424
            print LOG &timeStamp() . "\t$t\n";
-
 
425
         }
-
 
426
         close LOG;
-
 
427
      }
-
 
428
   } elsif ( $configuration{'logging'}{'log type'} eq 'syslog' ) {
-
 
429
      use Sys::Syslog;                        # all except setlogsock()
-
 
430
      use Sys::Syslog qw(:standard :macros);  # standard functions & macros
-
 
431
 
-
 
432
      my $syslogName = 'sysinfo-client';
-
 
433
      my $logopt = 'nofatal';
-
 
434
      my $facility = 'LOG_LOCAL0';
-
 
435
      my $priority = 'LOG_NOTICE';
-
 
436
 
-
 
437
      openlog( $syslogName, $logopt, $facility);
-
 
438
      syslog($priority, '%s', @_ );
-
 
439
      closelog();
-
 
440
   } else {
-
 
441
      warn "Log type $configuration{'logging'} incorrectly configured\n";
-
 
442
      return 0;
-
 
443
   }
-
 
444
   return 1;
-
 
445
}
334
 
446
 
335
 
447
 
336
 
448
 
337
1;
449
1;