summaryrefslogtreecommitdiff
path: root/lib/ohai/plugins/linux/lspci.rb
blob: f75a0f865da6bc76178e1a5cd12a216ca55b27ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
#
# Author:: Joerg Herzinger <joerg.herzinger@oiml.at>
# Author:: Phil Dibowitz <phil@ipom.com>
# Copyright:: Copyright (c) 2011 GLOBAL 2000/Friends of the Earth Austria
# Copyright:: Copyright (c) 2017 Facebook, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Ohai.plugin(:Lspci) do
  provides "pci"
  optional true

  collect_data(:linux) do
    devices = Mash.new

    h = /[0-9a-fA-F]/ # any hex digit
    hh = /#{h}#{h}/ # any 2 hex digits
    hhhh = /#{h}#{h}#{h}#{h}/ # any 4 hex digits

    d_id = "" # This identifies our pci devices

    def standard_form(devices, d_id, hhhh, tag, line)
      tmp = line.scan(/(.*)\s\[(#{hhhh})\]/)[0]
      devices[d_id]["#{tag}_name"] = tmp[0]
      devices[d_id]["#{tag}_id"] = tmp[1]
    end

    def standard_array(devices, d_id, tag, line)
      if !devices[d_id][tag].is_a?(Array)
        devices[d_id][tag] = [line]
      else
        devices[d_id][tag].push(line)
      end
    end

    shell_out("lspci -vnnmk").stdout.split("\n").each do |line|
      dev = line.scan(/^(.*):\s(.*)$/)[0]
      next if dev.nil?

      case dev[0]
      when "Device" # There are two different Device tags
        if ( tmp = dev[1].match(/(#{hh}:#{hh}.#{h})/) )
          # We have a device id
          d_id = tmp[0] # From now on we will need this id
          devices[d_id] = Mash.new
        else
          standard_form(devices, d_id, hhhh, "device", dev[1])
        end
      when "Class"
        standard_form(devices, d_id, hhhh, "class", dev[1])
      when "Vendor"
        standard_form(devices, d_id, hhhh, "vendor", dev[1])
      when "Driver"
        standard_array(devices, d_id, "driver", dev[1])
      when "Module"
        standard_array(devices, d_id, "module", dev[1])
      when "SDevice"
        standard_form(devices, d_id, hhhh, "sdevice", dev[1])
      end
    end

    pci devices
  end
end