Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev 191 Rev 194
Line 214... Line 214...
214
# see https://perldoc.perl.org/Getopt/Long.html
214
# see https://perldoc.perl.org/Getopt/Long.html
215
use Getopt::Long;
215
use Getopt::Long;
216
# allow -vvn (ie, --verbose --verbose --dryrun)
216
# allow -vvn (ie, --verbose --verbose --dryrun)
217
Getopt::Long::Configure ("bundling");
217
Getopt::Long::Configure ("bundling");
218
 
218
 
-
 
219
use sysinfoconf;
-
 
220
 
219
# Following are global variables overridden if configuration file exists
221
# Following are global variables overridden if configuration file exists
220
 
222
 
221
my $TESTING = 0; # if set to 1, will do everything, but will dump output to /tmp/sysinfo.testing.yaml
223
my $TESTING = 0; # if set to 1, will do everything, but will dump output to /tmp/sysinfo.testing.yaml
222
 
224
 
223
my $indentLevel = 2; # number of spaces to indent per level in XML or YAML
225
my $indentLevel = 2; # number of spaces to indent per level in XML or YAML
224
 
226
 
225
# paths to search for configuration file
-
 
226
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
-
 
227
 
-
 
228
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
-
 
229
 
-
 
230
my $reportDate = &timeStamp(); # set report date
227
my $reportDate = &timeStamp(); # set report date
231
 
228
 
232
my $interactive = 0; # if set to 1, will go into interactive mode and output to local file
229
my $interactive = 0; # if set to 1, will go into interactive mode and output to local file
233
my $periodicOverrideFile = '/tmp/sysinfo.firstrun'; # if this file exists, library.pm will tell all periodic modules to run anyway
230
my $periodicOverrideFile = '/tmp/sysinfo.firstrun'; # if this file exists, library.pm will tell all periodic modules to run anyway
234
my $periodic = 0; # if set to 1, will do modules which are only supposed to run weekly, monthly, etc...
231
my $periodic = 0; # if set to 1, will do modules which are only supposed to run weekly, monthly, etc...
Line 249... Line 246...
249
 
246
 
250
 
247
 
251
 
248
 
252
#######################################################
249
#######################################################
253
#
250
#
254
# timeStamp
-
 
255
#
-
 
256
# return current system date as YYYY-MM-DD HH:MM:SS
-
 
257
#
-
 
258
#######################################################
-
 
259
sub timeStamp {
-
 
260
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
-
 
261
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
-
 
262
}
-
 
263
   
-
 
264
#######################################################
-
 
265
# function to simply log things
-
 
266
# first parameter is the priority, if <= $logDef->{'log level'} will print
-
 
267
# all subsequent parameters assumed to be strings to sent to the log
-
 
268
# returns 0 on failure
-
 
269
#         1 on success
-
 
270
#         2 if priority > log level
-
 
271
#        -1 if $logDef is unset
-
 
272
# currently, only logs to a file
-
 
273
#######################################################
-
 
274
sub logIt {
-
 
275
   my $priority = shift;
-
 
276
 
-
 
277
   return -1 unless exists $configuration{'logging'};
-
 
278
   return 2 unless $priority <= $configuration{'logging'}{'log level'};
-
 
279
   if ( $configuration{'logging'}{'log type'} eq 'cache' ) {
-
 
280
      push @{ $configuration{'logging'}{'cache'} }, @_;
-
 
281
      return;
-
 
282
   } elsif ( defined( $configuration{'logging'}{'cache'} ) ) {
-
 
283
      unshift @_, @{ $configuration{'logging'}{'cache'} };
-
 
284
      delete $configuration{'logging'}{'cache'};
-
 
285
   }
-
 
286
   if ( $configuration{'logging'}{'log type'} eq 'file' ) {
-
 
287
      if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
-
 
288
         while ( my $t = shift ) {
-
 
289
            print LOG &timeStamp() . "\t$t\n";
-
 
290
         }
-
 
291
         close LOG;
-
 
292
      }
-
 
293
   } elsif ( $configuration{'logging'}{'log type'} eq 'syslog' ) {
-
 
294
      use Sys::Syslog;                        # all except setlogsock()
-
 
295
      use Sys::Syslog qw(:standard :macros);  # standard functions & macros
-
 
296
 
-
 
297
      my $syslogName = 'sysinfo-client';
-
 
298
      my $logopt = 'nofatal';
-
 
299
      my $facility = 'LOG_LOCAL0';
-
 
300
      my $priority = 'LOG_NOTICE';
-
 
301
 
-
 
302
      openlog( $syslogName, $logopt, $facility);
-
 
303
      syslog($priority, '%s', @_ );
-
 
304
      closelog();
-
 
305
   } else {
-
 
306
      warn "Log type $configuration{'logging'} incorrectly configured\n";
-
 
307
      return 0;
-
 
308
   }
