| 48 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
|
|
3 |
use strict;
|
|
|
4 |
|
| 257 |
rodolico |
5 |
use version ; our $VERSION = 'v1.1.0';
|
| 48 |
rodolico |
6 |
|
| 256 |
rodolico |
7 |
# Gets PCI information on BSD systems
|
| 48 |
rodolico |
8 |
# Author: R. W. Rodolico
|
|
|
9 |
# Date: 2016-04-08
|
| 256 |
rodolico |
10 |
#
|
| 48 |
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
|
|
|
13 |
# into the hash (as done in v2), then print the hash. Unneccessarily wasteful of memory
|
| 256 |
rodolico |
14 |
#
|
|
|
15 |
# Revision History
|
|
|
16 |
#
|
| 257 |
rodolico |
17 |
# 20250403 RWR v1.1.0
|
|
|
18 |
# rewrote to use lspci, available from pkg install pciutils
|
| 256 |
rodolico |
19 |
#
|
| 48 |
rodolico |
20 |
|
| 256 |
rodolico |
21 |
# find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
|
|
|
22 |
# or, if run interactively, in the parent of the modules
|
| 48 |
rodolico |
23 |
BEGIN {
|
| 251 |
rodolico |
24 |
use FindBin;
|
|
|
25 |
use File::Spec;
|
| 256 |
rodolico |
26 |
# prepend the bin directory and its parent
|
|
|
27 |
use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
|
|
|
28 |
eval( 'use library;' );
|
|
|
29 |
die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
|
| 48 |
rodolico |
30 |
}
|
|
|
31 |
|
| 256 |
rodolico |
32 |
#####
|
|
|
33 |
##### Change these to match your needs
|
|
|
34 |
#####
|
| 48 |
rodolico |
35 |
|
| 256 |
rodolico |
36 |
# 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
|
|
|
37 |
my $modulesList = {
|
|
|
38 |
'Data::Dumper' => undef,
|
|
|
39 |
};
|
| 48 |
rodolico |
40 |
|
| 256 |
rodolico |
41 |
# hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
|
|
|
42 |
# the full path (from which or where)
|
|
|
43 |
my $commandsList = {
|
| 257 |
rodolico |
44 |
'lspci' => undef,
|
| 256 |
rodolico |
45 |
};
|
|
|
46 |
|
|
|
47 |
# list of operating systems this module can be used on.
|
|
|
48 |
my $osList = {
|
|
|
49 |
# 'mswin32' => undef,
|
|
|
50 |
'freebsd' => undef,
|
|
|
51 |
# 'linux' => undef,
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
# the category the return data should go into. See sysinfo for a list
|
| 48 |
rodolico |
55 |
my $CATEGORY = 'pci';
|
|
|
56 |
|
| 256 |
rodolico |
57 |
#####
|
|
|
58 |
##### End of required
|
|
|
59 |
#####
|
| 48 |
rodolico |
60 |
|
| 256 |
rodolico |
61 |
# some variables needed for our system
|
|
|
62 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
|
|
63 |
my @out; # temporary location for each line of output
|
| 48 |
rodolico |
64 |
|
| 256 |
rodolico |
65 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
|
|
66 |
for my $module ( keys %$modulesList ) {
|
|
|
67 |
eval ( "use $module;" );
|
|
|
68 |
push @out, "$errorPrepend Could not load $module" if $@;
|
| 48 |
rodolico |
69 |
}
|
|
|
70 |
|
| 256 |
rodolico |
71 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
|
|
72 |
push @out, "$errorPrepend Invalid Operating System";
|
|
|
73 |
}
|
|
|
74 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
|
|
75 |
push @out, "$errorPrepend Can not find some commands needed";
|
|
|
76 |
}
|
|
|
77 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
|
|
78 |
#####
|
|
|
79 |
##### Your code starts here. Remember to push all output onto @out
|
|
|
80 |
#####
|
|
|
81 |
|
| 257 |
rodolico |
82 |
my %pci;
|
|
|
83 |
my @lines = `lspci -v -mm`;
|
|
|
84 |
chomp @lines;
|
|
|
85 |
my $slot;
|
|
|
86 |
my $line = 0;
|
|
|
87 |
while ( $line < @lines ) {
|
|
|
88 |
$line++ if ( $lines[$line] =~ m/^\s*$/ ); # blank line, go to next
|
|
|
89 |
last unless $line < @lines;
|
|
|
90 |
my ($key, @values) = split( ':', $lines[$line] );
|
|
|
91 |
my $value = trim( join( ':', @values ) );
|
|
|
92 |
$slot = $value if ( $key eq 'Slot' );
|
|
|
93 |
$pci{$slot}{$key} = $value;
|
|
|
94 |
$line++;
|
|
|
95 |
}
|
| 48 |
rodolico |
96 |
|
| 257 |
rodolico |
97 |
foreach my $slot ( keys %pci ) {
|
|
|
98 |
foreach my $key ( keys %{ $pci{$slot} } ) {
|
|
|
99 |
push @out, "$CATEGORY\t$slot\t$key\t$pci{$slot}{$key}";
|
| 48 |
rodolico |
100 |
}
|
| 257 |
rodolico |
101 |
}
|
|
|
102 |
|
| 256 |
rodolico |
103 |
|
|
|
104 |
|
|
|
105 |
#####
|
|
|
106 |
##### Your code ends here.
|
|
|
107 |
#####
|
| 48 |
rodolico |
108 |
}
|
| 256 |
rodolico |
109 |
|
|
|
110 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
|
|
111 |
print join( "\n", @out ) . "\n" unless caller;
|
|
|
112 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
|
|
113 |
my $return = join( "\n", @out );
|
|
|
114 |
|