Subversion Repositories camp_sysinfo_client_3

Rev

Rev 109 | Rev 111 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 rodolico 1
#!/usr/bin/env perl
2
 
45 rodolico 3
# v1.2.0 20161022 RWR
4
# moved makeConfig here so it is usable by install and configure
77 rodolico 5
#
6
# v1.3.0 20190108 RWR
7
# added UUID and set defaults for config file
85 rodolico 8
#
9
# v1.4.0 20190204 RWR
10
# added autoconvert from v1.2 configuration file to v1.3
11
# using dumper to set configuration file
94 rodolico 12
#
13
# version 2.0 20190330 RWR
14
# changed it so all configs are YAML
45 rodolico 15
 
21 rodolico 16
package sysinfoconf;
17
 
42 rodolico 18
 
94 rodolico 19
our $VERSION = '2.0';
21 rodolico 20
use warnings;
26 rodolico 21
use strict;  
22
 
92 rodolico 23
#use Data::Dumper;
101 rodolico 24
use YAML::Tiny;
35 rodolico 25
use File::Basename;
26
 
21 rodolico 27
use Exporter;
28
 
29
our @ISA = qw( Exporter );
30
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
44 rodolico 31
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
32
                  $scriptDir $confDir $binName $confName
21 rodolico 33
                  &showConf &transportsToConfig &getAnswer &yesno  
35 rodolico 34
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
53 rodolico 35
                  &runCommand &setDryRun &enableModules &makeConfig &findConf
21 rodolico 36
                );
37
 
36 rodolico 38
our $TESTING = 0;
35 rodolico 39
our $dryRun;
28 rodolico 40
our $clientName = '';
41
our $serialNumber = '';
42
our $hostname = '';
53 rodolico 43
 
104 rodolico 44
 
45
 
53 rodolico 46
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
47
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
48
 
49
 
28 rodolico 50
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
51
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
110 rodolico 52
our $transports = { }; # holds transportation mechanisms
21 rodolico 53
# the following are keys which are specific to the different transport channels
54
 
103 rodolico 55
our $sysinfo3 = 'sysinfo-client.yaml';
21 rodolico 56
 
28 rodolico 57
our $binDir = '/opt/camp/sysinfo-client';
44 rodolico 58
our $moduleDir = $binDir . '/modules';
59
our $scriptDir = $binDir . '/scripts';
28 rodolico 60
our $confDir = '/etc/camp/sysinfo-client';
21 rodolico 61
 
28 rodolico 62
our $binName = 'sysinfo-client';
107 rodolico 63
our $confName = 'sysinfo-client.yaml';
21 rodolico 64
 
35 rodolico 65
sub setDryRun {
66
   $dryRun = shift;
67
}
21 rodolico 68
 
28 rodolico 69
our %sendTypes = ( 
21 rodolico 70
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
71
                                      'keys' => 
72
                                      [
73
                                        'mailTo',
74
                                        'mailSubject',
75
                                        'mailCC',
76
                                        'mailBCC',
77
                                        'mailServer',
78
                                        'mailFrom',
79
                                        'logFile',
80
                                        'otherCLParams',
81
                                        'tls',
82
                                        'smtpUser',
83
                                        'smtpPass',
84
                                        'sendEmailScriptLocation'
85
                                      ],
86
                                    },
87
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
88
                                      'keys' => 
89
                                      [
90
                                        'URL',
91
                                        'key for report',
92
                                        'key for date',
93
                                        'key for hostname',
94
                                        'key for client',
95
                                        'key for serial number'
96
                                      ]
86 rodolico 97
                                    },
98
                  'Save Local' =>   { 'sendScript' => 'save_local',
99
                                      'keys' =>
100
                                      [
101
                                         'output directory'
102
                                      ]
103
                                   }
21 rodolico 104
                );
105
 
106
 
37 rodolico 107
sub enableModules {
108
   my $moduleDir = shift;
109
   my %modules;
110
   if ( opendir( my $dh, "$moduleDir" ) ) {
111
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
112
                 grep { ! /^\./ } 
113
                 readdir $dh;
114
      closedir( $dh );
115
      foreach my $filename ( keys %modules ) {
116
         if ( open FILE,"<$moduleDir/$_" ) {
117
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
118
            close FILE;
119
            chomp @descript;
120
            $modules{$filename}{'description'} = $descript[0];
121
         } # if
122
      } # foreach
123
   } # if
124
} # enableModules
125
 
