| 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 = '1.2';
  | 
        
        
           | 20 | 
           rodolico | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           # pci information for sysinfo client
  | 
        
        
            | 
            | 
           8 | 
           # Author: R. W. Rodolico
  | 
        
        
            | 
            | 
           9 | 
           # Date:   2016-04-08
  | 
        
        
            | 
            | 
           10 | 
              | 
        
        
           | 2 | 
           rodolico | 
           11 | 
           # gets information on pci information assuming lspci is installed
  | 
        
        
            | 
            | 
           12 | 
           # I really don't remember how I wrote this originally, so I just put everything
  | 
        
        
           | 20 | 
           rodolico | 
           13 | 
           # into the hash (as done in v2), then print the hash. Unneccessarily wasteful of memory
  | 
        
        
           | 256 | 
           rodolico | 
           14 | 
           #
  | 
        
        
            | 
            | 
           15 | 
           # Revision History
  | 
        
        
           | 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 | 
                   'lspci' => undef,
  | 
        
        
            | 
            | 
           41 | 
              };
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           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 = 'pci';
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
           | 256 | 
           rodolico | 
           53 | 
           #####
  | 
        
        
            | 
            | 
           54 | 
           ##### End of required
  | 
        
        
            | 
            | 
           55 | 
           #####
  | 
        
        
           | 2 | 
           rodolico | 
           56 | 
              | 
        
        
           | 256 | 
           rodolico | 
           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
  | 
        
        
           | 28 | 
           rodolico | 
           60 | 
              | 
        
        
           | 256 | 
           rodolico | 
           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 $@;
  | 
        
        
            | 
            | 
           65 | 
           }
  | 
        
        
            | 
            | 
           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 $pciInfo =  qx(lspci -Dvmm);
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
              # this is a regular expression to "find" the slot number, if one exists
  | 
        
        
            | 
            | 
           81 | 
              # Different versions of lspci use different keys for the name and the slot
  | 
        
        
            | 
            | 
           82 | 
              # in some cases, the key Device: is used for both the device name and the slot (Debian Etch lspci version 2.2.4-pre4)
  | 
        
        
            | 
            | 
           83 | 
              # so I have to use this kludge. I may rewrite it to just search the sys directory tree later.
  | 
        
        
            | 
            | 
           84 | 
              my $SLOT_REGEX = '^[0-9a-z]+[:.][0-9a-z]+';
  | 
        
        
            | 
            | 
           85 | 
              my @pciInfo = split ("\n\n", $pciInfo);
  | 
        
        
            | 
            | 
           86 | 
              my $i = 0;
  | 
        
        
            | 
            | 
           87 | 
              my %returnValue;
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
              while (my $test = shift (@pciInfo)) {
  | 
        
        
            | 
            | 
           90 | 
                 foreach my $thisLine (sort split("\n", $test)) {
  | 
        
        
            | 
            | 
           91 | 
                    if ($thisLine =~ m/([a-z]+):\s*(\S.*)/i) {
  | 
        
        
            | 
            | 
           92 | 
                       my ($key, $value) = (lc $1,$2);
  | 
        
        
            | 
            | 
           93 | 
                       # remove any leading whitespace
  | 
        
        
            | 
            | 
           94 | 
                       $key =~ s/^\s*//g;
  | 
        
        
            | 
            | 
           95 | 
                       $value =~ s/^\s*//g;
  | 
        
        
            | 
            | 
           96 | 
                       while (defined($returnValue{$i}{$key})) { # dup key, so give it a unique value
  | 
        
        
            | 
            | 
           97 | 
                          $key .= '0'; # just add some 0's at the end
  | 
        
        
            | 
            | 
           98 | 
                       }
  | 
        
        
            | 
            | 
           99 | 
                       $returnValue{$i}{$key} = $value;
  | 
        
        
           | 2 | 
           rodolico | 
           100 | 
                    }
  | 
        
        
            | 
            | 
           101 | 
                 }
  | 
        
        
           | 256 | 
           rodolico | 
           102 | 
                 unless (defined $returnValue{$i}{'slot'}) { # no slot number, so see if we have one
  | 
        
        
            | 
            | 
           103 | 
                    $returnValue{$i}{'slot'} = 'Unknown';
  | 
        
        
            | 
            | 
           104 | 
                    for my $thisKey ( keys %{$returnValue{$i}} ) {
  | 
        
        
            | 
            | 
           105 | 
                          if ($returnValue{$i}{$thisKey} =~ m/$SLOT_REGEX/i) {
  | 
        
        
            | 
            | 
           106 | 
                          $returnValue{$i}{'slot'} = $returnValue{$i}{$thisKey}; # this puts it in two places, so remove the original
  | 
        
        
            | 
            | 
           107 | 
                          delete $returnValue{$i}{$thisKey};
  | 
        
        
            | 
            | 
           108 | 
                          last;
  | 
        
        
            | 
            | 
           109 | 
                          }
  | 
        
        
           | 2 | 
           rodolico | 
           110 | 
                       }
  | 
        
        
           | 256 | 
           rodolico | 
           111 | 
                 }
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                 if (defined ($returnValue{$i}{'name'})) { # we need to not have this; it messes up the xml package
  | 
        
        
            | 
            | 
           114 | 
                    $returnValue{$i}{'device name'} = $returnValue{$i}{'name'};
  | 
        
        
            | 
            | 
           115 | 
                    delete $returnValue{$i}{'name'}
  | 
        
        
            | 
            | 
           116 | 
                 }
  | 
        
        
            | 
            | 
           117 | 
                 unless (defined ($returnValue{$i}{'name'})) { # no name, so see if we have one
  | 
        
        
            | 
            | 
           118 | 
                    $returnValue{$i}{'name'} = 'Unknown';
  | 
        
        
            | 
            | 
           119 | 
                    foreach my $thisKey ( 'slot', 'device', 'device0', 'sdevice', 'class', 'vendor', 'svendor' ) {
  | 
        
        
            | 
            | 
           120 | 
                       if (defined($returnValue{$i}{$thisKey}) && ($returnValue{$i} ne 'Unknown') ) {
  | 
        
        
            | 
            | 
           121 | 
                          $returnValue{$i}{'name'} = $returnValue{$i}{$thisKey};
  | 
        
        
            | 
            | 
           122 | 
                          last;
  | 
        
        
            | 
            | 
           123 | 
                       }
  | 
        
        
           | 2 | 
           rodolico | 
           124 | 
                    }
  | 
        
        
           | 256 | 
           rodolico | 
           125 | 
                 }
  | 
        
        
            | 
            | 
           126 | 
                 $i++;
  | 
        
        
           | 2 | 
           rodolico | 
           127 | 
              }
  | 
        
        
           | 256 | 
           rodolico | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
              foreach my $key ( keys %returnValue ) {
  | 
        
        
            | 
            | 
           130 | 
                 my $name = $returnValue{$key}{'name'};
  | 
        
        
            | 
            | 
           131 | 
                 my $temp = $returnValue{$key};
  | 
        
        
            | 
            | 
           132 | 
                 foreach my $info ( keys %$temp ) {
  | 
        
        
            | 
            | 
           133 | 
                    push @out, "$CATEGORY\t$name\t$info\t" . $$temp{$info} unless $info eq 'name';
  | 
        
        
           | 2 | 
           rodolico | 
           134 | 
                 }
  | 
        
        
            | 
            | 
           135 | 
              }
  | 
        
        
           | 256 | 
           rodolico | 
           136 | 
              #####
  | 
        
        
            | 
            | 
           137 | 
              ##### Your code ends here.
  | 
        
        
            | 
            | 
           138 | 
              #####
  | 
        
        
           | 2 | 
           rodolico | 
           139 | 
           }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
           | 256 | 
           rodolico | 
           141 | 
           # If we are testing from the command line (caller is undef), print the results for debugging
  | 
        
        
            | 
            | 
           142 | 
           print join( "\n", @out ) . "\n" unless caller;
  | 
        
        
            | 
            | 
           143 | 
           # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
  | 
        
        
            | 
            | 
           144 | 
           my $return = join( "\n", @out );
  | 
        
        
            | 
            | 
           145 | 
              |