Subversion Repositories camp_sysinfo_client_3

Rev

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