Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 rodolico 1
#!/usr/bin/env perl
2
 
16 rodolico 3
use warnings;
4
 
11 rodolico 5
my $seedFile = 'sysinfo-client.seed';
6
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
7
my $sysinfo3 = '/etc/camp/sysinfo-client.conf';
12 rodolico 8
my $configPath = '/etc/camp';
9
my $configFile = 'sysinfo-client.conf';
11 rodolico 10
 
11
my $clientName = '';
12
my $serialNumber = '';
13
my $hostname;
14
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
15
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
16 rodolico 16
my $transports = {}; # holds transportation mechanisms
11 rodolico 17
# the following are keys which are specific to the different transport channels
18
 
16 rodolico 19
my %sendTypes = ( 
20
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
21
                                      'keys' => 
22
                                      [
23
                                        'mailTo',
24
                                        'mailSubject',
25
                                        'mailCC',
26
                                        'mailBCC',
27
                                        'mailServer',
28
                                        'mailFrom',
29
                                        'logFile',
30
                                        'otherCLParams',
31
                                        'tls',
32
                                        'smtpUser',
33
                                        'smtpPass',
34
                                        'sendEmailScriptLocation'
35
                                      ],
36
                                    },
37
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
38
                                      'keys' => 
39
                                      [
40
                                        'URL',
41
                                        'key for report',
42
                                        'key for date',
43
                                        'key for hostname',
44
                                        'key for client',
45
                                        'key for serial number'
46
                                      ]
47
                                    }
48
                );
49
 
50
 
51
 
11 rodolico 52
sub showConf {
53
   my $conf;
54
   $conf .= "\$clientName = '" . $clientName . "';\n";
16 rodolico 55
   $conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
11 rodolico 56
   $conf .= "\$hostname = '" . $hostname . "';\n";
57
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
58
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
16 rodolico 59
   $conf .= &transportsToConfig();
11 rodolico 60
   return $conf;
61
}
62
 
16 rodolico 63
sub transportsToConfig {
64
   my $config;
65
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
66
      my $name = $$transports{ $priority }{'-name-'};
67
      $config .= "# Tranport $name at priority $priority\n";
68
      my $thisOne = $$transports{ $priority };
69
      foreach my $key ( sort keys %$thisOne ) {
70
         # if they are wanting variable substitution, surround with double quote
71
         # otherwise, use single quote
72
         if ( $$thisOne{$key} =~ m/(\$hostname)|(\$reportDate)|(\$clientName)|(\$serialNumber)/ ) {
73
            $config .= "\$\$transports{$priority}{'$key'} = \"$$thisOne{$key}\";\n";
74
         } else {
75
            $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
76
         }
77
      } # foreach
78
   } # foreach
79
   return $config;
80
} # transportsToConfig
81
 
82
 
83
 
11 rodolico 84
# prompt the user for a response, then allow them to enter it
85
# the first response is considered the default and is printed
86
# in all caps if more than one exists
87
# first answer is used if they simply press the Enter
88
# key. The response is returned all lower case if more than one
89
# exists.
90
# it is assumed 
91
sub getAnswer {
92
   my ( $prompt, @answers ) = @_;
16 rodolico 93
   $answers[0] = '' unless defined( $answers[0] );
11 rodolico 94
   my $default = $answers[0];
95
   my $onlyOneAnswer = scalar( @answers ) == 1;
96
   print $prompt . '[ ';
97
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
98
   print join( ' | ', @answers ) . ' ]: ';
99
   $thisAnswer = <>;
100
   chomp $thisAnswer;
101
   $thisAnswer = $default unless $thisAnswer;
102
   return $thisAnswer;
103
}
104
 
105
sub yesno {
106
   my $prompt = shift;
107
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
12 rodolico 108
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
11 rodolico 109
}
110
 
12 rodolico 111
sub findSendEmail {
112
   my $currentLocation = shift;
113
   my @possibles = ( '/opt/sendEmail/sendEmail', '/opt/sendEmail/sendEmail.pl' );
114
   return $currentLocation if ( $currentLocation && -x $currentLocation );
115
   # well, we didn't find it, so look around some more
116
   # we install it in /opt, so try there
117
   foreach my $current ( @possibles ) {
118
      return $current if -x $current;
119
   }
120
   if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
121
      $path = `perl getSendEmail.pl`;
122
      chomp $path;
123
      return $path;
124
   }
125
   return '';
126
}
127
 
