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