Subversion Repositories camp_sysinfo_client_3

Rev

Rev 28 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 28 Rev 35
Line 1... Line 1...
1
#!/usr/bin/env perl
1
#!/usr/bin/env perl
2
 
2
 
3
package sysinfoconf;
3
package sysinfoconf;
4
 
4
 
5
our $VERSION = '1.0.1';
5
our $VERSION = '1.1.0';
6
use warnings;
6
use warnings;
7
use strict;  
7
use strict;  
8
 
8
 
-
 
9
use Data::Dumper;
-
 
10
use File::Basename;
-
 
11
 
9
use Exporter;
12
use Exporter;
10
 
13
 
11
our @ISA = qw( Exporter );
14
our @ISA = qw( Exporter );
12
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
15
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
13
                  $transports $sysinfo3 %sendTypes $binDir $modulesDir
16
                  $transports $sysinfo3 %sendTypes $binDir $modulesDir
14
                  $scriptsDir $confDir $binName $confName
17
                  $scriptsDir $confDir $binName $confName
15
                  &showConf &transportsToConfig &getAnswer &yesno  
18
                  &showConf &transportsToConfig &getAnswer &yesno  
16
                  &writeConfig &processParameters $TESTING
19
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
-
 
20
                  &runCommand &setDryRun
17
                );
21
                );
18
 
22
 
19
our $TESTING;
23
our $TESTING = 3;
-
 
24
our $dryRun;
20
our $clientName = '';
25
our $clientName = '';
21
our $serialNumber = '';
26
our $serialNumber = '';
22
our $hostname = '';
27
our $hostname = '';
23
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
28
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
24
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
29
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
Line 33... Line 38...
33
our $confDir = '/etc/camp/sysinfo-client';
38
our $confDir = '/etc/camp/sysinfo-client';
34
 
39
 
35
our $binName = 'sysinfo-client';
40
our $binName = 'sysinfo-client';
36
our $confName = 'sysinfo-client.conf';
41
our $confName = 'sysinfo-client.conf';
37
 
42
 
-
 
43
sub setDryRun {
-
 
44
   $dryRun = shift;
-
 
45
}
38
 
46
 
39
our %sendTypes = ( 
47
our %sendTypes = ( 
40
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
48
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
41
                                      'keys' => 
49
                                      'keys' => 
42
                                      [
50
                                      [
Line 67... Line 75...
67
                                    }
75
                                    }
68
                );
76
                );
69
 
77
 
70
 
78
 
71
sub showConf {
79
sub showConf {
-
 
80
   my $configuration = shift;
-
 
81
#   print Dumper( $configuration ); die;
72
   my $conf;
82
   my $conf;
73
   $conf .= "\$clientName = '" . $clientName . "';\n";
83
   $conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
74
   $conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
84
   $conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
75
   $conf .= "\$hostname = '" . $hostname . "';\n";
85
   $conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
76
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
86
   $conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
77
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
87
   $conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
78
   $conf .= &transportsToConfig();
88
   $conf .= &transportsToConfig( $$configuration{ 'transports' } );
79
   return $conf;
89
   return $conf;
80
}
90
}
81
 
91
 
82
sub transportsToConfig {
92
sub transportsToConfig {
-
 
93
   my $transports = shift;
83
   my $config;
94
   my $config;
84
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
95
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
85
      my $name = $$transports{ $priority }{'-name-'};
96
      my $name = $$transports{ $priority }{'-name-'};
86
      $config .= "# Tranport $name at priority $priority\n";
97
      $config .= "# Tranport $name at priority $priority\n";
87
      my $thisOne = $$transports{ $priority };
98
      my $thisOne = $$transports{ $priority };
Line 117... Line 128...
117
   my $prompt = shift;
128
   my $prompt = shift;
118
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
129
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
119
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
130
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
120
}
131
}
121
 
132
 
-
 
133
# runs a system command. Also, if in testing mode, simply shows what
-
 
134
# would have been done.
-
 
135
sub runCommand {
-
 
136
   while ( my $command = shift ) {
-
 
137
      if ( $dryRun ) {
-
 
138
         print "$command\n";
-
 
139
      } else {
-
 
140
         `$command`;
-
 
141
      }
-
 
142
   }
-
 
143
   return 1;
-
 
144
} # runCommand
-
 
145
 
-
 
146
# checks if a directory exists and, if not, creates it
-
 
147
my %directories; # holds list of directories already created so no need to do an I/O
-
 
148
 
-
 
149
sub checkDirectoryExists {
-
 
150
   my ( $filename,$mod,$owner ) = @_;
-
 
151
   $mod = "0700" unless $mod;
-
 
152
   $owner = "root:root" unless $owner;
-
 
153
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
-
 
154
   my ($fn, $dirname) = fileparse( $filename );
-
 
155
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
-
 
156
   return '' if exists $directories{$dirname};
-
 
157
   if ( -d $dirname ) {
-
 
158
      $directories{$dirname} = 1;
-
 
159
      return '';
-
 
160
   }
-
 
161
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
-
 
162
      $directories{$dirname} = 1;
-
 
163
   }
-
 
164
   return '';   
-
 
165
}
-
 
166
 
122
sub writeConfig {
167
sub writeConfig {
123
   my ($path,$filename,$content) = @_;
168
   my ( $filename,$content ) = @_;
-
 
169
   my $path;
-
 
170
   ($filename, $path ) = fileparse( $filename );
-
 
171
 
124
   my $return;
172
   my $return;
125
   `mkdir -p $path` unless -d $path;
173
   `mkdir -p $path` unless -d $path;
126
   $filename = $path . '/' . $filename;
174
   $filename = $path . '/' . $filename;
127
   if ( -e $filename ) {
175
   if ( -e $filename ) {
128
      `cp $filename $filename.bak` if ( -e $filename );
176
      `cp $filename $filename.bak` if ( -e $filename );
129
      $return .= "Old config copied to $filename.bak\n";
177
      $return .= "Old config copied to $filename.bak\n";
130
   }
178
   }
-
 
179
   if ( $dryRun ) {
-
 
180
      $return .= "Not writing to configuration file\n";
-
 
181
   } else {
131
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
182
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
132
   print CONF $content;
183
      print CONF $content;
133
   close CONF;
184
      close CONF;
134
   `chmod 600 $filename`;
185
      `chmod 600 $filename`;
135
   $return .= "Configuration successfully written to $filename\n";
186
      $return .= "Configuration successfully written to $filename\n";
-
 
187
   }
136
   return $return;
188
   return $return;
137
}
189
}
138
 
190
 
139
sub processParameters {
191
sub processParameters {
140
   while ( my $parameter = shift ) {
192
   while ( my $parameter = shift ) {