11 rodolico 128
sub userConfirmation {
129
   my $temp;
130
 
131
   $clientName = &getAnswer( "Client Name ", ($clientName) );
132
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
133
   $hostname = &getAnswer( "Host Name ", ($hostname) );
134
 
135
   $temp = join( ',',@moduleDirs );
136
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
137
   @moduleDirs = split( ',', $temp );
138
 
139
   $temp = join( ',',@scriptDirs );
140
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
141
   @scriptDirs = split( ',', $temp );
142
 
143
}
144
 
145
# simply attempts to detect the operating system so we can do OS
146
# specific actions at the end.   
147
sub getOperatingSystem {
148
   my @OSTypes = ( 'ipfire','debian' );
149
   my $OS = `uname -a`;
150
   foreach $osType ( @OSTypes ) {
151
      return $osType if $OS =~ m/$osType/i;
152
   }
153
   return '';
154
} # getOperatingSystem
155
 
156
# special purpose for IPFire routers
157
sub ipFire {
158
   my @BACKUP_DIRS = ( '/etc/camp' );
159
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/fcron.daily/sysinfo.cron` if &yesno( 'Add link to fcron.daily' );
160
   # now, check for backup directories not in include.user
161
   open BACKUP, '</var/ipfire/backup/include.user';
162
   while ( $line = <BACKUP> ) {
163
      chomp $line;
164
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
165
         if ( $BACKUP_DIRS[$i] eq $line ) {
166
            $BACKUP_DIRS[$i] = '';
167
            last;
168
         } # if
169
      }# for
170
   } # while
171
   close BACKUP;
172
 
173
   # if any remain, append them to include.user
174
   open BACKUP, '>>/var/ipfire/backup/include.user';
175
   foreach my $backupDir ( @BACKUP_DIRS ) {
176
      print BACKUP "$backupDir\n" if $backupDir;
177
   }
178
   close BACKUP;
179
 
180
   # set all modules with ipfire or unix in the name to run
16 rodolico 181
   my $moduleDir = $moduleDirs[0];
182
   opendir $moduleDir, $moduleDir;
11 rodolico 183
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
184
   closedir $moduleDir;
16 rodolico 185
   foreach my $module ( @modules) {
186
      `chmod 0700 $moduleDir/$module`;
11 rodolico 187
   }
188
   print "All IPFire specific modules have been enabled\n";
189
}
190
 
191
# some debian specific things
192
sub debian {
16 rodolico 193
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo` 
11 rodolico 194
      if &yesno( 'Add link to cron.daily' );
195
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
196
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
197
      if ( &yesno( 'Back up old configuration? ') ) {
12 rodolico 198
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
11 rodolico 199
         print "Old configuration copied to /root/sysinfo.2.config.tgz\n";
200
      }
201
      `apt-get -y --purge remove sysinfo-client`;
202
      print "There may be some unused packages now. You can remove them with\napt-get autoremove\n\n";
203
   }
204
   `ln -s  /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client` 
205
      if &yesno( 'Would you like a link to /usr/local/bin/sysinfo-client' );
206
   # set all modules with debian or unix in the name to run
14 rodolico 207
   foreach my $directory ( @moduleDirs ) {
208
      opendir ( $dh, $directory ) or die "Could not open directory [$directory]\n";
209
      my @modules = grep { /^((dpkg)|(unix))/ } readdir $dh;
210
      closedir $dh;
211
      foreach my $module (@modules) {
212
         `chmod 0700 $directory/$module`;
213
         `chown root:root $directory/$module`;
214
      }
11 rodolico 215
   }
216
   print "All debian specific modules have been enabled\n";
217
}
218
 
16 rodolico 219
sub setUpTransport {
220
   my ($priority, $transport, $fields, $type ) = @_;
221
   $priority = getAnswer( 'Priority', $priority );
222
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
223
   $$transport{'-name-'} = $type unless $$transport{'-name-'};
224
   my $allKeys = $$fields{'keys'};
225
   foreach my $key ( @$allKeys ) {
226
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
227
         $temp = &findSendEmail( $$transport{$key} );
228
         $$transport{$key} = $temp if $temp;
229
      }
230
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
231
   }
232
   return ( $priority, $transport );
233
}
234
 
235
sub showAllTransports {
236
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
237
      print '='x40 . "\n";
238
      &showTransports( $priority, $$transports{ $priority } );
239
      print '='x40 . "\n";
240
   }
241
}   
242
 
243
sub showTransports {
244
   my ( $priority,$thisOne )  = @_;
245
   print $$thisOne{'-name-'} . " has priority $priority\n";
246
   foreach my $key ( sort keys %$thisOne ) {
247
      next if $key =~ m/^-.*-$/;
248
      print "$key = $$thisOne{$key}\n";
249
   }
250
}
251
 
