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';
6
 
7
my $clientName = '';
8
my $serialNumber = '';
9
my $hostname;
10
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
11
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
12
my $iSendReports = {};
13
# the following are keys which are specific to the different transport channels
14
my @sendEmailScriptKeys = ( 'mailTo', 'mailSubject','mailCC','mailBCC','mailServer','mailFrom','logFile','otherCLParams','tls','smtpUser','smtpPass', 'sendEmailScriptLocation' );
15
my @sendHTTPKeys = ( 'URL', 'urlVarName' );
16
 
17
sub showConf {
18
   my $conf;
19
   $conf .= "\$clientName = '" . $clientName . "';\n";
20
   $conf .= "\$serialNumber = '" . $serialNumber . "';\n";
21
   $conf .= "\$hostname = '" . $hostname . "';\n";
22
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
23
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
24
   foreach my $key ( keys %$iSendReports ) {
25
      $conf .= "\$\$iSendReports{'$key'} = \"" . $$iSendReports{$key} . "\";\n";
26
   }
27
   return $conf;
28
}
29
 
30
# prompt the user for a response, then allow them to enter it
31
# the first response is considered the default and is printed
32
# in all caps if more than one exists
33
# first answer is used if they simply press the Enter
34
# key. The response is returned all lower case if more than one
35
# exists.
36
# it is assumed 
37
sub getAnswer {
38
   my ( $prompt, @answers ) = @_;
39
   my $default = $answers[0];
40
   my $onlyOneAnswer = scalar( @answers ) == 1;
41
   print $prompt . '[ ';
42
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
43
   print join( ' | ', @answers ) . ' ]: ';
44
   $thisAnswer = <>;
45
   chomp $thisAnswer;
46
   $thisAnswer = $default unless $thisAnswer;
47
   return $thisAnswer;
48
}
49
 
50
sub yesno {
51
   my $prompt = shift;
52
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
53
   return lc( substr( $answer, 1, 1 ) ) ne 'y';
54
}
55
 
56
sub userConfirmation {
57
   my $temp;
58
 
59
   $clientName = &getAnswer( "Client Name ", ($clientName) );
60
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
61
   $hostname = &getAnswer( "Host Name ", ($hostname) );
62
 
63
   $temp = join( ',',@moduleDirs );
64
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
65
   @moduleDirs = split( ',', $temp );
66
 
67
   $temp = join( ',',@scriptDirs );
68
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
69
   @scriptDirs = split( ',', $temp );
70
 
71
   $temp = &getAnswer( "How do you want to send the reports ", ('sendEmailScript', 'http', 'sendmail' ) );
72
 
73
   if ( $temp eq 'sendEmailScript' ) {
74
      $$iSendReports{'sendScript'} = 'sendEmailScript';
75
      # get rid of http keys
76
      delete @{$iSendReports} {@sendHTTPKeys};
77
      for $key ( @sendEmailScriptKeys ) {
78
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
79
      }
80
   } elsif ( $temp eq 'http' ) {
81
      $$iSendReports{'sendScript'} = 'upload_http.pl';
82
      # get rid of mail keys
83
      delete @{$iSendReports} { @sendEmailScriptKeys };
84
      for $key ( @sendHTTPKeys ) {
85
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
86
      }
87
   } elsif ( $temp eq 'sendmail' ) {
88
      $iSendReports = {}; # null out the script
89
   } else {
90
      $$iSendReports{'sendScript'} = $temp;
91
      delete @{$iSendReports} { @sendEmailScriptKeys, @sendHTTPKeys };
92
      print "Unknown script, please manually configure\n";
93
   }
94
}
95
 
96
# simply attempts to detect the operating system so we can do OS
97
# specific actions at the end.   
98
sub getOperatingSystem {
99
   my @OSTypes = ( 'ipfire','debian' );
100
   my $OS = `uname -a`;
101
   foreach $osType ( @OSTypes ) {
102
      return $osType if $OS =~ m/$osType/i;
103
   }
104
   return '';
105
} # getOperatingSystem
106
 
