Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | 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
125 rodolico 15
#
16
# version 2.1 20191101 RWR
17
# changed --name-- tag to name
129 rodolico 18
#
19
# version 2.2 20191105 RWR
20
# fixed where dmidecode missing caused exception
135 rodolico 21
#
22
# version 2.2.1 20191112 RWR
23
# added timeStamp
45 rodolico 24
 
21 rodolico 25
package sysinfoconf;
26
 
42 rodolico 27
 
135 rodolico 28
our $VERSION = '2.2.1';
21 rodolico 29
use warnings;
26 rodolico 30
use strict;  
31
 
92 rodolico 32
#use Data::Dumper;
144 rodolico 33
use YAML::Tiny;
35 rodolico 34
use File::Basename;
35
 
21 rodolico 36
use Exporter;
37
 
38
our @ISA = qw( Exporter );
39
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
44 rodolico 40
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
41
                  $scriptDir $confDir $binName $confName
21 rodolico 42
                  &showConf &transportsToConfig &getAnswer &yesno  
144 rodolico 43
                  &writeConfig &processParameters $TESTING
44
                  &setDryRun &enableModules &makeConfig &findConf &timeStamp
21 rodolico 45
                );
46
 
36 rodolico 47
our $TESTING = 0;
35 rodolico 48
our $dryRun;
28 rodolico 49
our $clientName = '';
50
our $serialNumber = '';
51
our $hostname = '';
53 rodolico 52
 
104 rodolico 53
 
54
 
53 rodolico 55
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
56
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
57
 
58
 
28 rodolico 59
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
60
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
111 rodolico 61
our $transports = {}; # holds transportation mechanisms
21 rodolico 62
 
103 rodolico 63
our $sysinfo3 = 'sysinfo-client.yaml';
21 rodolico 64
 
28 rodolico 65
our $binDir = '/opt/camp/sysinfo-client';
44 rodolico 66
our $moduleDir = $binDir . '/modules';
67
our $scriptDir = $binDir . '/scripts';
28 rodolico 68
our $confDir = '/etc/camp/sysinfo-client';
21 rodolico 69
 
28 rodolico 70
our $binName = 'sysinfo-client';
107 rodolico 71
our $confName = 'sysinfo-client.yaml';
21 rodolico 72
 
35 rodolico 73
sub setDryRun {
74
   $dryRun = shift;
75
}
21 rodolico 76
 
28 rodolico 77
our %sendTypes = ( 
21 rodolico 78
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
79
                                      'keys' => 
80
                                      [
81
                                        'mailTo',
82
                                        'mailSubject',
83
                                        'mailCC',
84
                                        'mailBCC',
85
                                        'mailServer',
86
                                        'mailFrom',
87
                                        'logFile',
88
                                        'otherCLParams',
89
                                        'tls',
90
                                        'smtpUser',
91
                                        'smtpPass',
92
                                        'sendEmailScriptLocation'
93
                                      ],
94
                                    },
95
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
96
                                      'keys' => 
97
                                      [
98
                                        'URL',
99
                                        'key for report',
100
                                        'key for date',
101
                                        'key for hostname',
102
                                        'key for client',
103
                                        'key for serial number'
104
                                      ]
86 rodolico 105
                                    },
106
                  'Save Local' =>   { 'sendScript' => 'save_local',
107
                                      'keys' =>
108
                                      [
109
                                         'output directory'
110
                                      ]
111
                                   }
21 rodolico 112
                );
113
 
114
 
135 rodolico 115
#######################################################
116
#
117
# timeStamp
118
#
119
# return current system date as YYYY-MM-DD HH:MM:SS
120
#
121
#######################################################
122
sub timeStamp {
123
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
143 rodolico 124
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
135 rodolico 125
}
126
 
127
 
