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