107
# special purpose for IPFire routers
108
sub ipFire {
109
   my @BACKUP_DIRS = ( '/etc/camp' );
110
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/fcron.daily/sysinfo.cron` if &yesno( 'Add link to fcron.daily' );
111
   # now, check for backup directories not in include.user
112
   open BACKUP, '</var/ipfire/backup/include.user';
113
   while ( $line = <BACKUP> ) {
114
      chomp $line;
115
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
116
         if ( $BACKUP_DIRS[$i] eq $line ) {
117
            $BACKUP_DIRS[$i] = '';
118
            last;
119
         } # if
120
      }# for
121
   } # while
122
   close BACKUP;
123
 
124
   # if any remain, append them to include.user
125
   open BACKUP, '>>/var/ipfire/backup/include.user';
126
   foreach my $backupDir ( @BACKUP_DIRS ) {
127
      print BACKUP "$backupDir\n" if $backupDir;
128
   }
129
   close BACKUP;
130
 
131
   # set all modules with ipfire or unix in the name to run
132
   opendir $moduleDir, $moduleDirs[0];
133
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
134
   closedir $moduleDir;
135
   foreach my $module (@modules) {
136
      `chmod 0700 $MODULES_DIR/$module`;
137
   }
138
   print "All IPFire specific modules have been enabled\n";
139
}
140
 
141
# some debian specific things
142
sub debian {
143
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo.cron` 
144
      if &yesno( 'Add link to cron.daily' );
145
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
146
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
147
      if ( &yesno( 'Back up old configuration? ') ) {
148
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo` 
149
         print "Old configuration copied to /root/sysinfo.2.config.tgz\n";
150
      }
151
      `apt-get -y --purge remove sysinfo-client`;
152
      print "There may be some unused packages now. You can remove them with\napt-get autoremove\n\n";
153
   }
154
   `ln -s  /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client` 
155
      if &yesno( 'Would you like a link to /usr/local/bin/sysinfo-client' );
156
   # set all modules with debian or unix in the name to run
157
   opendir  $moduleDirs, $moduleDirs[0];
158
   my @modules = grep { /^((dpkg)|(unix))/ } readdir $moduleDir;
159
   closedir $moduleDir;
160
   foreach my $module (@modules) {
161
      `chmod 0700 $MODULES_DIR/$module`;
162
   }
163
   print "All debian specific modules have been enabled\n";
164
}
165
 
166
# See if existing configuration. If so, offer to load it
167
if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
168
   print "Loading defaults from existing config $sysinfo3\n";
169
   my $client_name;
170
   open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
171
   my $temp = join( '', <SEED> );
172
   close SEED;
173
   eval( $temp );
174
} else { # nope, so see if an old sysinfo2 or a seed file exists
175
   if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
176
      print "Loading defaults from sysinfo2 config $sysinfo2\n";
177
      my $client_name;
178
      open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
179
      my $temp = join( '', <SEED> );
180
      close SEED;
181
      eval( $temp );
182
      $clientName = $client_name if ( $client_name );
183
   }
184
 
185
   if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
186
      print "Loading seed file $seedFile\n";
187
      open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
188
      my $temp = join( '', <SEED> );
189
      close SEED;
190
      eval( $temp );
191
   }
192
}
193
 
194
unless ( $hostname ) {
195
   $hostname = `hostname -f`;
196
   chomp $hostname;
197
}
198
 
199
&userConfirmation();
200
 
201
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
202
print &showConf(  );
203
print '-'x40 . "\n";
204
print "Ok, I'll write it\n" if &yesno( "Write this configuration" );
205
 
206
my $os = &getOperatingSystem();
207
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
208
   &ipFire() if $os = 'ipfire';
209
   &debian() if $os = 'debian';
210
}
211
 
212
print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";
213
 
214
1;