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
 
26 rodolico 5
our $VERSION = '1.0';
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;
36
while (my $test = shift (@pciInfo)) {
37
   foreach my $thisLine (sort split("\n", $test)) {
38
      if ($thisLine =~ m/([a-z]+):\s*(\S.*)/i) {
39
         my ($key, $value) = (lc $1,$2);
40
         # remove any leading whitespace
41
         $key =~ s/^\s*//g;
42
         $value =~ s/^\s*//g;
43
         while (defined($returnValue{$i}{$key})) { # dup key, so give it a unique value
44
            $key .= '0'; # just add some 0's at the end
45
         }
46
         $returnValue{$i}{$key} = $value;
47
      }
48
   }
49
   unless (defined $returnValue{$i}{'slot'}) { # no slot number, so see if we have one
50
      $returnValue{$i}{'slot'} = 'Unknown';
51
      for my $thisKey ( keys %{$returnValue{$i}} ) {
52
            if ($returnValue{$i}{$thisKey} =~ m/$SLOT_REGEX/i) {
53
            $returnValue{$i}{'slot'} = $returnValue{$i}{$thisKey}; # this puts it in two places, so remove the original
54
            delete $returnValue{$i}{$thisKey};
55
            last;
56
            }
57
         }
58
   }
59
 
60
   if (defined ($returnValue{$i}{'name'})) { # we need to not have this; it messes up the xml package
61
      $returnValue{$i}{'device name'} = $returnValue{$i}{'name'};
62
      delete $returnValue{$i}{'name'}
63
   }
64
   unless (defined ($returnValue{$i}{'name'})) { # no name, so see if we have one
65
      $returnValue{$i}{'name'} = 'Unknown';
66
      foreach my $thisKey ( 'slot', 'device', 'device0', 'sdevice', 'class', 'vendor', 'svendor' ) {
67
         if (defined($returnValue{$i}{$thisKey}) && ($returnValue{$i} ne 'Unknown') ) {
68
            $returnValue{$i}{'name'} = $returnValue{$i}{$thisKey};
69
            last;
70
         }
71
      }
72
   }
73
   $i++;
74
}
75
 
76
foreach my $key ( keys %returnValue ) {
77
   my $name = $returnValue{$key}{'name'};
78
   my $temp = $returnValue{$key};
79
   foreach my $info ( keys %$temp ) {
80
      print "$CATEGORY\t$name\t$info\t" . $$temp{$info} . "\n" unless $info eq 'name';
81
   }
82
}