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
 
3
my $seedFile = 'sysinfo-client.seed';
4
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
5
my $sysinfo3 = '/etc/camp/sysinfo-client.conf';
12 rodolico 6
my $configPath = '/etc/camp';
7
my $configFile = 'sysinfo-client.conf';
11 rodolico 8
 
9
my $clientName = '';
10
my $serialNumber = '';
11
my $hostname;
12
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
13
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
14
my $iSendReports = {};
15
# the following are keys which are specific to the different transport channels
16
my @sendEmailScriptKeys = ( 'mailTo', 'mailSubject','mailCC','mailBCC','mailServer','mailFrom','logFile','otherCLParams','tls','smtpUser','smtpPass', 'sendEmailScriptLocation' );
17
my @sendHTTPKeys = ( 'URL', 'urlVarName' );
18
 
19
sub showConf {
20
   my $conf;
21
   $conf .= "\$clientName = '" . $clientName . "';\n";
22
   $conf .= "\$serialNumber = '" . $serialNumber . "';\n";
23
   $conf .= "\$hostname = '" . $hostname . "';\n";
24
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
25
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
26
   foreach my $key ( keys %$iSendReports ) {
12 rodolico 27
      $conf .= "\$\$iSendReports{'$key'} = '" . $$iSendReports{$key} . "';\n";
11 rodolico 28
   }
29
   return $conf;
30
}
31
 
32
# prompt the user for a response, then allow them to enter it
33
# the first response is considered the default and is printed
34
# in all caps if more than one exists
35
# first answer is used if they simply press the Enter
36
# key. The response is returned all lower case if more than one
37
# exists.
38
# it is assumed 
39
sub getAnswer {
40
   my ( $prompt, @answers ) = @_;
41
   my $default = $answers[0];
42
   my $onlyOneAnswer = scalar( @answers ) == 1;
43
   print $prompt . '[ ';
44
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
45
   print join( ' | ', @answers ) . ' ]: ';
46
   $thisAnswer = <>;
47
   chomp $thisAnswer;
48
   $thisAnswer = $default unless $thisAnswer;
49
   return $thisAnswer;
50
}
51
 
52
sub yesno {
53
   my $prompt = shift;
54
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
12 rodolico 55
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
11 rodolico 56
}
57
 
12 rodolico 58
sub findSendEmail {
59
   my $currentLocation = shift;
60
   my @possibles = ( '/opt/sendEmail/sendEmail', '/opt/sendEmail/sendEmail.pl' );
61
   return $currentLocation if ( $currentLocation && -x $currentLocation );
62
   # well, we didn't find it, so look around some more
63
   # we install it in /opt, so try there
64
   foreach my $current ( @possibles ) {
65
      return $current if -x $current;
66
   }
67
   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" ) ) {
68
      $path = `perl getSendEmail.pl`;
69
      chomp $path;
70
      return $path;
71
   }
72
   return '';
73
}
74
 
