Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev 136 Rev 138
Line 157... Line 157...
157
# $configuration{ 'logging' } = {
157
# $configuration{ 'logging' } = {
158
#    'log type'  => 'string',
158
#    'log type'  => 'string',
159
#    'log level' => #,
159
#    'log level' => #,
160
#    'other params' => something,
160
#    'other params' => something,
161
# };
161
# };
-
 
162
#
-
 
163
# The default log type is cache, which builds an array of all messages passed. When the log type is changed, the cache is
-
 
164
# checked for values and, if they exist, they are dumped to the log, then removed.
-
 
165
#
162
# Currently, the only log type is 'file', which has one other additional parameter, 'log path' which
166
# Currently, the only log type is 'file', which has one other additional parameter, 'log path' which
163
# points to the actual log to be created. The log is NOT limited in size, so use something else to
167
# points to the actual log to be created. The log is NOT limited in size, so use something else to
164
# do that.
168
# do that.
165
# log level is an integer which is compared the a priority passed to the logging function. The
169
# log level is an integer which is compared the a priority passed to the logging function. The
166
# higher log level is set, the more verbose the log.
170
# higher log level is set, the more verbose the log.
Line 179... Line 183...
179
   use File::Spec;
183
   use File::Spec;
180
   use lib File::Spec->catdir($FindBin::Bin);
184
   use lib File::Spec->catdir($FindBin::Bin);
181
}
185
}
182
 
186
 
183
use YAML::Tiny;
187
use YAML::Tiny;
-
 
188
use Data::Dumper;
184
 
189
 
185
# Following are global variables overridden if configuration file exists
190
# Following are global variables overridden if configuration file exists
186
 
191
 
187
my $TESTING = 0; # if set to 1, will do everything, but will dump output to /tmp/sysinfo.testing.yaml
192
my $TESTING = 0; # if set to 1, will do everything, but will dump output to /tmp/sysinfo.testing.yaml
188
 
193
 
Line 196... Line 201...
196
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
201
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
197
 
202
 
198
my $reportDate = &timeStamp(); # set report date
203
my $reportDate = &timeStamp(); # set report date
199
 
204
 
200
my %configuration = (
205
my %configuration = (
201
   'logging' => [],    # if set, will point to logging
206
   'logging' => ( 'log type' => 'cache' ),    # if set, will point to logging
202
   'moduleDirs' => [], # search paths for modules
207
   'moduleDirs' => [], # search paths for modules
203
   'scriptDirs' => [], # search paths for scripts
208
   'scriptDirs' => [], # search paths for scripts
204
   'clientName' => '',  # Required!! Must be set in conf file (no defaults)
209
   'clientName' => '',  # Required!! Must be set in conf file (no defaults)
205
   'serialNumber' => '', # serial number of machine
210
   'serialNumber' => '', # serial number of machine
206
   'UUID'         => '', # UUID of machine
211
   'UUID'         => '', # UUID of machine
Line 222... Line 227...
222
sub timeStamp {
227
sub timeStamp {
223
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
228
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
224
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;
229
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;
225
}
230
}
226
   
231
   
227
 
-
 
228
 
-
 
229
 
-
 
-
 
232
#######################################################
230
# function to simply things
233
# function to simply log things
231
# first parameter is the priority, if <= $logDef->{'log level'} will print
234
# first parameter is the priority, if <= $logDef->{'log level'} will print
232
# all subsequent parameters assumed to be strings to sent to the log
235
# all subsequent parameters assumed to be strings to sent to the log
233
# returns 0 on failure
236
# returns 0 on failure
234
#         1 on success
237
#         1 on success
235
#         2 if priority > log level
238
#         2 if priority > log level
236
#        -1 if $logDef is unset
239
#        -1 if $logDef is unset
237
# currently, only logs to a file
240
# currently, only logs to a file
-
 
241
#######################################################
238
sub logIt {
242
sub logIt {
239
   my $priority = shift;
243
   my $priority = shift;
240
 
244
 
241
   return -1 unless exists $configuration{'logging'};
245
   return -1 unless exists $configuration{'logging'};
242
   return 2 unless $priority <= $configuration{'logging'}{'log level'};
246
   return 2 unless $priority <= $configuration{'logging'}{'log level'};
-
 
247
   if ( $configuration{'logging'}{'log type'} eq 'cache' ) {
-
 
248
      push @{ $configuration{'logging'}{'cache'} }, @_;
-
 
249
      return;
-
 
250
   } elsif ( defined( $configuration{'logging'}{'cache'} ) ) {
-
 
251
      unshift @_, @{ $configuration{'logging'}{'cache'} };
-
 
252
      delete $configuration{'logging'}{'cache'};
-
 
253
   }
243
   if ( $configuration{'logging'}{'log type'} eq 'file' ) {
254
   if ( $configuration{'logging'}{'log type'} eq 'file' ) {
244
      if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
255
      if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
245
         while ( my $t = shift ) {
256
         while ( my $t = shift ) {
246
            print LOG &timeStamp() . "\t$t\n";
257
            print LOG &timeStamp() . "\t$t\n";
247
         }
258
         }
Line 266... Line 277...
266
#
277
#
267
#######################################################
278
#######################################################
268
 
279
 
269
sub findFile {
280
sub findFile {
270
   my ( $filename, $directories ) = @_;
281
   my ( $filename, $directories ) = @_;
-
 
282
   &logIt( 3, "Looking for $filename in findFile" );
271
   for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
283
   for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
272
      my $confFile = $$directories[$i] . '/' . $filename;
284
      my $confFile = $$directories[$i] . '/' . $filename;
-
 
285
      &logIt( 4, "Looking for $filename in $confFile" );
273
      return $confFile if ( -f $confFile );
286
      return $confFile if ( -f $confFile );
274
   }
287
   }
275
   return '';
288
   return '';
276
}
289
}
277
   
290
   
Line 287... Line 300...
287
#
300
#
288
#######################################################
301
#######################################################
289
 
302
 
290
sub loadConfigurationFile {   
303
sub loadConfigurationFile {   
291
   my ( $fileName, @searchPath ) = @_;
304
   my ( $fileName, @searchPath ) = @_;
-
 
305
   &logIt( 2, "Looking for config file $filename in " . join( ', ', @searchPath ) );
292
   my $confFile;
306
   my $confFile;
293
   if ( $confFile = &findFile( $fileName, \@searchPath ) ) {
307
   if ( $confFile = &findFile( $fileName, \@searchPath ) ) {
294
      &logIt( 2, "Opening configuration from $confFile" );
308
      &logIt( 3, "Opening configuration from $confFile" );
295
      my $yaml = YAML::Tiny->read( $confFile );
309
      my $yaml = YAML::Tiny->read( $confFile );
296
      &logIt( 4, "Opening configuration from $confFile" );
310
      &logIt( 4, "Configuration file contents\n$yaml" );
297
      return $yaml->[0];
311
      return $yaml->[0];
298
   }
312
   }
299
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
313
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
300
}
314
}
301
 
315