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
53 rodolico 24
                  &runCommand &setDryRun &enableModules &makeConfig &findConf
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 = '';
53 rodolico 32
 
33
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
34
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
35
 
36
 
28 rodolico 37
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
38
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
39
our $transports = {}; # holds transportation mechanisms
21 rodolico 40
# the following are keys which are specific to the different transport channels
41
 
53 rodolico 42
our $sysinfo3 = 'sysinfo-client.conf';
21 rodolico 43
 
28 rodolico 44
our $binDir = '/opt/camp/sysinfo-client';
44 rodolico 45
our $moduleDir = $binDir . '/modules';
46
our $scriptDir = $binDir . '/scripts';
28 rodolico 47
our $confDir = '/etc/camp/sysinfo-client';
21 rodolico 48
 
28 rodolico 49
our $binName = 'sysinfo-client';
50
our $confName = 'sysinfo-client.conf';
21 rodolico 51
 
35 rodolico 52
sub setDryRun {
53
   $dryRun = shift;
54
}
21 rodolico 55
 
28 rodolico 56
our %sendTypes = ( 
21 rodolico 57
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
58
                                      'keys' => 
59
                                      [
60
                                        'mailTo',
61
                                        'mailSubject',
62
                                        'mailCC',
63
                                        'mailBCC',
64
                                        'mailServer',
65
                                        'mailFrom',
66
                                        'logFile',
67
                                        'otherCLParams',
68
                                        'tls',
69
                                        'smtpUser',
70
                                        'smtpPass',
71
                                        'sendEmailScriptLocation'
72
                                      ],
73
                                    },
74
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
75
                                      'keys' => 
76
                                      [
77
                                        'URL',
78
                                        'key for report',
79
                                        'key for date',
80
                                        'key for hostname',
81
                                        'key for client',
82
                                        'key for serial number'
83
                                      ]
84
                                    }
85
                );
86
 
87
 
37 rodolico 88
sub enableModules {
89
   my $moduleDir = shift;
90
   my %modules;
91
   if ( opendir( my $dh, "$moduleDir" ) ) {
92
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
93
                 grep { ! /^\./ } 
94
                 readdir $dh;
95
      closedir( $dh );
96
      foreach my $filename ( keys %modules ) {
97
         if ( open FILE,"<$moduleDir/$_" ) {
98
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
99
            close FILE;
100
            chomp @descript;
101
            $modules{$filename}{'description'} = $descript[0];
102
         } # if
103
      } # foreach
104
   } # if
105
} # enableModules
106
 
21 rodolico 107
sub showConf {
35 rodolico 108
   my $configuration = shift;
109
#   print Dumper( $configuration ); die;
21 rodolico 110
   my $conf;
35 rodolico 111
   $conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
112
   $conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
113
   $conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
114
   $conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
115
   $conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
53 rodolico 116
   $conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
21 rodolico 117
   return $conf;
118
}
119
 
120
sub transportsToConfig {
35 rodolico 121
   my $transports = shift;
21 rodolico 122
   my $config;
123
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
124
      my $name = $$transports{ $priority }{'-name-'};
125
      $config .= "# Tranport $name at priority $priority\n";
126
      my $thisOne = $$transports{ $priority };
127
      foreach my $key ( sort keys %$thisOne ) {
128
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
129
      } # foreach
130
   } # foreach
131
   return $config;
132
} # transportsToConfig
133
 
45 rodolico 134
sub makeConfig {
135
   my @configFileNames = @_;
136
   my %config;
137
   my $clientName;
138
   my $serialNumber;
139
   my $hostname;
140
   my @moduleDirs;
141
   my @scriptDirs;
142
   my $transports = {};
143
 
144
   foreach my $config ( @configFileNames ) {
145
      open CONF,"<$config" or die "could not open $config: $!\n";
146
      my $contents = join( '', <CONF> );
147
      close CONF;
148
      # now, eval the information we just read.
149
      # NOTE: we must turn off strict while doing this, and we die if something breaks.
150
      no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
151
   }
152
   unless ( $hostname ) {
153
      $hostname = `hostname -f`;
154
      chomp $hostname;
155
   }
53 rodolico 156
   unless ( $serialNumber ) {
157
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
158
      chomp $serialNumber;
159
      $serialNumber =~ s/\s//gi;
160
   }
45 rodolico 161
 
162
 
163
   $config{'clientName'} = $clientName;
164
   $config{'serialNumber'} = $serialNumber;
165
   $config{'hostname'} = $hostname;
166
   $config{'moduleDirs'} = [ @moduleDirs ];
167
   $config{'scriptDirs'} = [ @scriptDirs ];
168
   $config{'transports'} = $transports;
169
   return \%config;
170
}
171
 
