Subversion Repositories camp_sysinfo_client_3

Rev

Rev 14 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14 Rev 16
Line 1... Line 1...
1
#!/usr/bin/env perl
1
#!/usr/bin/env perl
2
 
2
 
-
 
3
use warnings;
-
 
4
 
3
my $seedFile = 'sysinfo-client.seed';
5
my $seedFile = 'sysinfo-client.seed';
4
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
6
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
5
my $sysinfo3 = '/etc/camp/sysinfo-client.conf';
7
my $sysinfo3 = '/etc/camp/sysinfo-client.conf';
6
my $configPath = '/etc/camp';
8
my $configPath = '/etc/camp';
7
my $configFile = 'sysinfo-client.conf';
9
my $configFile = 'sysinfo-client.conf';
Line 9... Line 11...
9
my $clientName = '';
11
my $clientName = '';
10
my $serialNumber = '';
12
my $serialNumber = '';
11
my $hostname;
13
my $hostname;
12
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
14
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
13
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
15
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
14
my $iSendReports = {};
16
my $transports = {}; # holds transportation mechanisms
15
# the following are keys which are specific to the different transport channels
17
# the following are keys which are specific to the different transport channels
-
 
18
 
-
 
19
my %sendTypes = ( 
16
my @sendEmailScriptKeys = ( 'mailTo', 'mailSubject','mailCC','mailBCC','mailServer','mailFrom','logFile','otherCLParams','tls','smtpUser','smtpPass', 'sendEmailScriptLocation' );
20
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
17
my @sendHTTPKeys = ( 'URL', 'urlVarName' );
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
 
18
 
51
 
19
sub showConf {
52
sub showConf {
20
   my $conf;
53
   my $conf;
21
   $conf .= "\$clientName = '" . $clientName . "';\n";
54
   $conf .= "\$clientName = '" . $clientName . "';\n";
22
   $conf .= "\$serialNumber = '" . $serialNumber . "';\n";
55
   $conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
23
   $conf .= "\$hostname = '" . $hostname . "';\n";
56
   $conf .= "\$hostname = '" . $hostname . "';\n";
24
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
57
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
25
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
58
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
26
   foreach my $key ( keys %$iSendReports ) {
59
   $conf .= &transportsToConfig();
27
      $conf .= "\$\$iSendReports{'$key'} = '" . $$iSendReports{$key} . "';\n";
-
 
28
   }
-
 
29
   return $conf;
60
   return $conf;
30
}
61
}
31
 
62
 
-
 
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
 
32
# prompt the user for a response, then allow them to enter it
84
# prompt the user for a response, then allow them to enter it
33
# the first response is considered the default and is printed
85
# the first response is considered the default and is printed
34
# in all caps if more than one exists
86
# in all caps if more than one exists
35
# first answer is used if they simply press the Enter
87
# first answer is used if they simply press the Enter
36
# key. The response is returned all lower case if more than one
88
# key. The response is returned all lower case if more than one
37
# exists.
89
# exists.
38
# it is assumed 
90
# it is assumed 
39
sub getAnswer {
91
sub getAnswer {
40
   my ( $prompt, @answers ) = @_;
92
   my ( $prompt, @answers ) = @_;
-
 
93
   $answers[0] = '' unless defined( $answers[0] );
41
   my $default = $answers[0];
94
   my $default = $answers[0];
42
   my $onlyOneAnswer = scalar( @answers ) == 1;
95
   my $onlyOneAnswer = scalar( @answers ) == 1;
43
   print $prompt . '[ ';
96
   print $prompt . '[ ';
44
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
97
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
45
   print join( ' | ', @answers ) . ' ]: ';
98
   print join( ' | ', @answers ) . ' ]: ';
Line 85... Line 138...
85
 
138
 
86
   $temp = join( ',',@scriptDirs );
139
   $temp = join( ',',@scriptDirs );
87
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
140
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
88
   @scriptDirs = split( ',', $temp );
141
   @scriptDirs = split( ',', $temp );
89
 
142
 
90
   $temp = &getAnswer( "How do you want to send the reports ", ('sendEmailScript', 'http', 'sendmail' ) );
-
 
91
 
-
 
92
   if ( $temp eq 'sendEmailScript' ) {
-
 
93
      $$iSendReports{'sendScript'} = 'sendEmailScript';
-
 
94
      $$iSendReports{'sendEmailScriptLocation'} = &findSendEmail( $$iSendReports{'sendEmailScriptLocation'} );
-
 
95
           
-
 
96
      # get rid of http keys
-
 
97
      delete @{$iSendReports} {@sendHTTPKeys};
-
 
98
      for $key ( @sendEmailScriptKeys ) {
-
 
99
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
-
 
100
      }
-
 
101
   } elsif ( $temp eq 'http' ) {
-
 
102
      $$iSendReports{'sendScript'} = 'upload_http.pl';
-
 
103
      # get rid of mail keys
-
 
104
      delete @{$iSendReports} { @sendEmailScriptKeys };
-
 
105
      for $key ( @sendHTTPKeys ) {
-
 
106
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
-
 
107
      }
-
 
108
   } elsif ( $temp eq 'sendmail' ) {
-
 
109
      $iSendReports = {}; # null out the script
-
 
110
   } else {
-
 
111
      $$iSendReports{'sendScript'} = $temp;
-
 
112
      delete @{$iSendReports} { @sendEmailScriptKeys, @sendHTTPKeys };
-
 
113
      print "Unknown script, please manually configure\n";
-
 
114
   }
-
 
115
}
143
}
116
 
144
 
117
# simply attempts to detect the operating system so we can do OS
145
# simply attempts to detect the operating system so we can do OS
118
# specific actions at the end.   
146
# specific actions at the end.   
119
sub getOperatingSystem {
147
sub getOperatingSystem {
Line 148... Line 176...
148
      print BACKUP "$backupDir\n" if $backupDir;
176
      print BACKUP "$backupDir\n" if $backupDir;
149
   }
177
   }
150
   close BACKUP;
178
   close BACKUP;
151
 
179
 
152
   # set all modules with ipfire or unix in the name to run
180
   # set all modules with ipfire or unix in the name to run
-
 
181
   my $moduleDir = $moduleDirs[0];
153
   opendir $moduleDir, $moduleDirs[0];
182
   opendir $moduleDir, $moduleDir;
154
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
183
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
155
   closedir $moduleDir;
184
   closedir $moduleDir;
156
   foreach my $module (@modules) {
185
   foreach my $module ( @modules) {
157
      `chmod 0700 $MODULES_DIR/$module`;
186
      `chmod 0700 $moduleDir/$module`;
158
   }
187
   }
159
   print "All IPFire specific modules have been enabled\n";
188
   print "All IPFire specific modules have been enabled\n";
160
}
189
}
161
 