37 rodolico 128
sub enableModules {
129
   my $moduleDir = shift;
130
   my %modules;
131
   if ( opendir( my $dh, "$moduleDir" ) ) {
132
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
133
                 grep { ! /^\./ } 
134
                 readdir $dh;
135
      closedir( $dh );
136
      foreach my $filename ( keys %modules ) {
137
         if ( open FILE,"<$moduleDir/$_" ) {
138
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
139
            close FILE;
140
            chomp @descript;
141
            $modules{$filename}{'description'} = $descript[0];
142
         } # if
143
      } # foreach
144
   } # if
145
} # enableModules
146
 
21 rodolico 147
sub showConf {
81 rodolico 148
 
35 rodolico 149
   my $configuration = shift;
92 rodolico 150
   $configuration = Dump($configuration);
84 rodolico 151
   return $configuration;
21 rodolico 152
}
153
 
154
sub transportsToConfig {
35 rodolico 155
   my $transports = shift;
21 rodolico 156
   my $config;
157
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
124 rodolico 158
      my $name = $$transports{ $priority }{'name'};
21 rodolico 159
      $config .= "# Tranport $name at priority $priority\n";
160
      my $thisOne = $$transports{ $priority };
161
      foreach my $key ( sort keys %$thisOne ) {
162
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
163
      } # foreach
164
   } # foreach
165
   return $config;
166
} # transportsToConfig
167
 
94 rodolico 168
 
