]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Update git URLs for new repository locations
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook Name:: hardware
3 # Recipe:: default
4 #
5 # Copyright 2012, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "tools"
21 include_recipe "munin"
22
23 ohai_plugin "hardware" do
24   template "ohai.rb.erb"
25 end
26
27 case node[:cpu][:"0"][:vendor_id]
28 when "GenuineIntel"
29   package "intel-microcode"
30 when "AuthenticAMD"
31   package "amd64-microcode"
32 end
33
34 if node[:dmi] && node[:dmi][:system]
35   case node[:dmi][:system][:manufacturer]
36   when "empty"
37     manufacturer = node[:dmi][:base_board][:manufacturer]
38     product = node[:dmi][:base_board][:product_name]
39   else
40     manufacturer = node[:dmi][:system][:manufacturer]
41     product = node[:dmi][:system][:product_name]
42   end
43 else
44   manufacturer = "Unknown"
45   product = "Unknown"
46 end
47
48 units = []
49
50 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
51   units << "0"
52 end
53
54 case manufacturer
55 when "HP"
56   package "hponcfg"
57
58   package "hp-health" do
59     action :install
60     notifies :restart, "service[hp-health]"
61   end
62
63   service "hp-health" do
64     action [:enable, :start]
65     supports :status => true, :restart => true
66   end
67
68   units << "1"
69 when "TYAN"
70   units << "0"
71 when "TYAN Computer Corporation"
72   units << "0"
73 when "Supermicro"
74   case product
75   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-2028U-TN24R4T+", "Super Server"
76     units << "1"
77   else
78     units << "0"
79   end
80 when "IBM"
81   units << "0"
82 end
83
84 # Remove legacy HP G4 support which breaks modern hp-health 10.4
85 if manufacturer == "HP"
86   %w[/opt/hp/hp-health/bin/hpasmd /usr/lib/libhpasmintrfc.so.3.0 /usr/lib/libhpasmintrfc.so.3 /usr/lib/libhpasmintrfc.so].each do |filename|
87     file filename do
88       action :delete
89       manage_symlink_source false
90     end
91   end
92
93   directory "/opt/hp/hp-legacy" do
94     action :delete
95     recursive true
96   end
97 end
98
99 units.sort.uniq.each do |unit|
100   if node[:lsb][:release].to_f >= 16.04
101     service "serial-getty@ttyS#{unit}" do
102       action [:enable, :start]
103     end
104   else
105     file "/etc/init/ttySttyS#{unit}.conf" do
106       action :delete
107     end
108
109     template "/etc/init/ttyS#{unit}.conf" do
110       source "tty.conf.erb"
111       owner "root"
112       group "root"
113       mode 0o644
114       variables :unit => unit
115     end
116
117     service "ttyS#{unit}" do
118       provider Chef::Provider::Service::Upstart
119       action [:enable, :start]
120       supports :status => true, :restart => true, :reload => false
121       subscribes :restart, "template[/etc/init/ttyS#{unit}.conf]"
122     end
123   end
124 end
125
126 # if we need a different / special kernel version to make the hardware
127 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
128 # ensure that we have the package installed. the grub template will
129 # make sure that this is the default on boot.
130 if node[:hardware][:grub][:kernel]
131   kernel_version = node[:hardware][:grub][:kernel]
132
133   package "linux-image-#{kernel_version}-generic"
134   package "linux-image-extra-#{kernel_version}-generic"
135   package "linux-headers-#{kernel_version}-generic"
136
137   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
138   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
139   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
140 else
141   grub_entry = "0"
142 end
143
144 if File.exist?("/etc/default/grub")
145   execute "update-grub" do
146     action :nothing
147     command "/usr/sbin/update-grub"
148   end
149
150   template "/etc/default/grub" do
151     source "grub.erb"
152     owner "root"
153     group "root"
154     mode 0o644
155     variables :units => units, :entry => grub_entry
156     notifies :run, "execute[update-grub]"
157   end
158 end
159
160 execute "update-initramfs" do
161   action :nothing
162   command "update-initramfs -u -k all"
163   user "root"
164   group "root"
165 end
166
167 template "/etc/initramfs-tools/conf.d/mdadm" do
168   source "initramfs-mdadm.erb"
169   owner "root"
170   group "root"
171   mode 0o644
172   notifies :run, "execute[update-initramfs]"
173 end
174
175 package "haveged"
176 service "haveged" do
177   action [:enable, :start]
178 end
179
180 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
181
182 package "irqbalance"
183
184 template "/etc/default/irqbalance" do
185   source "irqbalance.erb"
186   owner "root"
187   group "root"
188   mode 0o644
189 end
190
191 service "irqbalance" do
192   action [:start, :enable]
193   supports :status => false, :restart => true, :reload => false
194   subscribes :restart, "template[/etc/default/irqbalance]"
195 end
196
197 # Link Layer Discovery Protocol Daemon
198 package "lldpd"
199 service "lldpd" do
200   action [:start, :enable]
201   supports :status => true, :restart => true, :reload => true
202 end
203
204 if node[:hardware][:mcelog][:enabled]
205   package "mcelog"
206
207   %w[bus cache dimm iomca page socket-memory unknown].each do |trigger|
208     template "/etc/mcelog/#{trigger}-error-trigger.local" do
209       source "mcelog-trigger.erb"
210       owner "root"
211       group "root"
212       mode 0o755
213     end
214   end
215
216   service "mcelog" do
217     action [:start, :enable]
218     supports :status => true, :restart => true, :reload => false
219   end
220 end
221
222 tools_packages = []
223 status_packages = {}
224
225 node[:kernel][:modules].each_key do |modname|
226   case modname
227   when "cciss"
228     tools_packages << "ssacli"
229     status_packages["cciss-vol-status"] ||= []
230   when "hpsa"
231     tools_packages << "ssacli"
232     status_packages["cciss-vol-status"] ||= []
233   when "mptsas"
234     tools_packages << "lsiutil"
235     status_packages["mpt-status"] ||= []
236   when "mpt2sas", "mpt3sas"
237     tools_packages << "sas2ircu"
238     status_packages["sas2ircu-status"] ||= []
239   when "megaraid_mm"
240     tools_packages << "megactl"
241     status_packages["megaraid-status"] ||= []
242   when "megaraid_sas"
243     tools_packages << "megacli"
244     status_packages["megaclisas-status"] ||= []
245   when "aacraid"
246     tools_packages << "arcconf"
247     status_packages["aacraid-status"] ||= []
248   when "arcmsr"
249     tools_packages << "areca"
250   end
251 end
252
253 node[:block_device].each do |name, attributes|
254   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
255
256   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
257     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
258   else
259     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
260       status_packages["cciss-vol-status"] |= [File.basename(sg)]
261     end
262   end
263 end
264
265 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
266   if tools_packages.include?(tools_package)
267     package tools_package
268   else
269     package tools_package do
270       action :purge
271     end
272   end
273 end
274
275 if tools_packages.include?("areca")
276   include_recipe "git"
277
278   git "/opt/areca" do
279     action :sync
280     repository "https://git.openstreetmap.org/private/areca.git"
281     user "root"
282     group "root"
283   end
284 else
285   directory "/opt/areca" do
286     action :delete
287     recursive true
288   end
289 end
290
291 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
292   if status_packages.include?(status_package)
293     package status_package
294
295     template "/etc/default/#{status_package}d" do
296       source "raid.default.erb"
297       owner "root"
298       group "root"
299       mode 0o644
300       variables :devices => status_packages[status_package]
301     end
302
303     service "#{status_package}d" do
304       action [:start, :enable]
305       supports :status => false, :restart => true, :reload => false
306       subscribes :restart, "template[/etc/default/#{status_package}d]"
307     end
308   else
309     package status_package do
310       action :purge
311     end
312
313     file "/etc/default/#{status_package}d" do
314       action :delete
315     end
316   end
317 end
318
319 disks = if node[:hardware][:disk]
320           node[:hardware][:disk][:disks]
321         else
322           []
323         end
324
325 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
326
327 nvmes = if node[:hardware][:pci]
328           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
329         else
330           []
331         end
332
333 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
334
335 if !intel_ssds.empty? || !intel_nvmes.empty?
336   package "unzip"
337
338   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip" do
339     source "https://downloadmirror.intel.com/27863/eng/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip"
340   end
341
342   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip" do
343     command "unzip Intel_SSD_Data_Center_Tool_3.0.13_Linux.zip isdct_3.0.13.400-17_amd64.deb"
344     cwd Chef::Config[:file_cache_path]
345     user "root"
346     group "root"
347     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_3.0.13.400-17_amd64.deb") }
348   end
349
350   dpkg_package "isdct" do
351     version "3.0.13.400-17"
352     source "#{Chef::Config[:file_cache_path]}/isdct_3.0.13.400-17_amd64.deb"
353   end
354 end
355
356 disks = disks.map do |disk|
357   next if disk[:state] == "spun_down"
358
359   if disk[:smart_device]
360     controller = node[:hardware][:disk][:controllers][disk[:controller]]
361     device = controller[:device].sub("/dev/", "")
362     smart = disk[:smart_device]
363
364     if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
365       array = node[:hardware][:disk][:arrays][disk[:arrays].first]
366       munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
367     elsif smart =~ /^.*,(\d+)$/
368       munin = "#{device}-#{Regexp.last_match(1)}"
369     elsif smart =~ %r{^.*,(\d+)/(\d+)$}
370       munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
371     end
372   elsif disk[:device]
373     device = disk[:device].sub("/dev/", "")
374     munin = device
375   end
376
377   next if device.nil?
378
379   Hash[
380     :device => device,
381     :smart => smart,
382     :munin => munin,
383     :hddtemp => munin.tr("-:", "_")
384   ]
385 end
386
387 smartd_service = if node[:lsb][:release].to_f >= 16.04
388                    "smartd"
389                  else
390                    "smartmontools"
391                  end
392
393 disks = disks.compact
394
395 if disks.count.positive?
396   package "smartmontools"
397
398   template "/usr/local/bin/smartd-mailer" do
399     source "smartd-mailer.erb"
400     owner "root"
401     group "root"
402     mode 0o755
403   end
404
405   template "/etc/smartd.conf" do
406     source "smartd.conf.erb"
407     owner "root"
408     group "root"
409     mode 0o644
410     variables :disks => disks
411   end
412
413   template "/etc/default/smartmontools" do
414     source "smartmontools.erb"
415     owner "root"
416     group "root"
417     mode 0o644
418   end
419
420   service smartd_service do
421     action [:enable, :start]
422     subscribes :reload, "template[/etc/smartd.conf]"
423     subscribes :restart, "template[/etc/default/smartmontools]"
424   end
425
426   # Don't try and do munin monitoring of disks behind
427   # an Areca controller as they only allow one thing to
428   # talk to the controller at a time and smartd will
429   # throw errors if it clashes with munin
430   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
431
432   disks.each do |disk|
433     munin_plugin "smart_#{disk[:munin]}" do
434       target "smart_"
435       conf "munin.smart.erb"
436       conf_variables :disk => disk
437     end
438   end
439 else
440   service smartd_service do
441     action [:stop, :disable]
442   end
443 end
444
445 if disks.count.positive?
446   munin_plugin "hddtemp_smartctl" do
447     conf "munin.hddtemp.erb"
448     conf_variables :disks => disks
449   end
450 else
451   munin_plugin "hddtemp_smartctl" do
452     action :delete
453     conf "munin.hddtemp.erb"
454   end
455 end
456
457 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
458           disks.map { |d| "smart_#{d[:munin]}" }
459
460 plugins.each do |plugin|
461   munin_plugin plugin do
462     action :delete
463   end
464 end
465
466 if File.exist?("/etc/mdadm/mdadm.conf")
467   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
468     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
469
470     line
471   end
472
473   file "/etc/mdadm/mdadm.conf" do
474     owner "root"
475     group "root"
476     mode 0o644
477     content mdadm_conf
478   end
479
480   service "mdadm" do
481     action :nothing
482     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
483   end
484 end
485
486 template "/etc/modules" do
487   source "modules.erb"
488   owner "root"
489   group "root"
490   mode 0o644
491 end
492
493 service "kmod" do
494   action :nothing
495   subscribes :start, "template[/etc/modules]"
496 end
497
498 if node[:hardware][:watchdog]
499   package "watchdog"
500
501   template "/etc/default/watchdog" do
502     source "watchdog.erb"
503     owner "root"
504     group "root"
505     mode 0o644
506     variables :module => node[:hardware][:watchdog]
507   end
508
509   service "watchdog" do
510     action [:enable, :start]
511   end
512 end
513
514 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
515   package "lm-sensors"
516
517   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
518     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
519     chip = format("coretemp-isa-%04d", cpu)
520
521     temps = if File.exist?("#{coretemp}/name")
522               Dir.glob("#{coretemp}/temp*_input").map do |temp|
523                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
524               end.sort
525             else
526               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
527                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
528               end.sort
529             end
530
531     if temps.first == 1
532       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
533       temps.shift
534     end
535
536     temps.each_with_index do |temp, index|
537       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
538     end
539   end
540
541   execute "/etc/sensors.d/chef.conf" do
542     action :nothing
543     command "/usr/bin/sensors -s"
544     user "root"
545     group "root"
546   end
547
548   template "/etc/sensors.d/chef.conf" do
549     source "sensors.conf.erb"
550     owner "root"
551     group "root"
552     mode 0o644
553     notifies :run, "execute[/etc/sensors.d/chef.conf]"
554   end
555 end