#!/usr/bin/env perl use warnings; use strict; use version ; our $VERSION = '1.2'; # pci information for sysinfo client # Author: R. W. Rodolico # Date: 2016-04-08 # gets information on pci information assuming lspci is installed # I really don't remember how I wrote this originally, so I just put everything # into the hash (as done in v2), then print the hash. Unneccessarily wasteful of memory # # Revision History # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script # or, if run interactively, in the parent of the modules BEGIN { use FindBin; use File::Spec; # prepend the bin directory and its parent use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); eval( 'use library;' ); die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@; } ##### ##### Change these to match your needs ##### # 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 my $modulesList = { 'Data::Dumper' => undef, }; # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become # the full path (from which or where) my $commandsList = { 'lspci' => undef, }; # list of operating systems this module can be used on. my $osList = { # 'mswin32' => undef, # 'freebsd' => undef, 'linux' => undef, }; # the category the return data should go into. See sysinfo for a list my $CATEGORY = 'pci'; ##### ##### End of required ##### # some variables needed for our system my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages my @out; # temporary location for each line of output # Try to load the modules we need. If we can not, then make a list of missing modules for error message. for my $module ( keys %$modulesList ) { eval ( "use $module;" ); push @out, "$errorPrepend Could not load $module" if $@; } if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system push @out, "$errorPrepend Invalid Operating System"; } if ( !@out && ! validCommandOnSystem ( $commandsList ) ) { push @out, "$errorPrepend Can not find some commands needed"; } if ( !@out ) { # we made it, we have everything, so do the processing ##### ##### Your code starts here. Remember to push all output onto @out ##### my $pciInfo = qx(lspci -Dvmm); # this is a regular expression to "find" the slot number, if one exists # Different versions of lspci use different keys for the name and the slot # in some cases, the key Device: is used for both the device name and the slot (Debian Etch lspci version 2.2.4-pre4) # so I have to use this kludge. I may rewrite it to just search the sys directory tree later. my $SLOT_REGEX = '^[0-9a-z]+[:.][0-9a-z]+'; my @pciInfo = split ("\n\n", $pciInfo); my $i = 0; my %returnValue; while (my $test = shift (@pciInfo)) { foreach my $thisLine (sort split("\n", $test)) { if ($thisLine =~ m/([a-z]+):\s*(\S.*)/i) { my ($key, $value) = (lc $1,$2); # remove any leading whitespace $key =~ s/^\s*//g; $value =~ s/^\s*//g; while (defined($returnValue{$i}{$key})) { # dup key, so give it a unique value $key .= '0'; # just add some 0's at the end } $returnValue{$i}{$key} = $value; } } unless (defined $returnValue{$i}{'slot'}) { # no slot number, so see if we have one $returnValue{$i}{'slot'} = 'Unknown'; for my $thisKey ( keys %{$returnValue{$i}} ) { if ($returnValue{$i}{$thisKey} =~ m/$SLOT_REGEX/i) { $returnValue{$i}{'slot'} = $returnValue{$i}{$thisKey}; # this puts it in two places, so remove the original delete $returnValue{$i}{$thisKey}; last; } } } if (defined ($returnValue{$i}{'name'})) { # we need to not have this; it messes up the xml package $returnValue{$i}{'device name'} = $returnValue{$i}{'name'}; delete $returnValue{$i}{'name'} } unless (defined ($returnValue{$i}{'name'})) { # no name, so see if we have one $returnValue{$i}{'name'} = 'Unknown'; foreach my $thisKey ( 'slot', 'device', 'device0', 'sdevice', 'class', 'vendor', 'svendor' ) { if (defined($returnValue{$i}{$thisKey}) && ($returnValue{$i} ne 'Unknown') ) { $returnValue{$i}{'name'} = $returnValue{$i}{$thisKey}; last; } } } $i++; } foreach my $key ( keys %returnValue ) { my $name = $returnValue{$key}{'name'}; my $temp = $returnValue{$key}; foreach my $info ( keys %$temp ) { push @out, "$CATEGORY\t$name\t$info\t" . $$temp{$info} unless $info eq 'name'; } } ##### ##### Your code ends here. ##### } # If we are testing from the command line (caller is undef), print the results for debugging print join( "\n", @out ) . "\n" unless caller; # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return my $return = join( "\n", @out );