Subversion Repositories camp_sysinfo_client_3

Rev

Rev 78 | Rev 81 | 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";
78 rodolico 117
   $conf .= "\$UUID = '" .      ( defined( $$configuration{'UUID'}     ) ? $$configuration{'UUID'}     : '' ) . "';\n";
35 rodolico 118
   $conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
119
   $conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
53 rodolico 120
   $conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
21 rodolico 121
   return $conf;
122
}
123
 
124
sub transportsToConfig {
35 rodolico 125
   my $transports = shift;
21 rodolico 126
   my $config;
127
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
128
      my $name = $$transports{ $priority }{'-name-'};
129
      $config .= "# Tranport $name at priority $priority\n";
130
      my $thisOne = $$transports{ $priority };
131
      foreach my $key ( sort keys %$thisOne ) {
132
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
133
      } # foreach
134
   } # foreach
135
   return $config;
136
} # transportsToConfig
137
 
45 rodolico 138
sub makeConfig {
139
   my @configFileNames = @_;
140
   my %config;
77 rodolico 141
   my $clientName = '';
142
   my $serialNumber = '';
143
   my $hostname = '';
80 rodolico 144
   my @moduleDirs;
145
   my @scriptDirs;
77 rodolico 146
   my $UUID = '';
45 rodolico 147
   my $transports = {};
148
 
149
   foreach my $config ( @configFileNames ) {
150
      open CONF,"<$config" or die "could not open $config: $!\n";
151
      my $contents = join( '', <CONF> );
152
      close CONF;
153
      # now, eval the information we just read.
154
      # NOTE: we must turn off strict while doing this, and we die if something breaks.
155
      no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
156
   }
157
   unless ( $hostname ) {
158
      $hostname = `hostname -f`;
159
      chomp $hostname;
160
   }
53 rodolico 161
   unless ( $serialNumber ) {
162
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
163
      chomp $serialNumber;
164
      $serialNumber =~ s/\s//gi;
165
   }
77 rodolico 166
   unless ( $UUID ) {
167
      $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
168
      $UUID =~ s/\s//gi;
169
   }
45 rodolico 170
 
171
   $config{'clientName'} = $clientName;
172
   $config{'serialNumber'} = $serialNumber;
77 rodolico 173
   $config{'UUID'} = $UUID;
45 rodolico 174
   $config{'hostname'} = $hostname;
175
   $config{'moduleDirs'} = [ @moduleDirs ];
176
   $config{'scriptDirs'} = [ @scriptDirs ];
177
   $config{'transports'} = $transports;
178
   return \%config;
179
}
180
 
21 rodolico 181
# prompt the user for a response, then allow them to enter it
182
# the first response is considered the default and is printed
183
# in all caps if more than one exists
184
# first answer is used if they simply press the Enter
185
# key. The response is returned all lower case if more than one
186
# exists.
187
# it is assumed 
188
sub getAnswer {
189
   my ( $prompt, @answers ) = @_;
190
   $answers[0] = '' unless defined( $answers[0] );
191
   my $default = $answers[0];
192
   my $onlyOneAnswer = scalar( @answers ) == 1;
193
   print $prompt . '[ ';
194
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
195
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 196
   my $thisAnswer = <>;
21 rodolico 197
   chomp $thisAnswer;
198
   $thisAnswer = $default unless $thisAnswer;
199
   return $thisAnswer;
200
}
201
 
202
sub yesno {
40 rodolico 203
   my ( $prompt, $default ) = @_;
204
   $default = 'yes' unless $default;
205
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 206
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
207
}
208
 
35 rodolico 209
# runs a system command. Also, if in testing mode, simply shows what
210
# would have been done.
211
sub runCommand {
212
   while ( my $command = shift ) {
213
      if ( $dryRun ) {
214
         print "$command\n";
215
      } else {
216
         `$command`;
217
      }
218
   }
219
   return 1;
220
} # runCommand
221
 
222
# checks if a directory exists and, if not, creates it
223
my %directories; # holds list of directories already created so no need to do an I/O
224
 
225
sub checkDirectoryExists {
226
   my ( $filename,$mod,$owner ) = @_;
227
   $mod = "0700" unless $mod;
228
   $owner = "root:root" unless $owner;
229
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
230
   my ($fn, $dirname) = fileparse( $filename );
231
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
232
   return '' if exists $directories{$dirname};
233
   if ( -d $dirname ) {
234
      $directories{$dirname} = 1;
235
      return '';
236
   }
237
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
238
      $directories{$dirname} = 1;
239
   }
240
   return '';   
241
}
242
 
21 rodolico 243
sub writeConfig {
35 rodolico 244
   my ( $filename,$content ) = @_;
245
   my $path;
246
   ($filename, $path ) = fileparse( $filename );
247
 
21 rodolico 248
   my $return;
249
   `mkdir -p $path` unless -d $path;
250
   $filename = $path . '/' . $filename;
251
   if ( -e $filename ) {
252
      `cp $filename $filename.bak` if ( -e $filename );
253
      $return .= "Old config copied to $filename.bak\n";
254
   }
35 rodolico 255
   if ( $dryRun ) {
256
      $return .= "Not writing to configuration file\n";
257
   } else {
258
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
259
      print CONF $content;
260
      close CONF;
261
      `chmod 600 $filename`;
262
      $return .= "Configuration successfully written to $filename\n";
263
   }
21 rodolico 264
   return $return;
265
}
266
 
267
sub processParameters {
268
   while ( my $parameter = shift ) {
269
      if ( $parameter eq '-v' ) {
26 rodolico 270
         print "$VERSION\n";
21 rodolico 271
         exit;
272
      }
273
   } # while
274
}
275
 
53 rodolico 276
sub findConf {
277
   my $confName = shift;
278
   my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
279
   for ( my $i = 0; $i < @confDirs; $i++ ) {
280
      if ( -d $confDirs[ $i ] ) {
55 rodolico 281
         return ( $confDirs[$i],  $confName );
53 rodolico 282
      }
283
   }
284
}
285
 
21 rodolico 286
 
287
1;