| 20 | rodolico | 1 | #!/usr/bin/env perl
 | 
        
           |  |  | 2 | use warnings;
 | 
        
           | 26 | rodolico | 3 | use strict;  
 | 
        
           | 2 | rodolico | 4 |   | 
        
           | 256 | rodolico | 5 | use version ; our $VERSION = 'v1.2.0';
 | 
        
           | 20 | rodolico | 6 |   | 
        
           | 256 | rodolico | 7 | # # Description: Get packages installed via rpm (RedHat)
 | 
        
           | 20 | rodolico | 8 | # Author: R. W. Rodolico
 | 
        
           |  |  | 9 | # Date:   2016-04-08
 | 
        
           | 256 | rodolico | 10 | #
 | 
        
           | 2 | rodolico | 11 | # gets information on software on systems with rpm on them (Redhat)
 | 
        
           | 256 | rodolico | 12 | #
 | 
        
           |  |  | 13 | # Revision History
 | 
        
           |  |  | 14 | #
 | 
        
           |  |  | 15 | #
 | 
        
           | 2 | rodolico | 16 |   | 
        
           | 256 | rodolico | 17 | # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
 | 
        
           |  |  | 18 | # or, if run interactively, in the parent of the modules
 | 
        
           | 2 | rodolico | 19 | BEGIN {
 | 
        
           | 251 | rodolico | 20 |    use FindBin;
 | 
        
           |  |  | 21 |    use File::Spec;
 | 
        
           | 256 | rodolico | 22 |    # prepend the bin directory and its parent
 | 
        
           |  |  | 23 |    use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
 | 
        
           |  |  | 24 |    eval( 'use library;' );
 | 
        
           |  |  | 25 |    die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
 | 
        
           | 2 | rodolico | 26 | }
 | 
        
           |  |  | 27 |   | 
        
           | 256 | rodolico | 28 | #####
 | 
        
           |  |  | 29 | ##### Change these to match your needs
 | 
        
           |  |  | 30 | #####
 | 
        
           | 2 | rodolico | 31 |   | 
        
           | 256 | rodolico | 32 | # Make this a list of all the modules we are going to use. You can replace undef with the version you need, if you like
 | 
        
           |  |  | 33 | my $modulesList = {
 | 
        
           |  |  | 34 |         'Data::Dumper'     => undef,
 | 
        
           |  |  | 35 |    };
 | 
        
           | 2 | rodolico | 36 |   | 
        
           | 256 | rodolico | 37 | # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
 | 
        
           |  |  | 38 | # the full path (from which or where)
 | 
        
           |  |  | 39 | my $commandsList = {
 | 
        
           |  |  | 40 |         'rpm' => undef,
 | 
        
           |  |  | 41 |    };
 | 
        
           | 2 | rodolico | 42 |   | 
        
           | 256 | rodolico | 43 | # list of operating systems this module can be used on.
 | 
        
           |  |  | 44 | my $osList = {
 | 
        
           |  |  | 45 | #         'mswin32' => undef,
 | 
        
           |  |  | 46 | #         'freebsd' => undef,
 | 
        
           |  |  | 47 |          'linux'   => undef,
 | 
        
           |  |  | 48 |    };
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 | # the category the return data should go into. See sysinfo for a list
 | 
        
           | 2 | rodolico | 51 | my $CATEGORY = 'software';
 | 
        
           |  |  | 52 |   | 
        
           | 256 | rodolico | 53 | #####
 | 
        
           |  |  | 54 | ##### End of required
 | 
        
           |  |  | 55 | #####
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 | # some variables needed for our system
 | 
        
           |  |  | 58 | my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
 | 
        
           |  |  | 59 | my @out; # temporary location for each line of output
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | # Try to load the modules we need. If we can not, then make a list of missing modules for error message.
 | 
        
           |  |  | 62 | for my $module ( keys %$modulesList ) {
 | 
        
           |  |  | 63 |    eval ( "use $module;" );
 | 
        
           |  |  | 64 |    push @out, "$errorPrepend Could not load $module" if $@;
 | 
        
           | 2 | rodolico | 65 | }
 | 
        
           | 256 | rodolico | 66 |   | 
        
           |  |  | 67 | if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
 | 
        
           |  |  | 68 |     push @out, "$errorPrepend Invalid Operating System";
 | 
        
           |  |  | 69 | }
 | 
        
           |  |  | 70 | if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
 | 
        
           |  |  | 71 |    push @out, "$errorPrepend Can not find some commands needed";
 | 
        
           |  |  | 72 | }
 | 
        
           |  |  | 73 | if ( !@out ) { # we made it, we have everything, so do the processing
 | 
        
           |  |  | 74 |    #####
 | 
        
           |  |  | 75 |    ##### Your code starts here. Remember to push all output onto @out
 | 
        
           |  |  | 76 |    #####
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |    my @packageList = split( "\n", qx( rpm -qa --qf "%{NAME}\\t%{VERSION}\\t%{DISTRIBUTION}\\n" ) );
 | 
        
           |  |  | 79 |    chomp @packageList;
 | 
        
           |  |  | 80 |    for ( my $i = 0; $i < @packageList; $i++ ) {
 | 
        
           |  |  | 81 |       my ($package, $version, $distribution)  = split( "\t", $packageList[$i]);
 | 
        
           |  |  | 82 |       push @out, "$CATEGORY\t$package\tversion\t$version\n";
 | 
        
           |  |  | 83 |    }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |    #####
 | 
        
           |  |  | 86 |    ##### Your code ends here.
 | 
        
           |  |  | 87 |    #####
 | 
        
           |  |  | 88 | }
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 | # If we are testing from the command line (caller is undef), print the results for debugging
 | 
        
           |  |  | 91 | print join( "\n", @out ) . "\n" unless caller;
 | 
        
           |  |  | 92 | # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
 | 
        
           |  |  | 93 | my $return = join( "\n", @out );
 | 
        
           |  |  | 94 |   |