Subversion Repositories camp_sysinfo_client_3

Rev

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