Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev 223 Rev 227
Line 33... Line 33...
33
use strict;  
33
use strict;  
34
 
34
 
35
#use Data::Dumper;
35
#use Data::Dumper;
36
use YAML::Tiny;
36
use YAML::Tiny;
37
use File::Basename;
37
use File::Basename;
-
 
38
use Data::Dumper;
38
 
39
 
39
use Exporter;
40
use Exporter;
40
 
41
 
41
our @ISA = qw( Exporter );
42
our @ISA = qw( Exporter );
42
our @EXPORT = qw( $clientName $serialNumber $hostname 
43
our @EXPORT = qw( $clientName $serialNumber $hostname 
Line 54... Line 55...
54
our $dryRun;
55
our $dryRun;
55
our $clientName = '';
56
our $clientName = '';
56
our $serialNumber = '';
57
our $serialNumber = '';
57
our $hostname = '';
58
our $hostname = '';
58
 
59
 
-
 
60
my %serialNumberTranslation = ( 
-
 
61
   'notspecified' => '', # not set in DMI
-
 
62
   '0123456789' => '' # used by some SuperMicro computers
-
 
63
);
-
 
64
 
-
 
65
my %UUIDTranslation = ( 
-
 
66
   '03000200-0400-0500-0006-000700080009' => '', # some routers
-
 
67
   'sot settable' => '' # not set in DMI
-
 
68
);
-
 
69
 
59
# paths to search for configuration file
70
# paths to search for configuration file
60
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
71
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
-
 
72
my $serverInfoFileName = '/etc/server.info';
61
 
73
 
62
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
74
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
63
 
75
 
64
 
76
 
65
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
77
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
Line 173... Line 185...
173
      } # foreach
185
      } # foreach
174
   } # foreach
186
   } # foreach
175
   return $config;
187
   return $config;
176
} # transportsToConfig
188
} # transportsToConfig
177
 
189
 
-
 
190
sub readServerInfo {
-
 
191
   my $serverInfoName = shift;
-
 
192
   my %return;
178
 
193
 
179
# read the configuration file passed in and return a reference to it
194
   print "Reading server info from $serverInfoName\n";
180
# to the calling routine
-
 
181
sub readConfig {
195
   
182
   my $filename = shift;
196
   if ( -f $serverInfoName ) {
183
   my $config;
-
 
184
   return $config unless -e $filename;
197
      print "found $serverInfoName\n";
185
   my $yaml = YAML::Tiny->read( $filename );
198
      open DATA, $serverInfoName or die "Could not read $serverInfoName: $!\n";
186
   return $yaml->[0];
199
      print "opened $serverInfoName\n";
187
}
-
 
188
 
-
 
189
# merges $hash2 into $hash, with $hash1 taking precedence
200
      while ( my $line = <DATA> ) {
190
# if a key is a hashref on both sides, will recursively merg
201
         next if $line =~ m/^#/;
191
# otherwise, if $hash1->{$key} already has a value, no action
-
 
192
# will be taken
-
 
193
# using this instead of Hash::Merge as we don't need the extra power
-
 
194
sub mergeHash {
-
 
195
   my ($hash1, $hash2) = shift;
202
         chomp $line;
196
   foreach my $key ( keys %$hash2 ) {
203
         my ($key,$value) = split( ':', $line );
197
      if ( ! ( defined( $hash1->{$key} ) && length($hash1->{$key} ) ) ) { # defined and not empty
-
 
198
         # just put it into hash 1
204
         $key =~ s/^\s+|\s+$//g;
199
         $hash1->{$key} = $hash2->{$key};
205
         $value =~ s/^\s+|\s+$//g;
200
      } elsif ( ref( $hash2->{$key} ) eq 'HASH' && ref( $hash1->{$key} ) eq 'HASH' ) {
-
 
201
         # they are both hashes, so recursively merge
206
         # note, we make the key lower case so we can find it
202
         $hash1 = &mergeHash( $hash1->{$key}, $hash2->{$key} );
207
         $return{ lc $key} = $value;
203
      }
208
      }
204
      # ignore everything else
-
 
205
   }
209
   }
206
   return $hash1;
210
   return \%return;
207
}
211
}
208
      
212
 
209
 
213
 
