Subversion Repositories camp_sysinfo_client_3

Rev

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