]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
bd525babff6d0e67e80a1ca88ffd5866e188af4a
[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   if product.end_with?("Gen8", "Gen9")
69     package "hp-ams" do
70       action :install
71       notifies :restart, "service[hp-ams]"
72     end
73
74     service "hp-ams" do
75       action [:enable, :start]
76       supports :status => true, :restart => true
77     end
78   end
79
80   units << "1"
81 when "TYAN"
82   units << "0"
83 when "TYAN Computer Corporation"
84   units << "0"
85 when "Supermicro"
86   case product
87   when "H8DGU", "X9SCD", "X7DBU", "X7DW3", "X9DR7/E-(J)LN4F", "X9DR3-F", "X9DRW", "SYS-1028U-TN10RT+", "SYS-2028U-TN24R4T+", "SYS-1029P-WTRT", "Super Server"
88     units << "1"
89   else
90     units << "0"
91   end
92 when "IBM"
93   units << "0"
94 end
95
96 units.sort.uniq.each do |unit|
97   service "serial-getty@ttyS#{unit}" do
98     action [:enable, :start]
99   end
100 end
101
102 # if we need a different / special kernel version to make the hardware
103 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
104 # ensure that we have the package installed. the grub template will
105 # make sure that this is the default on boot.
106 if node[:hardware][:grub][:kernel]
107   kernel_version = node[:hardware][:grub][:kernel]
108
109   package "linux-image-#{kernel_version}-generic"
110   package "linux-image-extra-#{kernel_version}-generic"
111   package "linux-headers-#{kernel_version}-generic"
112   package "linux-tools-#{kernel_version}-generic"
113
114   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
115   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
116   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
117 else
118   grub_entry = "0"
119 end
120
121 if File.exist?("/etc/default/grub")
122   execute "update-grub" do
123     action :nothing
124     command "/usr/sbin/update-grub"
125   end
126
127   template "/etc/default/grub" do
128     source "grub.erb"
129     owner "root"
130     group "root"
131     mode 0o644
132     variables :units => units, :entry => grub_entry
133     notifies :run, "execute[update-grub]"
134   end
135 end
136
137 execute "update-initramfs" do
138   action :nothing
139   command "update-initramfs -u -k all"
140   user "root"
141   group "root"
142 end
143
144 template "/etc/initramfs-tools/conf.d/mdadm" do
145   source "initramfs-mdadm.erb"
146   owner "root"
147   group "root"
148   mode 0o644
149   notifies :run, "execute[update-initramfs]"
150 end
151
152 package "haveged"
153 service "haveged" do
154   action [:enable, :start]
155 end
156
157 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
158
159 package "irqbalance"
160
161 template "/etc/default/irqbalance" do
162   source "irqbalance.erb"
163   owner "root"
164   group "root"
165   mode 0o644
166 end
167
168 service "irqbalance" do
169   action [:start, :enable]
170   supports :status => false, :restart => true, :reload => false
171   subscribes :restart, "template[/etc/default/irqbalance]"
172 end
173
174 # Link Layer Discovery Protocol Daemon
175 package "lldpd"
176 service "lldpd" do
177   action [:start, :enable]
178   supports :status => true, :restart => true, :reload => true
179 end
180
181 tools_packages = []
182 status_packages = {}
183
184 node[:kernel][:modules].each_key do |modname|
185   case modname
186   when "cciss"
187     tools_packages << "ssacli"
188     status_packages["cciss-vol-status"] ||= []
189   when "hpsa"
190     tools_packages << "ssacli"
191     status_packages["cciss-vol-status"] ||= []
192   when "mptsas"
193     tools_packages << "lsiutil"
194     status_packages["mpt-status"] ||= []
195   when "mpt2sas", "mpt3sas"
196     tools_packages << "sas2ircu"
197     status_packages["sas2ircu-status"] ||= []
198   when "megaraid_mm"
199     tools_packages << "megactl"
200     status_packages["megaraid-status"] ||= []
201   when "megaraid_sas"
202     tools_packages << "megacli"
203     status_packages["megaclisas-status"] ||= []
204   when "aacraid"
205     tools_packages << "arcconf"
206     status_packages["aacraid-status"] ||= []
207   when "arcmsr"
208     tools_packages << "areca"
209   end
210 end
211
212 node[:block_device].each do |name, attributes|
213   next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
214
215   if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
216     status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
217   else
218     Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
219       status_packages["cciss-vol-status"] |= [File.basename(sg)]
220     end
221   end
222 end
223
224 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
225   if tools_packages.include?(tools_package)
226     package tools_package
227   else
228     package tools_package do
229       action :purge
230     end
231   end
232 end
233
234 if tools_packages.include?("areca")
235   include_recipe "git"
236
237   git "/opt/areca" do
238     action :sync
239     repository "https://git.openstreetmap.org/private/areca.git"
240     user "root"
241     group "root"
242   end
243 else
244   directory "/opt/areca" do
245     action :delete
246     recursive true
247   end
248 end
249
250 if status_packages.include?("cciss-vol-status")
251   template "/usr/local/bin/cciss-vol-statusd" do
252     source "cciss-vol-statusd.erb"
253     owner "root"
254     group "root"
255     mode 0o755
256     notifies :restart, "service[cciss-vol-statusd]"
257   end
258
259   systemd_service "cciss-vol-statusd" do
260     description "Check cciss_vol_status values in the background"
261     exec_start "/usr/local/bin/cciss-vol-statusd"
262     private_tmp true
263     protect_system "full"
264     protect_home true
265     no_new_privileges true
266     notifies :restart, "service[cciss-vol-statusd]"
267   end
268 end
269
270 ["cciss-vol-status", "mpt-status", "sas2ircu-status", "megaraid-status", "megaclisas-status", "aacraid-status"].each do |status_package|
271   if status_packages.include?(status_package)
272     package status_package
273
274     template "/etc/default/#{status_package}d" do
275       source "raid.default.erb"
276       owner "root"
277       group "root"
278       mode 0o644
279       variables :devices => status_packages[status_package]
280     end
281
282     service "#{status_package}d" do
283       action [:start, :enable]
284       supports :status => false, :restart => true, :reload => false
285       subscribes :restart, "template[/etc/default/#{status_package}d]"
286     end
287   else
288     package status_package do
289       action :purge
290     end
291
292     file "/etc/default/#{status_package}d" do
293       action :delete
294     end
295   end
296 end
297
298 disks = if node[:hardware][:disk]
299           node[:hardware][:disk][:disks]
300         else
301           []
302         end
303
304 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
305
306 nvmes = if node[:hardware][:pci]
307           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
308         else
309           []
310         end
311
312 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
313
314 if !intel_ssds.empty? || !intel_nvmes.empty?
315   package "unzip"
316
317   intel_ssd_tool_version = "3.0.19"
318
319   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
320     source "https://downloadmirror.intel.com/28639/eng/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip"
321   end
322
323   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
324     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
325     cwd Chef::Config[:file_cache_path]
326     user "root"
327     group "root"
328     not_if { File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
329   end
330
331   dpkg_package "isdct" do
332     version "#{intel_ssd_tool_version}-1"
333     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
334   end
335 end
336
337 disks = disks.map do |disk|
338   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
339
340   if disk[:smart_device]
341     controller = node[:hardware][:disk][:controllers][disk[:controller]]
342
343     if controller && controller[:device]
344       device = controller[:device].sub("/dev/", "")
345       smart = disk[:smart_device]
346
347       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
348         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
349         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
350       elsif smart =~ /^.*,(\d+)$/
351         munin = "#{device}-#{Regexp.last_match(1)}"
352       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
353         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
354       end
355     end
356   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
357     device = Regexp.last_match(1)
358     munin = device
359   elsif disk[:device]
360     device = disk[:device].sub("/dev/", "")
361     munin = device
362   end
363
364   next if device.nil?
365
366   Hash[
367     :device => device,
368     :smart => smart,
369     :munin => munin,
370     :hddtemp => munin.tr("-:", "_")
371   ]
372 end
373
374 disks = disks.compact.uniq
375
376 if disks.count.positive?
377   package "smartmontools"
378
379   template "/usr/local/bin/smartd-mailer" do
380     source "smartd-mailer.erb"
381     owner "root"
382     group "root"
383     mode 0o755
384   end
385
386   template "/etc/smartd.conf" do
387     source "smartd.conf.erb"
388     owner "root"
389     group "root"
390     mode 0o644
391     variables :disks => disks
392   end
393
394   template "/etc/default/smartmontools" do
395     source "smartmontools.erb"
396     owner "root"
397     group "root"
398     mode 0o644
399   end
400
401   service "smartd" do
402     action [:enable, :start]
403     subscribes :reload, "template[/etc/smartd.conf]"
404     subscribes :restart, "template[/etc/default/smartmontools]"
405   end
406
407   # Don't try and do munin monitoring of disks behind
408   # an Areca controller as they only allow one thing to
409   # talk to the controller at a time and smartd will
410   # throw errors if it clashes with munin
411   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
412
413   disks.each do |disk|
414     munin_plugin "smart_#{disk[:munin]}" do
415       target "smart_"
416       conf "munin.smart.erb"
417       conf_variables :disk => disk
418     end
419   end
420 else
421   service "smartd" do
422     action [:stop, :disable]
423   end
424 end
425
426 if disks.count.positive?
427   munin_plugin "hddtemp_smartctl" do
428     conf "munin.hddtemp.erb"
429     conf_variables :disks => disks
430   end
431 else
432   munin_plugin "hddtemp_smartctl" do
433     action :delete
434     conf "munin.hddtemp.erb"
435   end
436 end
437
438 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
439           disks.map { |d| "smart_#{d[:munin]}" }
440
441 plugins.each do |plugin|
442   munin_plugin plugin do
443     action :delete
444     conf "munin.smart.erb"
445   end
446 end
447
448 if File.exist?("/etc/mdadm/mdadm.conf")
449   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
450     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
451
452     line
453   end
454
455   file "/etc/mdadm/mdadm.conf" do
456     owner "root"
457     group "root"
458     mode 0o644
459     content mdadm_conf
460   end
461
462   service "mdadm" do
463     action :nothing
464     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
465   end
466 end
467
468 template "/etc/modules" do
469   source "modules.erb"
470   owner "root"
471   group "root"
472   mode 0o644
473 end
474
475 service "kmod" do
476   action :nothing
477   subscribes :start, "template[/etc/modules]"
478 end
479
480 if node[:hardware][:watchdog]
481   package "watchdog"
482
483   template "/etc/default/watchdog" do
484     source "watchdog.erb"
485     owner "root"
486     group "root"
487     mode 0o644
488     variables :module => node[:hardware][:watchdog]
489   end
490
491   service "watchdog" do
492     action [:enable, :start]
493   end
494 end
495
496 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
497   package "lm-sensors"
498
499   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
500     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
501     chip = format("coretemp-isa-%04d", cpu)
502
503     temps = if File.exist?("#{coretemp}/name")
504               Dir.glob("#{coretemp}/temp*_input").map do |temp|
505                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
506               end.sort
507             else
508               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
509                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
510               end.sort
511             end
512
513     if temps.first == 1
514       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
515       temps.shift
516     end
517
518     temps.each_with_index do |temp, index|
519       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
520     end
521   end
522
523   execute "/etc/sensors.d/chef.conf" do
524     action :nothing
525     command "/usr/bin/sensors -s"
526     user "root"
527     group "root"
528   end
529
530   template "/etc/sensors.d/chef.conf" do
531     source "sensors.conf.erb"
532     owner "root"
533     group "root"
534     mode 0o644
535     notifies :run, "execute[/etc/sensors.d/chef.conf]"
536   end
537 end