210
sub makeConfig {
214
sub makeConfig {
211
   my @configFileNames = @_;
215
   my $configFile = shift;
212
   my $config = {}; # make sure it is a ref to a hash so mergeHash can recognize it
216
   my $config = {}; # make sure it is a ref to a hash so mergeHash can recognize it
213
 
-
 
214
   foreach my $configFile ( @configFileNames ) {
217
   my $serverInfo = readServerInfo( $serverInfoFileName );
-
 
218
   # die Dumper( $serverInfo );
215
      next unless $configFile && -e $configFile;
219
   if ( -f $configFile ) {
216
      print "Processing config file $configFile\n";
220
      print "Processing config file $configFile\n";
217
      my $thisConfig = &readConfig( $configFile );
221
      my $yaml = YAML::Tiny->read( $configFile );
218
      # add the new config to %config, overwriting any existing keys which are duplicated
-
 
219
      $config = &mergeHash( $config, $thisConfig );
222
      $config = $yaml->[0];
220
      #@config{keys %$thisConfig} = values %$thisConfig;
-
 
221
   }
223
   }
-
 
224
 
-
 
225
   # Fill in any values we are missing but which are found in $serverInfo (/etc/server.info)
-
 
226
   $config->{'clientName'} = defined( $serverInfo->{'owner'} ) ? $serverInfo->{'owner'} : '' unless $config->{'clientName'};
-
 
227
   $config->{'hostname'} = defined( $serverInfo->{'hostname'} ) ? $serverInfo->{'hostname'} : '' unless $config->{'hostname'};
-
 
228
   $config->{'serialNumber'} = defined( $serverInfo->{'serial'} ) ? $serverInfo->{'serial'} : '' unless $config->{'serialNumber'};
-
 
229
   $config->{'UUID'} = defined( $serverInfo->{'uuid'} ) ? $serverInfo->{'uuid'} : '' unless $config->{'UUID'};
-
 
230
   $config->{'location'} = defined( $serverInfo->{'location'} ) ? $serverInfo->{'location'} : '' unless $config->{'location'};
-
 
231
   $config->{'os_type'} = defined( $serverInfo->{'os_type'} ) ? $serverInfo->{'os_type'} : '' unless $config->{'os_type'};
-
 
232
   $config->{'tags'} = defined( $serverInfo->{'tags'} ) ? [ split ',', $serverInfo->{'tags'} ] : [] unless $config->{'tags'};
-
 
233
   
222
   # now, ensure the correct values are loaded in some areas
234
   # if these are still missing, we can try to run a program to get the values
223
   unless ( $config->{'hostname'} ) {
235
   unless ( $config->{'hostname'} ) {
224
      $hostname = `hostname -f`;
236
      $config->{'hostname'} = `hostname -f`;
225
      chomp $hostname;
237
      chomp $config->{'hostname'};
226
      $config->{'hostname'} = $hostname;
-
 
227
   }
238
   }
228
   unless ( $config->{'serialNumber'} ) {
239
   unless ( $config->{'serialNumber'} ) {
229
      $serialNumber = `dmidecode -s system-serial-number` if `which dmidecode`;
240
      $config->{'serialNumber'} = `dmidecode -s system-serial-number` if `which dmidecode`;
230
      chomp $serialNumber;
241
      chomp $config->{'serialNumber'};
231
      $serialNumber =~ s/\s//gi;
242
      $config->{'serialNumber'} =~ s/\s//gi;
232
      $config->{'serialNumber'} = $serialNumber;
243
      $config->{'serialNumber'} = $serialNumberTranslation{lc($config->{'serialNumber'})} if defined( $serialNumberTranslation{lc($config->{'serialNumber'})} );
233
   }
244
   }
234
   unless ( $config->{'UUID'} ) {
245
   unless ( $config->{'UUID'} ) {
235
      my $UUID = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
246
      $config->{'UUID'} = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '' if `which dmidecode`;
236
      $UUID =~ s/\s//gi;
247
      $config->{'UUID'} =~ s/\s//gi;
-
 
248
      $config->{'UUID'} = $UUIDTranslation{lc($config->{'UUID'})} if defined( $UUIDTranslation{lc($config->{'UUID'})} );
-
 
249
   }
237
      $config->{'UUID'} = $UUID;
250
   unless ( $config->{'os_type'} ) {
-
 
251
      $config->{'os_type'} = `installer/determineOS` if -x 'installer/determineOS';
238
   }
252
   }
239
   
253
   
240
   return $config;
254
   return $config;
241
}
255
}
242
 
256