| 11 | rodolico | 1 | #!/usr/bin/env perl
 | 
        
           |  |  | 2 |   | 
        
           | 16 | rodolico | 3 | use warnings;
 | 
        
           | 26 | rodolico | 4 | use strict;
 | 
        
           | 16 | rodolico | 5 |   | 
        
           | 45 | rodolico | 6 | # v1.1.0 20161022 RWR
 | 
        
           |  |  | 7 | # use library sysinfoconf.pm for loading, editing and displaying config
 | 
        
           | 77 | rodolico | 8 | #
 | 
        
           |  |  | 9 | # v1.2.0 20190108 RWR
 | 
        
           |  |  | 10 | # added UUID
 | 
        
           | 129 | rodolico | 11 | #
 | 
        
           |  |  | 12 | # v1.3.0 20191107 RWR
 | 
        
           |  |  | 13 | # changed the screen to allow the user to see most of the values and decide what to edit
 | 
        
           |  |  | 14 | # then write the results.
 | 
        
           |  |  | 15 | # also, parameters are passed that allow the import of a file in old conf format
 | 
        
           | 138 | rodolico | 16 | #
 | 
        
           |  |  | 17 | # v1.3.1 20191112 RWR
 | 
        
           |  |  | 18 | # Fixed showEntry() to set value to empty string if it is null 
 | 
        
           | 20 | rodolico | 19 |   | 
        
           | 129 | rodolico | 20 | use Getopt::Long;
 | 
        
           | 45 | rodolico | 21 |   | 
        
           | 129 | rodolico | 22 |   | 
        
           | 138 | rodolico | 23 | our $VERSION = '1.3.1';
 | 
        
           | 129 | rodolico | 24 |   | 
        
           | 21 | rodolico | 25 | # find our location and use it for searching for libraries
 | 
        
           |  |  | 26 | BEGIN {
 | 
        
           |  |  | 27 |    use FindBin;
 | 
        
           |  |  | 28 |    use File::Spec;
 | 
        
           |  |  | 29 |    use lib File::Spec->catdir($FindBin::Bin);
 | 
        
           | 11 | rodolico | 30 | }
 | 
        
           |  |  | 31 |   | 
        
           | 21 | rodolico | 32 | use sysinfoconf; # a library of shared routines with install.pl
 | 
        
           | 16 | rodolico | 33 |   | 
        
           | 45 | rodolico | 34 | my $config;
 | 
        
           | 16 | rodolico | 35 |   | 
        
           | 12 | rodolico | 36 | sub findSendEmail {
 | 
        
           |  |  | 37 |    my $currentLocation = shift;
 | 
        
           | 53 | rodolico | 38 |    my @possibles = ( 
 | 
        
           |  |  | 39 |             '/usr/local/opt/sendEmail/sendEmail',
 | 
        
           |  |  | 40 |             '/usr/local/opt/sendEmail/sendEmail.pl',
 | 
        
           |  |  | 41 |             '/opt/sendEmail/sendEmail', 
 | 
        
           |  |  | 42 |             '/opt/sendEmail/sendEmail.pl' 
 | 
        
           |  |  | 43 |             );
 | 
        
           | 12 | rodolico | 44 |    return $currentLocation if ( $currentLocation && -x $currentLocation );
 | 
        
           |  |  | 45 |    # well, we didn't find it, so look around some more
 | 
        
           | 104 | rodolico | 46 |    # Look through the possibles
 | 
        
           | 12 | rodolico | 47 |    foreach my $current ( @possibles ) {
 | 
        
           |  |  | 48 |       return $current if -x $current;
 | 
        
           |  |  | 49 |    }
 | 
        
           |  |  | 50 |    if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
 | 
        
           | 28 | rodolico | 51 |       my $path = `perl getSendEmail.pl`;
 | 
        
           | 12 | rodolico | 52 |       chomp $path;
 | 
        
           |  |  | 53 |       return $path;
 | 
        
           |  |  | 54 |    }
 | 
        
           |  |  | 55 |    return '';
 | 
        
           |  |  | 56 | }
 | 
        
           |  |  | 57 |   | 
        
           | 11 | rodolico | 58 | sub userConfirmation {
 | 
        
           | 45 | rodolico | 59 |    my $config = shift;
 | 
        
           | 11 | rodolico | 60 |   | 
        
           |  |  | 61 | }
 | 
        
           |  |  | 62 |   | 
        
           | 16 | rodolico | 63 | sub setUpTransport {
 | 
        
           |  |  | 64 |    my ($priority, $transport, $fields, $type ) = @_;
 | 
        
           |  |  | 65 |    $priority = getAnswer( 'Priority', $priority );
 | 
        
           |  |  | 66 |    $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
 | 
        
           | 108 | rodolico | 67 |    $$transport{'name'} = $type unless $$transport{'name'};
 | 
        
           | 16 | rodolico | 68 |    my $allKeys = $$fields{'keys'};
 | 
        
           |  |  | 69 |    foreach my $key ( @$allKeys ) {
 | 
        
           |  |  | 70 |       if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
 | 
        
           | 28 | rodolico | 71 |          my $temp = &findSendEmail( $$transport{$key} );
 | 
        
           | 16 | rodolico | 72 |          $$transport{$key} = $temp if $temp;
 | 
        
           |  |  | 73 |       }
 | 
        
           |  |  | 74 |       $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
 | 
        
           |  |  | 75 |    }
 | 
        
           |  |  | 76 |    return ( $priority, $transport );
 | 
        
           |  |  | 77 | }
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | sub showAllTransports {
 | 
        
           |  |  | 80 |    foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
 | 
        
           |  |  | 81 |       print '='x40 . "\n";
 | 
        
           |  |  | 82 |       &showTransports( $priority, $$transports{ $priority } );
 | 
        
           |  |  | 83 |       print '='x40 . "\n";
 | 
        
           |  |  | 84 |    }
 | 
        
           |  |  | 85 | }   
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 | sub showTransports {
 | 
        
           |  |  | 88 |    my ( $priority,$thisOne )  = @_;
 | 
        
           | 108 | rodolico | 89 |    print $$thisOne{'name'} . " has priority $priority\n";
 | 
        
           | 16 | rodolico | 90 |    foreach my $key ( sort keys %$thisOne ) {
 | 
        
           |  |  | 91 |       next if $key =~ m/^-.*-$/;
 | 
        
           |  |  | 92 |       print "$key = $$thisOne{$key}\n";
 | 
        
           |  |  | 93 |    }
 | 
        
           |  |  | 94 | }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | sub doTransports {
 | 
        
           |  |  | 97 |    my ( $transports ) = @_;
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |    foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
 | 
        
           | 108 | rodolico | 100 |       if ( &yesno( $$transports{$priority}{'name'} . " has a priority of $priority, edit it?") ) {
 | 
        
           |  |  | 101 |          #print Dumper( $sendTypes{$$transports{$priority}{'name'}} );
 | 
        
           | 16 | rodolico | 102 |          #die;
 | 
        
           | 108 | rodolico | 103 |          my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'name'}} );
 | 
        
           | 16 | rodolico | 104 |          if ( $newpriority != $priority ) {
 | 
        
           |  |  | 105 |             delete $$transports{$priority};
 | 
        
           |  |  | 106 |             $priority = $newpriority;
 | 
        
           |  |  | 107 |          }
 | 
        
           |  |  | 108 |          $$transports{$priority} = $temp;
 | 
        
           |  |  | 109 |       } # if
 | 
        
           |  |  | 110 |    }
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |    foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
 | 
        
           |  |  | 113 |       print '='x40 . "\n";
 | 
        
           |  |  | 114 |       &showTransports( $priority, $$transports{ $priority } );
 | 
        
           |  |  | 115 |       print '='x40 . "\n";
 | 
        
           |  |  | 116 |    }
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 |    while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
 | 
        
           | 28 | rodolico | 119 |       my $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
 | 
        
           | 16 | rodolico | 120 |       my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
 | 
        
           |  |  | 121 |       $$transports{$priority} = $temp;
 | 
        
           |  |  | 122 |    }
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |    foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
 | 
        
           |  |  | 125 |       print '='x40 . "\n";
 | 
        
           |  |  | 126 |       &showTransports( $priority, $$transports{ $priority } );
 | 
        
           |  |  | 127 |       print '='x40 . "\n";
 | 
        
           |  |  | 128 |    }
 | 
        
           |  |  | 129 | }
 | 
        
           |  |  | 130 |   | 
        
           | 129 | rodolico | 131 | sub showEntry {
 | 
        
           |  |  | 132 |    my ( $lineno, $prompt, $value ) = @_;
 | 
        
           |  |  | 133 |    $value = '' unless $value;
 | 
        
           |  |  | 134 |    return "$lineno. $prompt: $value\n";
 | 
        
           |  |  | 135 | }
 | 
        
           | 16 | rodolico | 136 |   | 
        
           | 129 | rodolico | 137 | sub displayEditScreen {
 | 
        
           |  |  | 138 |    my $config = shift;
 | 
        
           |  |  | 139 |    #print "\033[2J";    #clear the screen
 | 
        
           |  |  | 140 |    #print "\033[0;0H"; #jump to 0,0   
 | 
        
           |  |  | 141 |    print &showEntry( 1, 'Client Name', $config->{'clientName'} );
 | 
        
           |  |  | 142 |    print &showEntry( 2, 'Host Name', $config->{'hostname'} );
 | 
        
           |  |  | 143 |    print &showEntry( 3, 'Serial Number', $config->{'serialNumber'} );
 | 
        
           |  |  | 144 |    print &showEntry( 4, 'UUID', $config->{'UUID'} );
 | 
        
           |  |  | 145 |    print &showEntry( 5, 'Module Dirs', join( ',', $config->{'moduleDirs'} ?  @{ $config->{'moduleDirs'} } : () ) );
 | 
        
           |  |  | 146 |    print &showEntry( 6, 'Script Dirs', join( ',', $config->{'scriptDirs'} ?  @{ $config->{'scriptDirs'} } : () ) );
 | 
        
           |  |  | 147 |    print &showEntry( 7, 'Transports', '' );
 | 
        
           |  |  | 148 |    foreach my $key ( sort { $a <=> $b } keys %{ $config->{'transports'} } ) {
 | 
        
           |  |  | 149 |       print "\t$key\t" . $config->{'transports'}->{$key}->{'name'} . "\n";
 | 
        
           |  |  | 150 |    }
 | 
        
           |  |  | 151 |    print "Select Option to Edit, or 'Done' if finished [Done]: ";
 | 
        
           |  |  | 152 |    my $selection = <>;
 | 
        
           |  |  | 153 |    chomp $selection;
 | 
        
           |  |  | 154 |    $selection = 'd' unless $selection;
 | 
        
           |  |  | 155 |    return lc substr( $selection, 0, 1 );
 | 
        
           |  |  | 156 | }
 | 
        
           | 45 | rodolico | 157 |   | 
        
           | 129 | rodolico | 158 | sub getValue {
 | 
        
           |  |  | 159 |    my ( $prompt, $value ) = @_;
 | 
        
           | 138 | rodolico | 160 |    $value = '' unless $value;
 | 
        
           | 129 | rodolico | 161 |    print "$prompt [$value]: ";
 | 
        
           |  |  | 162 |    my $t = <>;
 | 
        
           |  |  | 163 |    chomp $t;
 | 
        
           |  |  | 164 |    return $t ? $t : $value;
 | 
        
           |  |  | 165 | }
 | 
        
           | 104 | rodolico | 166 |   | 
        
           | 129 | rodolico | 167 | sub editSelection {
 | 
        
           |  |  | 168 |    my $selection = shift;
 | 
        
           |  |  | 169 |    if ( $selection == 1 ) {
 | 
        
           |  |  | 170 |       $config->{'clientName'} = &getValue( 'Client Name', $config->{'clientName'} );
 | 
        
           |  |  | 171 |    } elsif ( $selection == 2 ) {
 | 
        
           |  |  | 172 |       $config->{'hostname'} = &getValue( 'Host Name', $config->{'hostname'} );
 | 
        
           |  |  | 173 |    } elsif ( $selection == 3 ) {
 | 
        
           |  |  | 174 |       $config->{'serialNumber'} = &getValue( 'Serial Number', $config->{'serialNumber'} );
 | 
        
           |  |  | 175 |    } elsif ( $selection == 4 ) {
 | 
        
           |  |  | 176 |       $config->{'UUID'} = &getValue( 'UUID', $config->{'UUID'} );
 | 
        
           |  |  | 177 |    } elsif ( $selection == 5 ) {
 | 
        
           |  |  | 178 |       my $t = join( ',', $config->{'moduleDirs'} ?  @{ $config->{'moduleDirs'} } : () );
 | 
        
           |  |  | 179 |       $t = &getValue( "Comma separated list of Module Directories\n\t", $t );
 | 
        
           |  |  | 180 |       my @t = split( ',', $t );
 | 
        
           |  |  | 181 |       $config->{'moduleDirs'} =  \@t;
 | 
        
           |  |  | 182 |    } elsif ( $selection == 6 ) {
 | 
        
           |  |  | 183 |       my $t = join( ',', $config->{'scriptDirs'} ?  @{ $config->{'scriptDirs'} } : () );
 | 
        
           |  |  | 184 |       $t = &getValue( "Comma separated list of Script Directories\n\t", $t );
 | 
        
           |  |  | 185 |       my @t = split( ',', $t );
 | 
        
           |  |  | 186 |       $config->{'scriptDirs'} =  \@t;
 | 
        
           |  |  | 187 |    } elsif ( $selection == 7 ) {
 | 
        
           |  |  | 188 |       print "Can not automatically do this at this time\nManually edit the file (press enter to continue)";
 | 
        
           |  |  | 189 |       my $t = <>;
 | 
        
           |  |  | 190 |    }
 | 
        
           |  |  | 191 | }
 | 
        
           |  |  | 192 |   | 
        
           | 132 | rodolico | 193 | sub help {
 | 
        
           |  |  | 194 |    print "$0 version $VERSION\n";
 | 
        
           |  |  | 195 |    print "Reads one or more sysinfo configuration file and\nallows user to edit the values\n\n";
 | 
        
           |  |  | 196 |    print "$0 --verbose --file=/full/paht/to/input -output=/full/path/to/sysinfo-client.yaml\n";
 | 
        
           |  |  | 197 |    print "   --verbose may be entered more than one time to increase verbosity\n";
 | 
        
           |  |  | 198 |    print "   --file may be entered multiple times and may contain a comma delimited group of files\n";
 | 
        
           |  |  | 199 |    print "   --output is the file which will be written to\n\n";
 | 
        
           |  |  | 200 |    print "All parameters are optional\n";
 | 
        
           |  |  | 201 |    print "Will search common locations for sysinfo-client.yaml and, if found, append this to the end\n";
 | 
        
           |  |  | 202 |    print "of the input file list.\n";
 | 
        
           |  |  | 203 |    print "If no --output is passed, will write to the last --file passed or the file which was found\n";
 | 
        
           |  |  | 204 |    print "in the search\n";
 | 
        
           |  |  | 205 |    print "All parameters accept the short flag, ie -v, -f, -o and -h\n";
 | 
        
           |  |  | 206 |    exit;
 | 
        
           |  |  | 207 | }
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 |   | 
        
           | 129 | rodolico | 210 | my @confFileName;
 | 
        
           | 132 | rodolico | 211 | my $outputFile;
 | 
        
           | 129 | rodolico | 212 | my $verbose;
 | 
        
           |  |  | 213 | my $help;
 | 
        
           |  |  | 214 | my $version;
 | 
        
           |  |  | 215 |   | 
        
           |  |  | 216 | # handle any command line parameters that may have been passed in
 | 
        
           |  |  | 217 | # verbose is incremental, so for example, -vv would give more info than -v
 | 
        
           |  |  | 218 | # there can be multiple files passed, and an entry can have a comma
 | 
        
           |  |  | 219 | # separated list. The files will be processed in order, with the last
 | 
        
           |  |  | 220 | # one passed in being the one that overrides everything.
 | 
        
           |  |  | 221 | GetOptions (
 | 
        
           | 132 | rodolico | 222 |             "verbose|v+"  => \$verbose, # verbosity level, 0-9
 | 
        
           |  |  | 223 |             'file|f=s'    => \@confFileName, # array of files to read
 | 
        
           |  |  | 224 |             'output|o=s'  => \$outputFile, # file to save to
 | 
        
           | 129 | rodolico | 225 |             'help|h'      => \$help
 | 
        
           |  |  | 226 |             ) or die "Error parsing command line\n";
 | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 | if ( $help ) { &help() ; exit; }
 | 
        
           |  |  | 229 |   | 
        
           |  |  | 230 | # if they passed something like -f file1,file2,file3, parse it out
 | 
        
           |  |  | 231 | @confFileName = split(/,/,join(',',@confFileName));
 | 
        
           |  |  | 232 |   | 
        
           |  |  | 233 | # look for the standard configuration file in the standard place
 | 
        
           | 103 | rodolico | 234 | ( $confDir, $sysinfo3 ) = &findConf( $sysinfo3 );
 | 
        
           | 129 | rodolico | 235 | # add it at the tail end of the list
 | 
        
           |  |  | 236 | push @confFileName, $confDir . "/" . $sysinfo3 if ( $confDir );
 | 
        
           | 132 | rodolico | 237 | $outputFile = $confFileName[-1] unless $outputFile;
 | 
        
           | 53 | rodolico | 238 |   | 
        
           | 129 | rodolico | 239 | print "Loading defaults from existing configs\n";
 | 
        
           |  |  | 240 | $config = &makeConfig( @confFileName  );
 | 
        
           | 104 | rodolico | 241 |   | 
        
           | 129 | rodolico | 242 | my $selection;
 | 
        
           |  |  | 243 | while ( ( $selection = &displayEditScreen( $config ) ) ne 'd' ) {
 | 
        
           |  |  | 244 |    &editSelection( $selection );
 | 
        
           | 11 | rodolico | 245 | }
 | 
        
           |  |  | 246 |   | 
        
           | 132 | rodolico | 247 | if ( &yesno( "Ready to write configuration to $outputFile, ok?" ) ) {
 | 
        
           |  |  | 248 |    &writeConfig( $outputFile, &showConf( $config ) );
 | 
        
           |  |  | 249 | }
 | 
        
           | 20 | rodolico | 250 |   | 
        
           | 45 | rodolico | 251 | #use Data::Dumper;
 | 
        
           |  |  | 252 | #print Dumper( $config ); die;
 | 
        
           | 16 | rodolico | 253 |   | 
        
           | 132 | rodolico | 254 | #&doTransports( $$config{'transports'} );
 | 
        
           | 11 | rodolico | 255 |   | 
        
           |  |  | 256 |   | 
        
           |  |  | 257 | 1;
 |