Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 rodolico 1
#!/usr/bin/env perl
2
 
3
package sysinfoconf;
4
 
40 rodolico 5
our $VERSION = '1.1.1';
21 rodolico 6
use warnings;
26 rodolico 7
use strict;  
8
 
35 rodolico 9
use Data::Dumper;
10
use File::Basename;
11
 
21 rodolico 12
use Exporter;
13
 
14
our @ISA = qw( Exporter );
15
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
16
                  $transports $sysinfo3 %sendTypes $binDir $modulesDir
17
                  $scriptsDir $confDir $binName $confName
18
                  &showConf &transportsToConfig &getAnswer &yesno  
35 rodolico 19
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
37 rodolico 20
                  &runCommand &setDryRun &enableModules
21 rodolico 21
                );
22
 
36 rodolico 23
our $TESTING = 0;
35 rodolico 24
our $dryRun;
28 rodolico 25
our $clientName = '';
26
our $serialNumber = '';
27
our $hostname = '';
28
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
29
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
30
our $transports = {}; # holds transportation mechanisms
21 rodolico 31
# the following are keys which are specific to the different transport channels
32
 
28 rodolico 33
our $sysinfo3 = '/etc/camp/sysinfo-client/sysinfo-client.conf';
21 rodolico 34
 
28 rodolico 35
our $binDir = '/opt/camp/sysinfo-client';
36
our $modulesDir = $binDir . '/modules';
37
our $scriptsDir = $binDir . '/scripts';
38
our $confDir = '/etc/camp/sysinfo-client';
21 rodolico 39
 
28 rodolico 40
our $binName = 'sysinfo-client';
41
our $confName = 'sysinfo-client.conf';
21 rodolico 42
 
35 rodolico 43
sub setDryRun {
44
   $dryRun = shift;
45
}
21 rodolico 46
 
28 rodolico 47
our %sendTypes = ( 
21 rodolico 48
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
49
                                      'keys' => 
50
                                      [
51
                                        'mailTo',
52
                                        'mailSubject',
53
                                        'mailCC',
54
                                        'mailBCC',
55
                                        'mailServer',
56
                                        'mailFrom',
57
                                        'logFile',
58
                                        'otherCLParams',
59
                                        'tls',
60
                                        'smtpUser',
61
                                        'smtpPass',
62
                                        'sendEmailScriptLocation'
63
                                      ],
64
                                    },
65
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
66
                                      'keys' => 
67
                                      [
68
                                        'URL',
69
                                        'key for report',
70
                                        'key for date',
71
                                        'key for hostname',
72
                                        'key for client',
73
                                        'key for serial number'
74
                                      ]
75
                                    }
76
                );
77
 
78
 
37 rodolico 79
sub enableModules {
80
   my $moduleDir = shift;
81
   my %modules;
82
   if ( opendir( my $dh, "$moduleDir" ) ) {
83
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
84
                 grep { ! /^\./ } 
85
                 readdir $dh;
86
      closedir( $dh );
87
      foreach my $filename ( keys %modules ) {
88
         if ( open FILE,"<$moduleDir/$_" ) {
89
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
90
            close FILE;
91
            chomp @descript;
92
            $modules{$filename}{'description'} = $descript[0];
93
         } # if
94
      } # foreach
95
   } # if
96
} # enableModules
97
 
21 rodolico 98
sub showConf {
35 rodolico 99
   my $configuration = shift;
100
#   print Dumper( $configuration ); die;
21 rodolico 101
   my $conf;
35 rodolico 102
   $conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
103
   $conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
104
   $conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
105
   $conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
106
   $conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
107
   $conf .= &transportsToConfig( $$configuration{ 'transports' } );
21 rodolico 108
   return $conf;
109
}
110
 
111
sub transportsToConfig {
35 rodolico 112
   my $transports = shift;
21 rodolico 113
   my $config;
114
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
115
      my $name = $$transports{ $priority }{'-name-'};
116
      $config .= "# Tranport $name at priority $priority\n";
117
      my $thisOne = $$transports{ $priority };
118
      foreach my $key ( sort keys %$thisOne ) {
119
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
120
      } # foreach
121
   } # foreach
122
   return $config;
123
} # transportsToConfig
124
 
125
# prompt the user for a response, then allow them to enter it
126
# the first response is considered the default and is printed
127
# in all caps if more than one exists
128
# first answer is used if they simply press the Enter
129
# key. The response is returned all lower case if more than one
130
# exists.
131
# it is assumed 
132
sub getAnswer {
133
   my ( $prompt, @answers ) = @_;
134
   $answers[0] = '' unless defined( $answers[0] );
135
   my $default = $answers[0];
136
   my $onlyOneAnswer = scalar( @answers ) == 1;
137
   print $prompt . '[ ';
138
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
139
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 140
   my $thisAnswer = <>;
21 rodolico 141
   chomp $thisAnswer;
142
   $thisAnswer = $default unless $thisAnswer;
143
   return $thisAnswer;
144
}
145
 
146
sub yesno {
40 rodolico 147
   my ( $prompt, $default ) = @_;
148
   $default = 'yes' unless $default;
149
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 150
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
151
}
152
 
35 rodolico 153
# runs a system command. Also, if in testing mode, simply shows what
154
# would have been done.
155
sub runCommand {
156
   while ( my $command = shift ) {
157
      if ( $dryRun ) {
158
         print "$command\n";
159
      } else {
160
         `$command`;
161
      }
162
   }
163
   return 1;
164
} # runCommand
165
 
166
# checks if a directory exists and, if not, creates it
167
my %directories; # holds list of directories already created so no need to do an I/O
168
 
169
sub checkDirectoryExists {
170
   my ( $filename,$mod,$owner ) = @_;
171
   $mod = "0700" unless $mod;
172
   $owner = "root:root" unless $owner;
173
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
174
   my ($fn, $dirname) = fileparse( $filename );
175
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
176
   return '' if exists $directories{$dirname};
177
   if ( -d $dirname ) {
178
      $directories{$dirname} = 1;
179
      return '';
180
   }
181
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
182
      $directories{$dirname} = 1;
183
   }
184
   return '';   
185
}
186
 
21 rodolico 187
sub writeConfig {
35 rodolico 188
   my ( $filename,$content ) = @_;
189
   my $path;
190
   ($filename, $path ) = fileparse( $filename );
191
 
21 rodolico 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
   }
35 rodolico 199
   if ( $dryRun ) {
200
      $return .= "Not writing to configuration file\n";
201
   } else {
202
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
203
      print CONF $content;
204
      close CONF;
205
      `chmod 600 $filename`;
206
      $return .= "Configuration successfully written to $filename\n";
207
   }
21 rodolico 208
   return $return;
209
}
210
 
211
sub processParameters {
212
   while ( my $parameter = shift ) {
213
      if ( $parameter eq '-v' ) {
26 rodolico 214
         print "$VERSION\n";
21 rodolico 215
         exit;
216
      }
217
   } # while
218
}
219
 
220
 
221
 
222
1;