Subversion Repositories camp_sysinfo_client_3

Rev

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