├── README └── pci_devices.rb /README: -------------------------------------------------------------------------------- 1 | This script is used to create facter facts based on lspci. Just put it in your puppetmodule/lib/facter directory and alter the code to your specific fact-needs. 2 | 3 | The script only requires and lspci binary and has been tested on Debian Lenny and Squeeze 4 | 5 | This code is provided as-is and is licenced under the GPLv3. 6 | -------------------------------------------------------------------------------- /pci_devices.rb: -------------------------------------------------------------------------------- 1 | # Copyright: Pieter Lexis 2 | 3 | # This program is free software: you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # There are no dependencies needed for this script, except for lspci. 14 | # This script is only tested on Debian (Lenny and Squeeze), if you 15 | # have any improvements, send a pull request, ticket or email. 16 | # The latest version of this script is available on github at 17 | # https://github.com/kumina/fact-pci_devices 18 | 19 | def add_fact(fact, code) 20 | Facter.add(fact) { setcode { code } } 21 | end 22 | 23 | case Facter.value(:operatingsystem) 24 | when /Debian|Ubuntu/i 25 | lspci = "/usr/bin/lspci" 26 | when /RedHat|CentOS|Fedora|Scientific|SLES/i 27 | lspci = "/sbin/lspci" 28 | else 29 | lspci = "" 30 | end 31 | 32 | # We can't do this if we don't know the location of lspci 33 | if !lspci.empty? and FileTest.exists?(lspci) 34 | # Create a hash of ALL PCI devices, the key is the PCI slot ID. 35 | # { SLOT_ID => { ATTRIBUTE => VALUE }, ... } 36 | slot="" 37 | # after the following loop, devices will contain ALL PCI devices and the info returned from lspci 38 | devices = {} 39 | %x{#{lspci} -v -mm -k}.each_line do |line| 40 | if not line =~ /^$/ # We don't need to parse empty lines 41 | splitted = line.split(/\t/) 42 | # lspci has a nice syntax: 43 | # ATTRIBUTE:\tVALUE 44 | # We use this to fill our hash 45 | if splitted[0] =~ /^Slot:$/ 46 | slot=splitted[1].chomp 47 | devices[slot] = {} 48 | else 49 | # The chop is needed to strip the ':' from the string 50 | devices[slot][splitted[0].chop] = splitted[1].chomp 51 | end 52 | end 53 | end 54 | 55 | # To create your own facts, edit the following code: 56 | raid_counter = 0 57 | devices.each_key do |a| 58 | case devices[a].fetch("Class") 59 | when /^RAID/ 60 | add_fact("raidcontroller#{raid_counter}_vendor", "#{devices[a].fetch('Vendor')}") 61 | add_fact("raidcontroller#{raid_counter}_driver", "#{devices[a].fetch('Driver')}") 62 | raid_counter += 1 63 | end 64 | end 65 | end 66 | --------------------------------------------------------------------------------