-
 
309
   return 1;
-
 
310
}
-
 
311
 
-
 
312
 
-
 
313
#######################################################
-
 
314
#
-
 
315
# findFile( $filename, @directories )
-
 
316
#
-
 
317
# Locates a file by searching sequentially in one or more
-
 
318
# directories, returning the first one found
-
 
319
# 
-
 
320
# Returns '' if not found
-
 
321
#
-
 
322
#######################################################
-
 
323
 
-
 
324
sub findFile {
-
 
325
   my ( $filename, $directories ) = @_;
-
 
326
   &logIt( 3, "Looking for $filename in findFile" );
-
 
327
   for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
-
 
328
      my $confFile = $$directories[$i] . '/' . $filename;
-
 
329
      &logIt( 4, "Looking for $filename in $confFile" );
-
 
330
      return $confFile if ( -f $confFile );
-
 
331
   }
-
 
332
   return '';
-
 
333
}
-
 
334
   
-
 
335
 
-
 
336
#######################################################
-
 
337
#
-
 
338
# loadConfigurationFile($confFile)
-
 
339
#
-
 
340
# Loads configuration file defined by $configurationFile, and dies if not available
-
 
341
# This is a YAML file containing serialized contents of 
-
 
342
# Parameters:
-
 
343
#    $fileName - name of file to look for
-
 
344
#    @searchPath - array of paths to find $filename
-
 
345
#
-
 
346
#######################################################
-
 
347
 
-
 
348
sub loadConfigurationFile {   
-
 
349
   my ( $fileName, @searchPath ) = @_;
-
 
350
   &logIt( 2, "Looking for config file $fileName in " . join( ', ', @searchPath ) );
-
 
351
   my $confFile;
-
 
352
   if ( $confFile = &findFile( $fileName, \@searchPath ) ) {
-
 
353
      &logIt( 3, "Opening configuration from $confFile" );
-
 
354
      my $yaml = YAML::Tiny->read( $confFile );
-
 
355
      &logIt( 4, "Configuration file contents\n$yaml" );
-
 
356
      return $yaml->[0];
-
 
357
   }
-
 
358
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
-
 
359
}
-
 
360
 
-
 
361
#######################################################
-
 
362
#
-
 
363
# sendResults( $parameters, $message, $scriptDirectory )
251
# sendResults( $parameters, $message, $scriptDirectory )
364
#
252
#
365
# Sends results of run to server using external script. If external
253
# Sends results of run to server using external script. If external
366
# script not defined, just print to STDOUT
254
# script not defined, just print to STDOUT
367
#
255
#
Line 714... Line 602...
714
 
602
 
715
if ( $interactive ) {
603
if ( $interactive ) {
716
   %configuration = %{ &interactiveConfig( \%configuration ) };
604
   %configuration = %{ &interactiveConfig( \%configuration ) };
717
} else {
605
} else {
718
   # load the configuration file
606
   # load the configuration file
719
   %configuration = %{ &loadConfigurationFile( $configurationFile, @confFileSearchPath) };
607
   %configuration = %{ &loadConfigurationFile( \$configurationFile, @confFileSearchPath) };
720
}
608
}
721
 
609
 
722
`touch $periodicOverrideFile` if $periodic; # tells periodic modules to run
610
`touch $periodicOverrideFile` if $periodic; # tells periodic modules to run
723
 
611
 
724
#die Dumper (\%configuration );
612
#die Dumper (\%configuration );
Line 784... Line 672...
784
}
672
}
785
 
673
 
786
unlink ( $periodicOverrideFile ) if -e $periodicOverrideFile;
674
unlink ( $periodicOverrideFile ) if -e $periodicOverrideFile;
787
&logIt( 0, 'Ending sysinfo Run' );
675
&logIt( 0, 'Ending sysinfo Run' );
788
 
676
 
789
`$configuration{postRunScript}` if $configuration{'postRunScript'};
677
`$configuration{postRunScript}{script name}` if $configuration{'postRunScript'}{'script name'};
790
 
678
 
791
1;
679
1;