252
sub doTransports {
253
   my ( $transports ) = @_;
254
 
255
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
256
      if ( &yesno( $$transports{$priority}{'-name-'} . " has a priority of $priority, edit it?") ) {
257
         #print Dumper( $sendTypes{$$transports{$priority}{'-name-'}} );
258
         #die;
259
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'-name-'}} );
260
         if ( $newpriority != $priority ) {
261
            delete $$transports{$priority};
262
            $priority = $newpriority;
263
         }
264
         $$transports{$priority} = $temp;
265
      } # if
266
   }
267
 
268
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
269
      print '='x40 . "\n";
270
      &showTransports( $priority, $$transports{ $priority } );
271
      print '='x40 . "\n";
272
   }
273
 
274
   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
275
      $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
276
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
277
      $$transports{$priority} = $temp;
278
   }
279
 
280
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
281
      print '='x40 . "\n";
282
      &showTransports( $priority, $$transports{ $priority } );
283
      print '='x40 . "\n";
284
   }
285
}
286
 
287
 
12 rodolico 288
sub writeConfig {
289
   my ($path,$filename,$content) = @_;
290
   my $return;
291
   `mkdir -p $path` unless -d $path;
292
   $filename = $path . '/' . $filename;
293
   if ( -e $filename ) {
294
      `cp $filename $filename.bak` if ( -e $filename );
295
      $return .= "Old config copied to $filename.bak\n";
296
   }
297
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
298
   print CONF $content;
299
   close CONF;
300
   $return .= "Configuration successfully written to $filename\n";
301
   return $return;
302
}
303
 
16 rodolico 304
sub convertSysinfo2 {
11 rodolico 305
   my $client_name;
16 rodolico 306
   my $iMailResults;
307
   my $mailTo;
308
   my $mailSubject;
309
   my $mailCC;
310
   my $mailBCC;
311
   my $mailServer;
312
   my $mailServerPort;
313
   my $mailFrom;
314
   my $SENDMAIL;
315
   my $clientName;
316
   my $serialNumber;
317
   my $hostname;
318
   my $transports = {}; # holds transportation mechanisms
319
   open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
11 rodolico 320
   my $temp = join( '', <SEED> );
321
   close SEED;
322
   eval( $temp );
16 rodolico 323
   if ( $iMailResults ) {
324
      $temp = {};
325
      $$temp{'-name-'} = 'SendEmail';
326
      $$temp{'mailTo'} = $mailTo if $mailTo;
327
      $$temp{'$mailFrom'} = $mailFrom if $mailFrom;
328
      $$temp{'mailCC'} = $mailCC if $mailCC;
329
      $$temp{'mailServer'} = $mailServer if $mailServer;
330
      $$temp{'mailServer'} .= ":$mailServerPort" if $mailServerPort;
331
      $$transports{'1'} = $temp;
332
   }   
333
   $clientName = $client_name if ( $client_name );
334
   return ( $clientName,$hostname,$serialNumber,$transports );
335
}
336
 
337
sub loadConfig {
338
   # See if existing configuration. If so, offer to load it
339
   if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
340
      print "Loading defaults from existing config $sysinfo3\n";
11 rodolico 341
      my $client_name;
16 rodolico 342
      open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
11 rodolico 343
      my $temp = join( '', <SEED> );
344
      close SEED;
345
      eval( $temp );
16 rodolico 346
   } else { # nope, so see if an old sysinfo2 or a seed file exists
347
      if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
348
         print "Loading defaults from sysinfo2 config $sysinfo2\n";
349
         # NOTE: the return values are all placed into globals!!!!
350
         ( $clientName,$hostname,$serialNumber,$transports ) = &convertSysinfo2();
351
      }
352
      # seed files are expected to be in the correct format already
353
      if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
354
         print "Loading seed file $seedFile\n";
355
         open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
356
         my $temp = join( '', <SEED> );
357
         close SEED;
358
         eval( $temp );
359
      }
11 rodolico 360
   }
361
}
362
 
16 rodolico 363
&loadConfig();
364
 
11 rodolico 365
unless ( $hostname ) {
366
   $hostname = `hostname -f`;
367
   chomp $hostname;
368
}
369
 
370
&userConfirmation();
16 rodolico 371
&doTransports( $transports );
11 rodolico 372
 
373
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
374
print &showConf(  );
375
print '-'x40 . "\n";
16 rodolico 376
#print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );
11 rodolico 377
 
12 rodolico 378
 
11 rodolico 379
my $os = &getOperatingSystem();
380
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
12 rodolico 381
   &ipFire() if $os eq 'ipfire';
382
   &debian() if $os eq 'debian';
11 rodolico 383
}
384
 
385
print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";
386
 
387
1;