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
 
3
package sysinfoconf;
4
 
5
use warnings;
6
use Exporter;
7
 
8
our @ISA = qw( Exporter );
9
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
10
                  $transports $sysinfo3 %sendTypes $binDir $modulesDir
11
                  $scriptsDir $confDir $binName $confName
12
                  &showConf &transportsToConfig &getAnswer &yesno  
13
                  &writeConfig &processParameters
14
                );
15
 
16
 
17
$clientName = '';
18
$serialNumber = '';
19
$hostname = '';
20
@moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
21
@scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
22
$transports = {}; # holds transportation mechanisms
23
# the following are keys which are specific to the different transport channels
24
 
25
$sysinfo3 = '/etc/camp/sysinfo-client/sysinfo-client.conf';
26
 
27
$binDir = '/opt/camp/sysinfo-client';
28
$modulesDir = $binDir . '/modules';
29
$scriptsDir = $binDir . '/scripts';
30
$confDir = '/etc/camp/sysinfo-client';
31
 
32
$binName = 'sysinfo-client';
33
$confName = 'sysinfo-client.conf';
34
 
35
 
36
%sendTypes = ( 
37
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
38
                                      'keys' => 
39
                                      [
40
                                        'mailTo',
41
                                        'mailSubject',
42
                                        'mailCC',
43
                                        'mailBCC',
44
                                        'mailServer',
45
                                        'mailFrom',
46
                                        'logFile',
47
                                        'otherCLParams',
48
                                        'tls',
49
                                        'smtpUser',
50
                                        'smtpPass',
51
                                        'sendEmailScriptLocation'
52
                                      ],
53
                                    },
54
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
55
                                      'keys' => 
56
                                      [
57
                                        'URL',
58
                                        'key for report',
59
                                        'key for date',
60
                                        'key for hostname',
61
                                        'key for client',
62
                                        'key for serial number'
63
                                      ]
64
                                    }
65
                );
66
 
67
 
68
sub showConf {
69
   my $conf;
70
   $conf .= "\$clientName = '" . $clientName . "';\n";
71
   $conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
72
   $conf .= "\$hostname = '" . $hostname . "';\n";
73
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
74
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
75
   $conf .= &transportsToConfig();
76
   return $conf;
77
}
78
 
79
sub transportsToConfig {
80
   my $config;
81
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
82
      my $name = $$transports{ $priority }{'-name-'};
83
      $config .= "# Tranport $name at priority $priority\n";
84
      my $thisOne = $$transports{ $priority };
85
      foreach my $key ( sort keys %$thisOne ) {
86
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
87
      } # foreach
88
   } # foreach
89
   return $config;
90
} # transportsToConfig
91
 
92
# prompt the user for a response, then allow them to enter it
93
# the first response is considered the default and is printed
94
# in all caps if more than one exists
95
# first answer is used if they simply press the Enter
96
# key. The response is returned all lower case if more than one
97
# exists.
98
# it is assumed 
99
sub getAnswer {
100
   my ( $prompt, @answers ) = @_;
101
   $answers[0] = '' unless defined( $answers[0] );
102
   my $default = $answers[0];
103
   my $onlyOneAnswer = scalar( @answers ) == 1;
104
   print $prompt . '[ ';
105
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
106
   print join( ' | ', @answers ) . ' ]: ';
107
   $thisAnswer = <>;
108
   chomp $thisAnswer;
109
   $thisAnswer = $default unless $thisAnswer;
110
   return $thisAnswer;
111
}
112
 
113
sub yesno {
114
   my $prompt = shift;
115
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
116
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
117
}
118
 
119
sub writeConfig {
120
   my ($path,$filename,$content) = @_;
121
   my $return;
122
   `mkdir -p $path` unless -d $path;
123
   $filename = $path . '/' . $filename;
124
   if ( -e $filename ) {
125
      `cp $filename $filename.bak` if ( -e $filename );
126
      $return .= "Old config copied to $filename.bak\n";
127
   }
128
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
129
   print CONF $content;
130
   close CONF;
131
   `chmod 600 $filename`;
132
   $return .= "Configuration successfully written to $filename\n";
133
   return $return;
134
}
135
 
136
sub processParameters {
137
   while ( my $parameter = shift ) {
138
      if ( $parameter eq '-v' ) {
139
         print "$main::VERSION\n";
140
         exit;
141
      }
142
   } # while
143
}
144
 
145
 
146
 
147
1;