21 rodolico 126
sub showConf {
81 rodolico 127
 
35 rodolico 128
   my $configuration = shift;
92 rodolico 129
   $configuration = Dump($configuration);
84 rodolico 130
   return $configuration;
81 rodolico 131
 
83 rodolico 132
   #return $conf;
133
   #my $conf;
134
   #$conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
135
   #$conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
136
   #$conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
137
   #$conf .= "\$UUID = '" .      ( defined( $$configuration{'UUID'}     ) ? $$configuration{'UUID'}     : '' ) . "';\n";
138
   #$conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
139
   #$conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
140
   #$conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
141
   #return $conf;
21 rodolico 142
}
143
 
144
sub transportsToConfig {
35 rodolico 145
   my $transports = shift;
21 rodolico 146
   my $config;
147
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
148
      my $name = $$transports{ $priority }{'-name-'};
149
      $config .= "# Tranport $name at priority $priority\n";
150
      my $thisOne = $$transports{ $priority };
151
      foreach my $key ( sort keys %$thisOne ) {
152
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
153
      } # foreach
154
   } # foreach
155
   return $config;
156
} # transportsToConfig
157
 
94 rodolico 158
 
159
# read the configuration file passed in and return a reference to it
160
# to the calling routine
161
sub readConfig {
162
   my $filename = shift;
163
   my $config;
164
   return $config unless -e $filename;
165
   if ( open CONF,"<$filename" ) {
166
      my $contents = join( '', <CONF> );
167
      close CONF;
168
      # now, look to see what kind of file this is.
169
      if ( $contents =~ m/^---(\s*#.*)?$/m ) {
170
         print "Reading $filename as YAML\n";
171
         #print "Contents are:\n\n=====================$contents\n=====================\n";
172
         # this is a yaml file
173
         #$contents = YAML::Tiny->read( $config );
174
         $config = Load( $contents );
175
      } elsif ( $contents =~ m/\$clientName/ ) {
176
         # this is old style
177
         print "Reading $filename as old school file\n";
178
         my $clientName = '';
179
         my $serialNumber = '';
180
         my $hostname = '';
181
         my @moduleDirs;
182
         my @scriptDirs;
183
         my $UUID = '';
184
         my $transports;
185
         # now, eval the information we just read.
186
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
187
         no strict "vars";
188
         eval( $contents );
189
         use strict "vars";
190
         die "Error during eval: $@\n" if $@;
191
         $config->{'clientName'} = $clientName if $clientName;
192
         $config->{'serialNumber'} = $serialNumber if $serialNumber;
193
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
194
         $config->{'hostname'} = $hostname if $hostname;
195
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
196
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
197
         $config->{'transports'} = $transports if $transports;
198
      }
199
   } else {
200
         warn "Could not read config file $filename, skipped: $!\n";
201
   }
202
   return $config;
203
}
204
 
45 rodolico 205
sub makeConfig {
206
   my @configFileNames = @_;
207
   my %config;
208
 
209
   foreach my $config ( @configFileNames ) {
94 rodolico 210
      my $thisConfig = readConfig( $config );
211
      # add the new config to %config, overwriting any existing keys which are duplicated
212
      @config{keys %$thisConfig} = values %$thisConfig;
45 rodolico 213
   }
94 rodolico 214
   # now, ensure the correct values are loaded in some areas
81 rodolico 215
   unless ( $config{'hostname'} ) {
45 rodolico 216
      $hostname = `hostname -f`;
217
      chomp $hostname;
81 rodolico 218
      $config{'hostname'} = $hostname;
45 rodolico 219
   }
84 rodolico 220
   unless ( $config{'serialNumber'} ) {
53 rodolico 221
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
222
      chomp $serialNumber;
223
      $serialNumber =~ s/\s//gi;
84 rodolico 224
      $config{'serialNumber'} = $serialNumber;
53 rodolico 225
   }
81 rodolico 226
   unless ( $config{'UUID'} ) {
94 rodolico 227
      my $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
77 rodolico 228
      $UUID =~ s/\s//gi;
81 rodolico 229
      $config{'UUID'} = $UUID;
77 rodolico 230
   }
45 rodolico 231
 
110 rodolico 232
   # ensure we have the default SaveLocal transport defined
233
   unless ( defined $config{'transports'}{'99'} ) {
234
      $config{'transports'}{'99'} = {
235
                         'name'=> 'SaveLocal',
236
                         'output directory' => '/tmp',
237
                         'sendScript' => 'save_local'
238
                        };
239
   }
240
 
45 rodolico 241
   return \%config;
242
}
243
 
21 rodolico 244
# prompt the user for a response, then allow them to enter it
245
# the first response is considered the default and is printed
246
# in all caps if more than one exists
247
# first answer is used if they simply press the Enter
248
# key. The response is returned all lower case if more than one
249
# exists.
250
# it is assumed 
251
sub getAnswer {
252
   my ( $prompt, @answers ) = @_;
253
   $answers[0] = '' unless defined( $answers[0] );
254
   my $default = $answers[0];
255
   my $onlyOneAnswer = scalar( @answers ) == 1;
256
   print $prompt . '[ ';
257
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
258
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 259
   my $thisAnswer = <>;
21 rodolico 260
   chomp $thisAnswer;
261
   $thisAnswer = $default unless $thisAnswer;
262
   return $thisAnswer;
263
}
264
 
265
sub yesno {
40 rodolico 266
   my ( $prompt, $default ) = @_;
267
   $default = 'yes' unless $default;
268
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 269
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
270
}
271
 
35 rodolico 272
# runs a system command. Also, if in testing mode, simply shows what
273
# would have been done.
274
sub runCommand {
275
   while ( my $command = shift ) {
276
      if ( $dryRun ) {
277
         print "$command\n";
278
      } else {
279
         `$command`;
280
      }
281
   }
282
   return 1;
283
} # runCommand
284
 
285
# checks if a directory exists and, if not, creates it
286
my %directories; # holds list of directories already created so no need to do an I/O
287
 
288
sub checkDirectoryExists {
289
   my ( $filename,$mod,$owner ) = @_;
290
   $mod = "0700" unless $mod;
291
   $owner = "root:root" unless $owner;
292
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
293
   my ($fn, $dirname) = fileparse( $filename );
294
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
295
   return '' if exists $directories{$dirname};
296
   if ( -d $dirname ) {
297
      $directories{$dirname} = 1;
298
      return '';
299
   }
300
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
301
      $directories{$dirname} = 1;
302
   }
303
   return '';   
304
}
305
 
