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
 
35 rodolico 5
our $VERSION = '1.1.0';
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 {
147
   my $prompt = shift;
148
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
149
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
150
}
151
 
35 rodolico 152
# runs a system command. Also, if in testing mode, simply shows what
153
# would have been done.
154
sub runCommand {
155
   while ( my $command = shift ) {
156
      if ( $dryRun ) {
157
         print "$command\n";
158
      } else {
159
         `$command`;
160
      }
161
   }
162
   return 1;
163
} # runCommand
164
 
165
# checks if a directory exists and, if not, creates it
166
my %directories; # holds list of directories already created so no need to do an I/O
167
 
168
sub checkDirectoryExists {
169
   my ( $filename,$mod,$owner ) = @_;
170
   $mod = "0700" unless $mod;
171
   $owner = "root:root" unless $owner;
172
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
173
   my ($fn, $dirname) = fileparse( $filename );
174
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
175
   return '' if exists $directories{$dirname};
176
   if ( -d $dirname ) {
177
      $directories{$dirname} = 1;
178
      return '';
179
   }
180
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
181
      $directories{$dirname} = 1;
182
   }
183
   return '';   
184
}
185
 
21 rodolico 186
sub writeConfig {
35 rodolico 187
   my ( $filename,$content ) = @_;
188
   my $path;
189
   ($filename, $path ) = fileparse( $filename );
190
 
21 rodolico 191
   my $return;
192
   `mkdir -p $path` unless -d $path;
193
   $filename = $path . '/' . $filename;
194
   if ( -e $filename ) {
195
      `cp $filename $filename.bak` if ( -e $filename );
196
      $return .= "Old config copied to $filename.bak\n";
197
   }
35 rodolico 198
   if ( $dryRun ) {
199
      $return .= "Not writing to configuration file\n";
200
   } else {
201
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
202
      print CONF $content;
203
      close CONF;
204
      `chmod 600 $filename`;
205
      $return .= "Configuration successfully written to $filename\n";
206
   }
21 rodolico 207
   return $return;
208
}
209
 
210
sub processParameters {
211
   while ( my $parameter = shift ) {
212
      if ( $parameter eq '-v' ) {
26 rodolico 213
         print "$VERSION\n";
21 rodolico 214
         exit;
215
      }
216
   } # while
217
}
218
 
219
 
220
 
221
1;