Subversion Repositories camp_sysinfo_client_3

Rev

Rev 11 | Rev 26 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/env perl
use warnings;

$main::VERSION = '1.0';

# 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

BEGIN {
   push @INC, shift;
}

use library;

my $command = &validCommandOnSystem('lspci');
exit 1 unless $command;


my $CATEGORY = 'pci';

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;
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 ) {
      print "$CATEGORY\t$name\t$info\t" . $$temp{$info} . "\n" unless $info eq 'name';
   }
}