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