Module: Facter::Util::Processor

Defined in:
lib/facter/util/processor.rb

Class Method Summary collapse

Class Method Details

.aix_processor_listObject

aix_processor_list is intended to generate a list of values for the processorX facts. The behavior is as follows from [#11609](projects.puppetlabs.com/issues/11609)

  1. Take a list of all the processor identifiers for the platform, represented as system-native identifiers in strings.

  2. Sort the list

  3. Assign an incrementing from 0 integer index to each identifier.

  4. Store the value of the system identifier in the processorX fact where X is the incrementing index.

Returns an Array, sorted, containing the values for the facts.



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
# File 'lib/facter/util/processor.rb', line 17

def self.aix_processor_list
  return_value = []
  aix_proc_id_list = []

  if output = lsdev then
    output.split("\n").each do |line|
      if match = line.match(/proc\d+/)
        aix_proc_id_list << match[0]
      end
    end
  end

  # Generalized alphanumeric sort to put "proc12" behind "proc4"
  padding = 8
  aix_proc_id_list = aix_proc_id_list.sort do |a,b|
    a,b = [a,b].map do |s|
      s.gsub(/\d+/) { |m| "0"*(padding - m.size) + m }
    end
    a<=>b
  end

  aix_proc_id_list.each do |proc_id|
    if output = lsattr("lsattr -El #{proc_id} -a type")
      if match = output.match(/type\s+([^\s]+)\s+Processor/i)
        return_value << match[1]
      end
    end
  end

  return_value
end

.enum_cpuinfoObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/facter/util/processor.rb', line 214

def self.enum_cpuinfo
  processor_num = -1
  processor_list = []
  cpuinfo = "/proc/cpuinfo"

  if File.exists?(cpuinfo)
    model = Facter.value(:architecture)
    case model
    when "x86_64", "amd64", "i386", "x86", /parisc/, "hppa", "ia64"
      File.readlines(cpuinfo).each do |l|
        if l =~ /processor\s+:\s+(\d+)/
          processor_num = $1.to_i
        elsif l =~ /model name\s+:\s+(.*)\s*$/
          processor_list[processor_num] = $1 unless processor_num == -1
          processor_num = -1
        elsif l =~ /processor\s+(\d+):\s+(.*)/
          processor_num = $1.to_i
          processor_list[processor_num] = $2 unless processor_num == -1
        end
      end

    when "ppc64"
      File.readlines(cpuinfo).each do |l|
        if l =~ /processor\s+:\s+(\d+)/
          processor_num = $1.to_i
        elsif l =~ /cpu\s+:\s+(.*)\s*$/
          processor_list[processor_num] = $1 unless processor_num == -1
          processor_num = -1
        end
      end

    when /arm/
      File.readlines(cpuinfo).each do |l|
        if l =~ /Processor\s+:\s+(.+)/
          processor_num += 1
          processor_list[processor_num] = $1.chomp
        elsif l =~ /processor\s+:\s+(\d+)\s*$/
          proc_num = $1.to_i
          if proc_num != 0
            processor_num += 1
            processor_list[processor_num] = processor_list[processor_num-1]
          end
        end
      end

    when /sparc/
      File.readlines(cpuinfo).each do |l|
        if l =~ /cpu\s+:\s+(.*)\s*$/
          processor_num += 1
          processor_list[processor_num] = $1
        end
      end
    end
  end
  processor_list
end

.enum_kstatObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/facter/util/processor.rb', line 271

def self.enum_kstat
  processor_num = -1
  processor_list = []
  kstat = Facter::Core::Execution.exec('/usr/bin/kstat cpu_info')
  if kstat
    kstat.each_line do |l|
      if l =~ /cpu_info(\d+)/
        processor_num = $1.to_i
      elsif l =~ /brand\s+(.*)\s*$/
        processor_list[processor_num] = $1 unless processor_num == -1
        processor_num = -1
      end
    end
  end
  processor_list
end

.getconf_cpu_chip_type(command = "getconf CPU_CHIP_TYPE") ⇒ Object

getconf_cpu_chip_type delegates directly to Facter::Core::Execution.exec.



210
211
212
# File 'lib/facter/util/processor.rb', line 210

def self.getconf_cpu_chip_type(command="getconf CPU_CHIP_TYPE")
  Facter::Core::Execution.exec(command)
end

.getconf_cpu_version(command = "getconf CPU_VERSION") ⇒ Object

getconf_cpu_version delegates directly to Facter::Core::Execution.exec.



204
205
206
# File 'lib/facter/util/processor.rb', line 204

def self.getconf_cpu_version(command="getconf CPU_VERSION")
  Facter::Core::Execution.exec(command)
end

.hpux_processor_listObject

hpux_processor_list is intended to generate a list of values for the processorX facts.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/facter/util/processor.rb', line 76

def self.hpux_processor_list
  return_value = []
  hpux_proc_id_list = []
  cpu = ""

  ##
  # first, try parsing machinfo output.
  if output = machinfo then
    output.split("\n").each do |line|
      if line.match(/processor model:\s+\d+\s+(.*)/) then
        cpu = $1.to_s
      elsif line.match(/\d+\s+((?:PA-RISC|Intel).*processors.*)/) then
        cpu = $1.to_s
        cpu.sub!(/processors/, "processor")
      elsif line.match(/\s+(Intel.*Processor.*)/) then
        cpu = $1.to_s
      end
    end
  end

  ##
  # if that fails, try looking using model command and cross referencing against
  # sched.models, which could be in three places. This file only catered for
  # PA-RISC. Unfortunately, the file is not up to date sometimes.
  if cpu.empty? then
    m = model
    m.sub!(/\s+$/, "")
    m.sub!(/.*\//, "")
    m.sub!(/.*\s+/, "")

    if sched_models_lines = read_sched_models
      sched_models_lines.each do |l|
        if l.match(m) and l.match(/^\S+\s+\d+\.\d+\s+(\S+)/) then
          cpu = "PA-RISC " + $1.to_s.sub!(/^PA/, "") + " processor"
          break # we assume first match is the only match.
        end
      end
    end
  end

  ##
  # if that also fails, report the CPU version based on unistd.h and chip type based on getconf.
  if cpu.empty? then
    cpu_version = getconf_cpu_version
    cpu_chip_type = getconf_cpu_chip_type
    cpu_string = ""

    if lines = read_unistd_h("/usr/include/sys/unistd.h") then
      lines.each do |l|
        if l.match(/define.*0x#{cpu_version.to_i.to_s(16)}.*\/\*\s+(.*)\s+\*\//) then
          cpu_string = $1.to_s
          break
        end
      end
    end

    if cpu_string.empty? then
      cpu_string = "CPU v" + cpu_version
    end

    cpu = cpu_string + " CHIP TYPE #" + cpu_chip_type
  end

  ##
  # now count (logical) CPUs using ioscan. We set processorX for X in 0..processorcount
  # to cpu as worked out above. HP-UX does not support more than one installed CPU
  # model.
  if output = ioscan then
    output.split("\n").each do |line|
      if line.match(/processor/) then
        hpux_proc_id_list << cpu
      end
    end
  end

  hpux_proc_id_list
end

.ioscan(command = "ioscan -fknCprocessor") ⇒ Object

ioscan delegates directly to Facter::Core::Execution.exec.



198
199
200
# File 'lib/facter/util/processor.rb', line 198

def self.ioscan(command="ioscan -fknCprocessor")
  Facter::Core::Execution.exec(command)
end

.kernel_fact_valueObject

kernel_fact_value is intended to directly delegate to Facter.value(:kernel) to make it easier to stub the kernel fact without affecting the entire system.



69
70
71
# File 'lib/facter/util/processor.rb', line 69

def self.kernel_fact_value
  Facter.value(:kernel)
end

.lsattr(command = "lsattr -El proc0 -a type") ⇒ Object

lsattr is intended to directly delegate to Facter::Core::Execution.exec in an effort to make the processorX facts easier to test. See also the lsdev method.



61
62
63
# File 'lib/facter/util/processor.rb', line 61

def self.lsattr(command="lsattr -El proc0 -a type")
  Facter::Core::Execution.exec(command)
end

.lsdev(command = "lsdev -Cc processor") ⇒ Object

lsdev is intended to directly delegate to Facter::Core::Execution.exec in an effort to make the processorX facts easier to test by stubbing only the behaviors we need to stub to get the output of the system command.



53
54
55
# File 'lib/facter/util/processor.rb', line 53

def self.lsdev(command="lsdev -Cc processor")
  Facter::Core::Execution.exec(command)
end

.machinfo(command = "/usr/contrib/bin/machinfo") ⇒ Object

machinfo delegates directly to Facter::Core::Execution.exec, as with lsdev above.



186
187
188
# File 'lib/facter/util/processor.rb', line 186

def self.machinfo(command="/usr/contrib/bin/machinfo")
  Facter::Core::Execution.exec(command)
end

.model(command = "model") ⇒ Object

model delegates directly to Facter::Core::Execution.exec.



192
193
194
# File 'lib/facter/util/processor.rb', line 192

def self.model(command="model")
  Facter::Core::Execution.exec(command)
end