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
20
                  &runCommand &setDryRun
21 rodolico 21
                );
22
 
35 rodolico 23
our $TESTING = 3;
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
 
79
sub showConf {
35 rodolico 80
   my $configuration = shift;
81
#   print Dumper( $configuration ); die;
21 rodolico 82
   my $conf;
35 rodolico 83
   $conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
84
   $conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
85
   $conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
86
   $conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
87
   $conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
88
   $conf .= &transportsToConfig( $$configuration{ 'transports' } );
21 rodolico 89
   return $conf;
90
}
91
 
92
sub transportsToConfig {
35 rodolico 93
   my $transports = shift;
21 rodolico 94
   my $config;
95
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
96
      my $name = $$transports{ $priority }{'-name-'};
97
      $config .= "# Tranport $name at priority $priority\n";
98
      my $thisOne = $$transports{ $priority };
99
      foreach my $key ( sort keys %$thisOne ) {
100
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
101
      } # foreach
102
   } # foreach
103
   return $config;
104
} # transportsToConfig
105
 
106
# prompt the user for a response, then allow them to enter it
107
# the first response is considered the default and is printed
108
# in all caps if more than one exists
109
# first answer is used if they simply press the Enter
110
# key. The response is returned all lower case if more than one
111
# exists.
112
# it is assumed 
113
sub getAnswer {
114
   my ( $prompt, @answers ) = @_;
115
   $answers[0] = '' unless defined( $answers[0] );
116
   my $default = $answers[0];
117
   my $onlyOneAnswer = scalar( @answers ) == 1;
118
   print $prompt . '[ ';
119
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
120
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 121
   my $thisAnswer = <>;
21 rodolico 122
   chomp $thisAnswer;
123
   $thisAnswer = $default unless $thisAnswer;
124
   return $thisAnswer;
125
}
126
 
127
sub yesno {
128
   my $prompt = shift;
129
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
130
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
131
}
132
 
35 rodolico 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
 
21 rodolico 167
sub writeConfig {
35 rodolico 168
   my ( $filename,$content ) = @_;
169
   my $path;
170
   ($filename, $path ) = fileparse( $filename );
171
 
21 rodolico 172
   my $return;
173
   `mkdir -p $path` unless -d $path;
174
   $filename = $path . '/' . $filename;
175
   if ( -e $filename ) {
176
      `cp $filename $filename.bak` if ( -e $filename );
177
      $return .= "Old config copied to $filename.bak\n";
178
   }
35 rodolico 179
   if ( $dryRun ) {
180
      $return .= "Not writing to configuration file\n";
181
   } else {
182
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
183
      print CONF $content;
184
      close CONF;
185
      `chmod 600 $filename`;
186
      $return .= "Configuration successfully written to $filename\n";
187
   }
21 rodolico 188
   return $return;
189
}
190
 
191
sub processParameters {
192
   while ( my $parameter = shift ) {
193
      if ( $parameter eq '-v' ) {
26 rodolico 194
         print "$VERSION\n";
21 rodolico 195
         exit;
196
      }
197
   } # while
198
}
199
 
200
 
201
 
202
1;