Subversion Repositories camp_sysinfo_client_3

Rev

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

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