11 rodolico 75
sub userConfirmation {
76
   my $temp;
77
 
78
   $clientName = &getAnswer( "Client Name ", ($clientName) );
79
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
80
   $hostname = &getAnswer( "Host Name ", ($hostname) );
81
 
82
   $temp = join( ',',@moduleDirs );
83
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
84
   @moduleDirs = split( ',', $temp );
85
 
86
   $temp = join( ',',@scriptDirs );
87
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
88
   @scriptDirs = split( ',', $temp );
89
 
90
   $temp = &getAnswer( "How do you want to send the reports ", ('sendEmailScript', 'http', 'sendmail' ) );
91
 
92
   if ( $temp eq 'sendEmailScript' ) {
93
      $$iSendReports{'sendScript'} = 'sendEmailScript';
12 rodolico 94
      $$iSendReports{'sendEmailScriptLocation'} = &findSendEmail( $$iSendReports{'sendEmailScriptLocation'} );
95
 
11 rodolico 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
}
116
 
117
# simply attempts to detect the operating system so we can do OS
118
# specific actions at the end.   
119
sub getOperatingSystem {
120
   my @OSTypes = ( 'ipfire','debian' );
121
   my $OS = `uname -a`;
122
   foreach $osType ( @OSTypes ) {
123
      return $osType if $OS =~ m/$osType/i;
124
   }
125
   return '';
126
} # getOperatingSystem
127
 
128
# special purpose for IPFire routers
129
sub ipFire {
130
   my @BACKUP_DIRS = ( '/etc/camp' );
131
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/fcron.daily/sysinfo.cron` if &yesno( 'Add link to fcron.daily' );
132
   # now, check for backup directories not in include.user
133
   open BACKUP, '</var/ipfire/backup/include.user';
134
   while ( $line = <BACKUP> ) {
135
      chomp $line;
136
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
137
         if ( $BACKUP_DIRS[$i] eq $line ) {
138
            $BACKUP_DIRS[$i] = '';
139
            last;
140
         } # if
141
      }# for
142
   } # while
143
   close BACKUP;
144
 
145
   # if any remain, append them to include.user
146
   open BACKUP, '>>/var/ipfire/backup/include.user';
147
   foreach my $backupDir ( @BACKUP_DIRS ) {
148
      print BACKUP "$backupDir\n" if $backupDir;
149
   }
150
   close BACKUP;
151
 
152
   # set all modules with ipfire or unix in the name to run
153
   opendir $moduleDir, $moduleDirs[0];
154
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
155
   closedir $moduleDir;
156
   foreach my $module (@modules) {
157
      `chmod 0700 $MODULES_DIR/$module`;
158
   }
159
   print "All IPFire specific modules have been enabled\n";
160
}
161
 
162
# some debian specific things
163
sub debian {
164
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo.cron` 
165
      if &yesno( 'Add link to cron.daily' );
166
   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' ) ) {
168
      if ( &yesno( 'Back up old configuration? ') ) {
12 rodolico 169
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
11 rodolico 170
         print "Old configuration copied to /root/sysinfo.2.config.tgz\n";
171
      }
172
      `apt-get -y --purge remove sysinfo-client`;
173
      print "There may be some unused packages now. You can remove them with\napt-get autoremove\n\n";
174
   }
175
   `ln -s  /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client` 
176
      if &yesno( 'Would you like a link to /usr/local/bin/sysinfo-client' );
177
   # set all modules with debian or unix in the name to run
14 rodolico 178
   foreach my $directory ( @moduleDirs ) {
179
      opendir ( $dh, $directory ) or die "Could not open directory [$directory]\n";
180
      my @modules = grep { /^((dpkg)|(unix))/ } readdir $dh;
181
      closedir $dh;
182
      foreach my $module (@modules) {
183
         `chmod 0700 $directory/$module`;
184
         `chown root:root $directory/$module`;
185
      }
11 rodolico 186
   }
187
   print "All debian specific modules have been enabled\n";
188
}
189
 
12 rodolico 190
sub writeConfig {
191
   my ($path,$filename,$content) = @_;
192
   my $return;
193
   `mkdir -p $path` unless -d $path;
194
   $filename = $path . '/' . $filename;
195
   if ( -e $filename ) {
196
      `cp $filename $filename.bak` if ( -e $filename );
197
      $return .= "Old config copied to $filename.bak\n";
198
   }
199
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
200
   print CONF $content;
201
   close CONF;
202
   $return .= "Configuration successfully written to $filename\n";
203
   return $return;
204
}
205
 
11 rodolico 206
# See if existing configuration. If so, offer to load it
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;
210
   open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
211
   my $temp = join( '', <SEED> );
212
   close SEED;
213
   eval( $temp );
214
} else { # nope, so see if an old sysinfo2 or a seed file exists
215
   if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
216
      print "Loading defaults from sysinfo2 config $sysinfo2\n";
217
      my $client_name;
218
      open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
219
      my $temp = join( '', <SEED> );
220
      close SEED;
221
      eval( $temp );
222
      $clientName = $client_name if ( $client_name );
223
   }
224
 
225
   if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
226
      print "Loading seed file $seedFile\n";
227
      open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
228
      my $temp = join( '', <SEED> );
229
      close SEED;
230
      eval( $temp );
231
   }
232
}
233
 
234
unless ( $hostname ) {
235
   $hostname = `hostname -f`;
236
   chomp $hostname;
237
}
238
 
239
&userConfirmation();
240
 
241
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
242
print &showConf(  );
243
print '-'x40 . "\n";
12 rodolico 244
print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );
11 rodolico 245
 
12 rodolico 246
 
11 rodolico 247
my $os = &getOperatingSystem();
248
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
12 rodolico 249
   &ipFire() if $os eq 'ipfire';
250
   &debian() if $os eq 'debian';
11 rodolico 251
}
252
 
253
print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";
254
 
255
1;