21 rodolico 172
# prompt the user for a response, then allow them to enter it
173
# the first response is considered the default and is printed
174
# in all caps if more than one exists
175
# first answer is used if they simply press the Enter
176
# key. The response is returned all lower case if more than one
177
# exists.
178
# it is assumed 
179
sub getAnswer {
180
   my ( $prompt, @answers ) = @_;
181
   $answers[0] = '' unless defined( $answers[0] );
182
   my $default = $answers[0];
183
   my $onlyOneAnswer = scalar( @answers ) == 1;
184
   print $prompt . '[ ';
185
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
186
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 187
   my $thisAnswer = <>;
21 rodolico 188
   chomp $thisAnswer;
189
   $thisAnswer = $default unless $thisAnswer;
190
   return $thisAnswer;
191
}
192
 
193
sub yesno {
40 rodolico 194
   my ( $prompt, $default ) = @_;
195
   $default = 'yes' unless $default;
196
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 197
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
198
}
199
 
35 rodolico 200
# runs a system command. Also, if in testing mode, simply shows what
201
# would have been done.
202
sub runCommand {
203
   while ( my $command = shift ) {
204
      if ( $dryRun ) {
205
         print "$command\n";
206
      } else {
207
         `$command`;
208
      }
209
   }
210
   return 1;
211
} # runCommand
212
 
213
# checks if a directory exists and, if not, creates it
214
my %directories; # holds list of directories already created so no need to do an I/O
215
 
216
sub checkDirectoryExists {
217
   my ( $filename,$mod,$owner ) = @_;
218
   $mod = "0700" unless $mod;
219
   $owner = "root:root" unless $owner;
220
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
221
   my ($fn, $dirname) = fileparse( $filename );
222
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
223
   return '' if exists $directories{$dirname};
224
   if ( -d $dirname ) {
225
      $directories{$dirname} = 1;
226
      return '';
227
   }
228
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
229
      $directories{$dirname} = 1;
230
   }
231
   return '';   
232
}
233
 
21 rodolico 234
sub writeConfig {
35 rodolico 235
   my ( $filename,$content ) = @_;
236
   my $path;
237
   ($filename, $path ) = fileparse( $filename );
238
 
21 rodolico 239
   my $return;
240
   `mkdir -p $path` unless -d $path;
241
   $filename = $path . '/' . $filename;
242
   if ( -e $filename ) {
243
      `cp $filename $filename.bak` if ( -e $filename );
244
      $return .= "Old config copied to $filename.bak\n";
245
   }
35 rodolico 246
   if ( $dryRun ) {
247
      $return .= "Not writing to configuration file\n";
248
   } else {
249
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
250
      print CONF $content;
251
      close CONF;
252
      `chmod 600 $filename`;
253
      $return .= "Configuration successfully written to $filename\n";
254
   }
21 rodolico 255
   return $return;
256
}
257
 
258
sub processParameters {
259
   while ( my $parameter = shift ) {
260
      if ( $parameter eq '-v' ) {
26 rodolico 261
         print "$VERSION\n";
21 rodolico 262
         exit;
263
      }
264
   } # while
265
}
266
 
53 rodolico 267
sub findConf {
268
   my $confName = shift;
269
   my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
270
   for ( my $i = 0; $i < @confDirs; $i++ ) {
271
      if ( -d $confDirs[ $i ] ) {
55 rodolico 272
         return ( $confDirs[$i],  $confName );
53 rodolico 273
      }
274
   }
275
}
276
 
21 rodolico 277
 
278
1;