190
 
162
# some debian specific things
191
# some debian specific things
163
sub debian {
192
sub debian {
164
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo.cron` 
193
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo` 
165
      if &yesno( 'Add link to cron.daily' );
194
      if &yesno( 'Add link to cron.daily' );
166
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
195
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
167
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
196
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
168
      if ( &yesno( 'Back up old configuration? ') ) {
197
      if ( &yesno( 'Back up old configuration? ') ) {
169
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
198
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
Line 185... Line 214...
185
      }
214
      }
186
   }
215
   }
187
   print "All debian specific modules have been enabled\n";
216
   print "All debian specific modules have been enabled\n";
188
}
217
}
189
 
218
 
-
 
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
 
190
sub writeConfig {
288
sub writeConfig {
191
   my ($path,$filename,$content) = @_;
289
   my ($path,$filename,$content) = @_;
192
   my $return;
290
   my $return;
193
   `mkdir -p $path` unless -d $path;
291
   `mkdir -p $path` unless -d $path;
194
   $filename = $path . '/' . $filename;
292
   $filename = $path . '/' . $filename;
Line 201... Line 299...
201
   close CONF;
299
   close CONF;
202
   $return .= "Configuration successfully written to $filename\n";
300
   $return .= "Configuration successfully written to $filename\n";
203
   return $return;
301
   return $return;
204
}
302
}
205
 
303
 
206
# See if existing configuration. If so, offer to load it
304
sub convertSysinfo2 {
207
if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
-
 
208
   print "Loading defaults from existing config $sysinfo3\n";
-
 
209
   my $client_name;
305
   my $client_name;
-
 
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
210
   open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
319
   open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
211
   my $temp = join( '', <SEED> );
320
   my $temp = join( '', <SEED> );
212
   close SEED;
321
   close SEED;
213
   eval( $temp );
322
   eval( $temp );
-
 
323
   if ( $iMailResults ) {
-
 
324
      $temp = {};
214
} else { # nope, so see if an old sysinfo2 or a seed file exists
325
      $$temp{'-name-'} = 'SendEmail';
215
   if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
326
      $$temp{'mailTo'} = $mailTo if $mailTo;
216
      print "Loading defaults from sysinfo2 config $sysinfo2\n";
327
      $$temp{'$mailFrom'} = $mailFrom if $mailFrom;
217
      my $client_name;
328
      $$temp{'mailCC'} = $mailCC if $mailCC;
218
      open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
329
      $$temp{'mailServer'} = $mailServer if $mailServer;
219
      my $temp = join( '', <SEED> );
330
      $$temp{'mailServer'} .= ":$mailServerPort" if $mailServerPort;
220
      close SEED;
331
      $$transports{'1'} = $temp;
221
      eval( $temp );
332
   }   
222
      $clientName = $client_name if ( $client_name );
333
   $clientName = $client_name if ( $client_name );
-
 
334
   return ( $clientName,$hostname,$serialNumber,$transports );
223
   }
335
}
224
 
336
 
-
 
337
sub loadConfig {
-
 
338
   # See if existing configuration. If so, offer to load it
225
   if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
339
   if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
226
      print "Loading seed file $seedFile\n";
340
      print "Loading defaults from existing config $sysinfo3\n";
-
 
341
      my $client_name;
227
      open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
342
      open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
228
      my $temp = join( '', <SEED> );
343
      my $temp = join( '', <SEED> );
229
      close SEED;
344
      close SEED;
230
      eval( $temp );
345
      eval( $temp );
-
 
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
      }
231
   }
360
   }
232
}
361
}
233
 
362
 
-
 
363
&loadConfig();
-
 
364
 
234
unless ( $hostname ) {
365
unless ( $hostname ) {
235
   $hostname = `hostname -f`;
366
   $hostname = `hostname -f`;
236
   chomp $hostname;
367
   chomp $hostname;
237
}
368
}
238
 
369
 
239
&userConfirmation();
370
&userConfirmation();
-
 
371
&doTransports( $transports );
240
 
372
 
241
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
373
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
242
print &showConf(  );
374
print &showConf(  );
243
print '-'x40 . "\n";
375
print '-'x40 . "\n";
244
print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );
376
#print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );
245
 
377
 
246
 
378
 
247
my $os = &getOperatingSystem();
379
my $os = &getOperatingSystem();
248
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
380
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
249
   &ipFire() if $os eq 'ipfire';
381
   &ipFire() if $os eq 'ipfire';