21 rodolico 306
sub writeConfig {
35 rodolico 307
   my ( $filename,$content ) = @_;
308
   my $path;
309
   ($filename, $path ) = fileparse( $filename );
310
 
21 rodolico 311
   my $return;
312
   `mkdir -p $path` unless -d $path;
313
   $filename = $path . '/' . $filename;
314
   if ( -e $filename ) {
315
      `cp $filename $filename.bak` if ( -e $filename );
316
      $return .= "Old config copied to $filename.bak\n";
317
   }
35 rodolico 318
   if ( $dryRun ) {
319
      $return .= "Not writing to configuration file\n";
320
   } else {
321
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
322
      print CONF $content;
323
      close CONF;
324
      `chmod 600 $filename`;
325
      $return .= "Configuration successfully written to $filename\n";
326
   }
21 rodolico 327
   return $return;
328
}
329
 
330
sub processParameters {
331
   while ( my $parameter = shift ) {
332
      if ( $parameter eq '-v' ) {
26 rodolico 333
         print "$VERSION\n";
21 rodolico 334
         exit;
335
      }
336
   } # while
337
}
338
 
53 rodolico 339
sub findConf {
340
   my $confName = shift;
341
   my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
342
   for ( my $i = 0; $i < @confDirs; $i++ ) {
343
      if ( -d $confDirs[ $i ] ) {
55 rodolico 344
         return ( $confDirs[$i],  $confName );
53 rodolico 345
      }
346
   }
104 rodolico 347
   return ( '', $confName );
53 rodolico 348
}
21 rodolico 349
 
104 rodolico 350
 
351
 
352
 
21 rodolico 353
1;