169
# read the configuration file passed in and return a reference to it
170
# to the calling routine
171
sub readConfig {
172
   my $filename = shift;
173
   my $config;
174
   return $config unless -e $filename;
175
   if ( open CONF,"<$filename" ) {
176
      my $contents = join( '', <CONF> );
177
      close CONF;
178
      # now, look to see what kind of file this is.
179
      if ( $contents =~ m/^---(\s*#.*)?$/m ) {
180
         print "Reading $filename as YAML\n";
181
         #print "Contents are:\n\n=====================$contents\n=====================\n";
182
         # this is a yaml file
183
         #$contents = YAML::Tiny->read( $config );
161 rodolico 184
         # try to load the contents into $config, and warn if there is a problem.
185
         eval( $config = Load( $contents ) ); warn $@ if $@;
94 rodolico 186
      } elsif ( $contents =~ m/\$clientName/ ) {
187
         # this is old style
188
         print "Reading $filename as old school file\n";
189
         my $clientName = '';
190
         my $serialNumber = '';
191
         my $hostname = '';
192
         my @moduleDirs;
193
         my @scriptDirs;
194
         my $UUID = '';
195
         my $transports;
196
         # now, eval the information we just read.
197
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
198
         no strict "vars";
199
         eval( $contents );
200
         use strict "vars";
201
         die "Error during eval: $@\n" if $@;
202
         $config->{'clientName'} = $clientName if $clientName;
203
         $config->{'serialNumber'} = $serialNumber if $serialNumber;
204
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
205
         $config->{'hostname'} = $hostname if $hostname;
206
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
207
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
208
         $config->{'transports'} = $transports if $transports;
129 rodolico 209
         foreach my $trans ( keys %{$config->{'transports'}} ) {
210
            if ( exists ( $config->{'transports'}->{$trans}->{'-name-'} ) ) {
211
               $config->{'transports'}->{$trans}->{'name'} = $config->{'transports'}->{$trans}->{'-name-'};
212
               delete( $config->{'transports'}->{$trans}->{'-name-'} );
213
            }
214
         }
94 rodolico 215
      }
216
   } else {
217
         warn "Could not read config file $filename, skipped: $!\n";
218
   }
219
   return $config;
220
}
221
 
45 rodolico 222
sub makeConfig {
223
   my @configFileNames = @_;
224
   my %config;
225
 
226
   foreach my $config ( @configFileNames ) {
129 rodolico 227
      my $thisConfig = &readConfig( $config ) if $config && -e $config;
94 rodolico 228
      # add the new config to %config, overwriting any existing keys which are duplicated
229
      @config{keys %$thisConfig} = values %$thisConfig;
45 rodolico 230
   }
94 rodolico 231
   # now, ensure the correct values are loaded in some areas
81 rodolico 232
   unless ( $config{'hostname'} ) {
45 rodolico 233
      $hostname = `hostname -f`;
234
      chomp $hostname;
81 rodolico 235
      $config{'hostname'} = $hostname;
45 rodolico 236
   }
84 rodolico 237
   unless ( $config{'serialNumber'} ) {
53 rodolico 238
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
239
      chomp $serialNumber;
240
      $serialNumber =~ s/\s//gi;
84 rodolico 241
      $config{'serialNumber'} = $serialNumber;
53 rodolico 242
   }
81 rodolico 243
   unless ( $config{'UUID'} ) {
129 rodolico 244
      my $UUID = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
77 rodolico 245
      $UUID =~ s/\s//gi;
81 rodolico 246
      $config{'UUID'} = $UUID;
77 rodolico 247
   }
45 rodolico 248
 
110 rodolico 249
   # ensure we have the default SaveLocal transport defined
250
   unless ( defined $config{'transports'}{'99'} ) {
251
      $config{'transports'}{'99'} = {
252
                         'name'=> 'SaveLocal',
253
                         'output directory' => '/tmp',
254
                         'sendScript' => 'save_local'
255
                        };
256
   }
257
 
45 rodolico 258
   return \%config;
259
}
260
 
21 rodolico 261
# prompt the user for a response, then allow them to enter it
262
# the first response is considered the default and is printed
263
# in all caps if more than one exists
264
# first answer is used if they simply press the Enter
265
# key. The response is returned all lower case if more than one
266
# exists.
267
# it is assumed 
268
sub getAnswer {
269
   my ( $prompt, @answers ) = @_;
270
   $answers[0] = '' unless defined( $answers[0] );
271
   my $default = $answers[0];
272
   my $onlyOneAnswer = scalar( @answers ) == 1;
273
   print $prompt . '[ ';
274
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
275
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 276
   my $thisAnswer = <>;
21 rodolico 277
   chomp $thisAnswer;
278
   $thisAnswer = $default unless $thisAnswer;
279
   return $thisAnswer;
280
}
281
 
282
sub yesno {
40 rodolico 283
   my ( $prompt, $default ) = @_;
284
   $default = 'yes' unless $default;
285
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 286
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
287
}
288
 
289
sub writeConfig {
132 rodolico 290
   use File::Temp qw / tempfile /;
35 rodolico 291
   my ( $filename,$content ) = @_;
129 rodolico 292
   if ( $filename ) { # they sent us a filename
293
      my $path;
294
      ($filename, $path ) = fileparse( $filename );
295
      `mkdir -p $path` unless -d $path;
296
      $filename = $path . '/' . $filename;
21 rodolico 297
      `cp $filename $filename.bak` if ( -e $filename );
129 rodolico 298
      unless ( $dryRun ) {
299
         open CONF,">$filename" or die "Could not write to $filename: $!\n";
300
         print CONF $content;
301
         close CONF;
302
         `chmod 600 $filename`;
303
      }
304
   } else { # no path provided, so just create a temp file
305
      # we will create a temporary file and return the name
306
      # it is the calling programs responsiblity to remove the file
307
      my $fh;
308
      ($fh,$filename) = tempfile( UNLINK => 0 );
309
      print $fh $content;
310
      close $fh
21 rodolico 311
   }
129 rodolico 312
   return $filename;
21 rodolico 313
}
314
 
315
sub processParameters {
316
   while ( my $parameter = shift ) {
317
      if ( $parameter eq '-v' ) {
26 rodolico 318
         print "$VERSION\n";
21 rodolico 319
         exit;
320
      }
321
   } # while
322
}
323
 
53 rodolico 324
sub findConf {
325
   my $confName = shift;
326
   for ( my $i = 0; $i < @confDirs; $i++ ) {
327
      if ( -d $confDirs[ $i ] ) {
55 rodolico 328
         return ( $confDirs[$i],  $confName );
53 rodolico 329
      }
330
   }
104 rodolico 331
   return ( '', $confName );
53 rodolico 332
}
21 rodolico 333
 
104 rodolico 334
 
335
 
336
 
21 rodolico 337
1;