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
 
45 rodolico 3
# v1.2.0 20161022 RWR
4
# moved makeConfig here so it is usable by install and configure
5
 
21 rodolico 6
package sysinfoconf;
7
 
42 rodolico 8
 
45 rodolico 9
our $VERSION = '1.2.0';
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 
44 rodolico 20
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
21
                  $scriptDir $confDir $binName $confName
21 rodolico 22
                  &showConf &transportsToConfig &getAnswer &yesno  
35 rodolico 23
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
45 rodolico 24
                  &runCommand &setDryRun &enableModules &makeConfig
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';
44 rodolico 40
our $moduleDir = $binDir . '/modules';
41
our $scriptDir = $binDir . '/scripts';
28 rodolico 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
 
45 rodolico 129
sub makeConfig {
130
   my @configFileNames = @_;
131
   my %config;
132
   my $clientName;
133
   my $serialNumber;
134
   my $hostname;
135
   my @moduleDirs;
136
   my @scriptDirs;
137
   my $transports = {};
138
 
139
   foreach my $config ( @configFileNames ) {
140
      open CONF,"<$config" or die "could not open $config: $!\n";
141
      my $contents = join( '', <CONF> );
142
      close CONF;
143
      # now, eval the information we just read.
144
      # NOTE: we must turn off strict while doing this, and we die if something breaks.
145
      no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
146
   }
147
   unless ( $hostname ) {
148
      $hostname = `hostname -f`;
149
      chomp $hostname;
150
   }
151
 
152
 
153
   $config{'clientName'} = $clientName;
154
   $config{'serialNumber'} = $serialNumber;
155
   $config{'hostname'} = $hostname;
156
   $config{'moduleDirs'} = [ @moduleDirs ];
157
   $config{'scriptDirs'} = [ @scriptDirs ];
158
   $config{'transports'} = $transports;
159
   return \%config;
160
}
161
 
21 rodolico 162
# prompt the user for a response, then allow them to enter it
163
# the first response is considered the default and is printed
164
# in all caps if more than one exists
165
# first answer is used if they simply press the Enter
166
# key. The response is returned all lower case if more than one
167
# exists.
168
# it is assumed 
169
sub getAnswer {
170
   my ( $prompt, @answers ) = @_;
171
   $answers[0] = '' unless defined( $answers[0] );
172
   my $default = $answers[0];
173
   my $onlyOneAnswer = scalar( @answers ) == 1;
174
   print $prompt . '[ ';
175
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
176
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 177
   my $thisAnswer = <>;
21 rodolico 178
   chomp $thisAnswer;
179
   $thisAnswer = $default unless $thisAnswer;
180
   return $thisAnswer;
181
}
182
 
183
sub yesno {
40 rodolico 184
   my ( $prompt, $default ) = @_;
185
   $default = 'yes' unless $default;
186
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 187
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
188
}
189
 
35 rodolico 190
# runs a system command. Also, if in testing mode, simply shows what
191
# would have been done.
192
sub runCommand {
193
   while ( my $command = shift ) {
194
      if ( $dryRun ) {
195
         print "$command\n";
196
      } else {
197
         `$command`;
198
      }
199
   }
200
   return 1;
201
} # runCommand
202
 
203
# checks if a directory exists and, if not, creates it
204
my %directories; # holds list of directories already created so no need to do an I/O
205
 
206
sub checkDirectoryExists {
207
   my ( $filename,$mod,$owner ) = @_;
208
   $mod = "0700" unless $mod;
209
   $owner = "root:root" unless $owner;
210
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
211
   my ($fn, $dirname) = fileparse( $filename );
212
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
213
   return '' if exists $directories{$dirname};
214
   if ( -d $dirname ) {
215
      $directories{$dirname} = 1;
216
      return '';
217
   }
218
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
219
      $directories{$dirname} = 1;
220
   }
221
   return '';   
222
}
223
 
21 rodolico 224
sub writeConfig {
35 rodolico 225
   my ( $filename,$content ) = @_;
226
   my $path;
227
   ($filename, $path ) = fileparse( $filename );
228
 
21 rodolico 229
   my $return;
230
   `mkdir -p $path` unless -d $path;
231
   $filename = $path . '/' . $filename;
232
   if ( -e $filename ) {
233
      `cp $filename $filename.bak` if ( -e $filename );
234
      $return .= "Old config copied to $filename.bak\n";
235
   }
35 rodolico 236
   if ( $dryRun ) {
237
      $return .= "Not writing to configuration file\n";
238
   } else {
239
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
240
      print CONF $content;
241
      close CONF;
242
      `chmod 600 $filename`;
243
      $return .= "Configuration successfully written to $filename\n";
244
   }
21 rodolico 245
   return $return;
246
}
247
 
248
sub processParameters {
249
   while ( my $parameter = shift ) {
250
      if ( $parameter eq '-v' ) {
26 rodolico 251
         print "$VERSION\n";
21 rodolico 252
         exit;
253
      }
254
   } # while
255
}
256
 
257
 
258
 
259
1;