Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 rodolico 1
#!/usr/bin/env perl
2
use warnings;
26 rodolico 3
use strict;  
2 rodolico 4
 
28 rodolico 5
our $VERSION = '1.1';
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
2 rodolico 14
 
15
BEGIN {
16
   push @INC, shift;
17
}
18
 
19
use library;
20
 
21
my $command = &validCommandOnSystem('lspci');
22
exit 1 unless $command;
23
 
24
 
25
my $CATEGORY = 'pci';
26
 
27
my $pciInfo =  qx(lspci -Dvmm);
28
 
29
# this is a regular expression to "find" the slot number, if one exists
30
# Different versions of lspci use different keys for the name and the slot
31
# in some cases, the key Device: is used for both the device name and the slot (Debian Etch lspci version 2.2.4-pre4)
32
# so I have to use this kludge. I may rewrite it to just search the sys directory tree later.
33
my $SLOT_REGEX = '^[0-9a-z]+[:.][0-9a-z]+';
34
my @pciInfo = split ("\n\n", $pciInfo);
35
my $i = 0;
28 rodolico 36
my %returnValue;
37
 
2 rodolico 38
while (my $test = shift (@pciInfo)) {
39
   foreach my $thisLine (sort split("\n", $test)) {
40
      if ($thisLine =~ m/([a-z]+):\s*(\S.*)/i) {
41
         my ($key, $value) = (lc $1,$2);
42
         # remove any leading whitespace
43
         $key =~ s/^\s*//g;
44
         $value =~ s/^\s*//g;
45
         while (defined($returnValue{$i}{$key})) { # dup key, so give it a unique value
46
            $key .= '0'; # just add some 0's at the end
47
         }
48
         $returnValue{$i}{$key} = $value;
49
      }
50
   }
51
   unless (defined $returnValue{$i}{'slot'}) { # no slot number, so see if we have one
52
      $returnValue{$i}{'slot'} = 'Unknown';
53
      for my $thisKey ( keys %{$returnValue{$i}} ) {
54
            if ($returnValue{$i}{$thisKey} =~ m/$SLOT_REGEX/i) {
55
            $returnValue{$i}{'slot'} = $returnValue{$i}{$thisKey}; # this puts it in two places, so remove the original
56
            delete $returnValue{$i}{$thisKey};
57
            last;
58
            }
59
         }
60
   }
61
 
62
   if (defined ($returnValue{$i}{'name'})) { # we need to not have this; it messes up the xml package
63
      $returnValue{$i}{'device name'} = $returnValue{$i}{'name'};
64
      delete $returnValue{$i}{'name'}
65
   }
66
   unless (defined ($returnValue{$i}{'name'})) { # no name, so see if we have one
67
      $returnValue{$i}{'name'} = 'Unknown';
68
      foreach my $thisKey ( 'slot', 'device', 'device0', 'sdevice', 'class', 'vendor', 'svendor' ) {
69
         if (defined($returnValue{$i}{$thisKey}) && ($returnValue{$i} ne 'Unknown') ) {
70
            $returnValue{$i}{'name'} = $returnValue{$i}{$thisKey};
71
            last;
72
         }
73
      }
74
   }
75
   $i++;
76
}
77
 
78
foreach my $key ( keys %returnValue ) {
79
   my $name = $returnValue{$key}{'name'};
80
   my $temp = $returnValue{$key};
81
   foreach my $info ( keys %$temp ) {
82
      print "$CATEGORY\t$name\t$info\t" . $$temp{$info} . "\n" unless $info eq 'name';
83
   }
84
}