]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Revert "Avoid installing sas2ircu-status on Ubuntu 22.04"
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook:: 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 "apt"
21 include_recipe "git"
22 include_recipe "munin"
23 include_recipe "prometheus"
24 include_recipe "sysfs"
25 include_recipe "tools"
26
27 ohai_plugin "hardware" do
28   template "ohai.rb.erb"
29 end
30
31 case node[:cpu][:"0"][:vendor_id]
32 when "GenuineIntel"
33   package "intel-microcode"
34 when "AuthenticAMD"
35   package "amd64-microcode"
36 end
37
38 if node[:dmi] && node[:dmi][:system]
39   case node[:dmi][:system][:manufacturer]
40   when "empty"
41     manufacturer = node[:dmi][:base_board][:manufacturer]
42     product = node[:dmi][:base_board][:product_name]
43   else
44     manufacturer = node[:dmi][:system][:manufacturer]
45     product = node[:dmi][:system][:product_name]
46   end
47 else
48   manufacturer = "Unknown"
49   product = "Unknown"
50 end
51
52 units = []
53
54 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric") || node[:roles].include?("prgmr")
55   units << "0"
56 end
57
58 case manufacturer
59 when "HP"
60   package "hponcfg"
61
62   package "hp-health" do
63     action :install
64     notifies :restart, "service[hp-health]"
65     only_if { node[:lsb][:release].to_f < 22.04 }
66   end
67
68   service "hp-health" do
69     action [:enable, :start]
70     supports :status => true, :restart => true
71     only_if { node[:lsb][:release].to_f < 22.04 }
72   end
73
74   if product.end_with?("Gen8", "Gen9")
75     package "hp-ams" do
76       action :install
77       notifies :restart, "service[hp-ams]"
78     end
79
80     service "hp-ams" do
81       action [:enable, :start]
82       supports :status => true, :restart => true
83     end
84   end
85
86   units << "1"
87 when "TYAN"
88   units << "0"
89 when "TYAN Computer Corporation"
90   units << "0"
91 when "Supermicro"
92   units << "1"
93 when "IBM"
94   units << "0"
95 when "VMware, Inc."
96   package "open-vm-tools"
97
98   # Remove timeSync plugin completely
99   # https://github.com/vmware/open-vm-tools/issues/302
100   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
101     action :delete
102     notifies :restart, "service[open-vm-tools]"
103   end
104
105   # Attempt to tell Host we are not interested in timeSync
106   execute "vmware-toolbox-cmd-timesync-disable" do
107     command "/usr/bin/vmware-toolbox-cmd timesync disable"
108     ignore_failure true
109   end
110
111   service "open-vm-tools" do
112     action [:enable, :start]
113     supports :status => true, :restart => true
114   end
115 end
116
117 units.sort.uniq.each do |unit|
118   service "serial-getty@ttyS#{unit}" do
119     action [:enable, :start]
120   end
121 end
122
123 # if we need a different / special kernel version to make the hardware
124 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
125 # ensure that we have the package installed. the grub template will
126 # make sure that this is the default on boot.
127 if node[:hardware][:grub][:kernel]
128   kernel_version = node[:hardware][:grub][:kernel]
129
130   package "linux-image-#{kernel_version}-generic"
131   package "linux-image-extra-#{kernel_version}-generic"
132   package "linux-headers-#{kernel_version}-generic"
133   package "linux-tools-#{kernel_version}-generic"
134
135   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
136   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
137   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
138 else
139   grub_entry = "0"
140 end
141
142 if File.exist?("/etc/default/grub")
143   execute "update-grub" do
144     action :nothing
145     command "/usr/sbin/update-grub"
146     not_if { kitchen? }
147   end
148
149   template "/etc/default/grub" do
150     source "grub.erb"
151     owner "root"
152     group "root"
153     mode "644"
154     variables :units => units, :entry => grub_entry
155     notifies :run, "execute[update-grub]"
156   end
157 end
158
159 execute "update-initramfs" do
160   action :nothing
161   command "update-initramfs -u -k all"
162   user "root"
163   group "root"
164 end
165
166 template "/etc/initramfs-tools/conf.d/mdadm" do
167   source "initramfs-mdadm.erb"
168   owner "root"
169   group "root"
170   mode "644"
171   notifies :run, "execute[update-initramfs]"
172 end
173
174 package "haveged"
175 service "haveged" do
176   action [:enable, :start]
177 end
178
179 if node[:kernel][:modules].include?("ipmi_si")
180   package "ipmitool"
181   package "freeipmi-tools"
182
183   template "/etc/prometheus/ipmi_local.yml" do
184     source "ipmi_local.yml.erb"
185     owner "root"
186     group "root"
187     mode "644"
188   end
189
190   prometheus_exporter "ipmi" do
191     port 9290
192     options "--config.file=/etc/prometheus/ipmi_local.yml"
193     subscribes :restart, "template[/etc/prometheus/ipmi_local.yml]"
194   end
195 end
196
197 package "irqbalance"
198
199 service "irqbalance" do
200   action [:start, :enable]
201   supports :status => false, :restart => true, :reload => false
202 end
203
204 package "lldpd"
205
206 service "lldpd" do
207   action [:start, :enable]
208   supports :status => true, :restart => true, :reload => true
209 end
210
211 ohai_plugin "lldp" do
212   template "lldp.rb.erb"
213 end
214
215 tools_packages = []
216 status_packages = {}
217
218 if node[:virtualization][:role] != "guest" ||
219    (node[:virtualization][:system] != "lxc" &&
220     node[:virtualization][:system] != "lxd" &&
221     node[:virtualization][:system] != "openvz")
222
223   node[:kernel][:modules].each_key do |modname|
224     case modname
225     when "cciss"
226       tools_packages << "ssacli"
227       status_packages["cciss-vol-status"] ||= []
228     when "hpsa"
229       tools_packages << "ssacli"
230       status_packages["cciss-vol-status"] ||= []
231     when "mptsas"
232       tools_packages << "lsiutil"
233       status_packages["mpt-status"] ||= []
234     when "mpt2sas", "mpt3sas"
235       tools_packages << "sas2ircu"
236       status_packages["sas2ircu-status"] ||= []
237     when "megaraid_mm"
238       tools_packages << "megactl"
239       status_packages["megaraid-status"] ||= []
240     when "megaraid_sas"
241       tools_packages << "megacli"
242       status_packages["megaclisas-status"] ||= []
243     when "aacraid"
244       tools_packages << "arcconf"
245       status_packages["aacraid-status"] ||= []
246     when "arcmsr"
247       tools_packages << "areca"
248     end
249   end
250
251   node[:block_device].each do |name, attributes|
252     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
253
254     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
255       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
256     else
257       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
258         status_packages["cciss-vol-status"] |= [File.basename(sg)]
259       end
260     end
261   end
262 end
263
264 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
265   if tools_packages.include?(tools_package)
266     package tools_package
267   else
268     package tools_package do
269       action :purge
270     end
271   end
272 end
273
274 if tools_packages.include?("areca")
275   include_recipe "git"
276
277   git "/opt/areca" do
278     action :sync
279     repository "https://git.openstreetmap.org/private/areca.git"
280     depth 1
281     user "root"
282     group "root"
283     not_if { kitchen? }
284   end
285 else
286   directory "/opt/areca" do
287     action :delete
288     recursive true
289   end
290 end
291
292 if status_packages.include?("cciss-vol-status")
293   template "/usr/local/bin/cciss-vol-statusd" do
294     source "cciss-vol-statusd.erb"
295     owner "root"
296     group "root"
297     mode "755"
298     notifies :restart, "service[cciss-vol-statusd]"
299   end
300
301   systemd_service "cciss-vol-statusd" do
302     description "Check cciss_vol_status values in the background"
303     exec_start "/usr/local/bin/cciss-vol-statusd"
304     private_tmp true
305     protect_system "full"
306     protect_home true
307     no_new_privileges true
308     notifies :restart, "service[cciss-vol-statusd]"
309   end
310 else
311   systemd_service "cciss-vol-statusd" do
312     action :delete
313   end
314
315   template "/usr/local/bin/cciss-vol-statusd" do
316     action :delete
317   end
318 end
319
320 %w[cciss-vol-status mpt-status sas2ircu-status megaraid-status megaclisas-status aacraid-status].each do |status_package|
321   if status_packages.include?(status_package)
322     package status_package
323
324     template "/etc/default/#{status_package}d" do
325       source "raid.default.erb"
326       owner "root"
327       group "root"
328       mode "644"
329       variables :devices => status_packages[status_package]
330     end
331
332     service "#{status_package}d" do
333       action [:start, :enable]
334       supports :status => false, :restart => true, :reload => false
335       subscribes :restart, "template[/etc/default/#{status_package}d]"
336     end
337   else
338     package status_package do
339       action :purge
340     end
341
342     file "/etc/default/#{status_package}d" do
343       action :delete
344     end
345   end
346 end
347
348 disks = if node[:hardware][:disk]
349           node[:hardware][:disk][:disks]
350         else
351           []
352         end
353
354 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
355
356 nvmes = if node[:hardware][:pci]
357           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
358         else
359           []
360         end
361
362 unless nvmes.empty?
363   package "nvme-cli"
364 end
365
366 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
367
368 if !intel_ssds.empty? || !intel_nvmes.empty?
369   package "unzip"
370
371   intel_mas_tool_version = "1.10"
372   intel_mas_package_version = "#{intel_mas_tool_version}.155-0"
373
374   remote_file "#{Chef::Config[:file_cache_path]}/Intel_MAS_CLI_Tool_#{intel_mas_tool_version}_Linux.zip" do
375     source "https://downloadmirror.intel.com/646992/Intel_MAS_CLI_Tool_Linux_#{intel_mas_tool_version}-v2.zip"
376   end
377
378   execute "#{Chef::Config[:file_cache_path]}/Intel_MAS_CLI_Tool_#{intel_mas_tool_version}_Linux.zip" do
379     command "unzip Intel_MAS_CLI_Tool_#{intel_mas_tool_version}_Linux.zip intelmas_#{intel_mas_package_version}_amd64.deb"
380     cwd Chef::Config[:file_cache_path]
381     user "root"
382     group "root"
383     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/intelmas_#{intel_mas_package_version}_amd64.deb") }
384   end
385
386   dpkg_package "intelmas" do
387     version "#{intel_mas_package_version}"
388     source "#{Chef::Config[:file_cache_path]}/intelmas_#{intel_mas_package_version}_amd64.deb"
389   end
390
391   dpkg_package "isdct" do
392     action :purge
393   end
394 end
395
396 disks = disks.map do |disk|
397   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
398
399   if disk[:smart_device]
400     controller = node[:hardware][:disk][:controllers][disk[:controller]]
401
402     if controller && controller[:device]
403       device = controller[:device].sub("/dev/", "")
404       smart = disk[:smart_device]
405
406       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
407         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
408         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
409       elsif smart =~ /^.*,(\d+)$/
410         munin = "#{device}-#{Regexp.last_match(1)}"
411       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
412         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
413       end
414     elsif disk[:device]
415       device = disk[:device].sub("/dev/", "")
416       smart = disk[:smart_device]
417
418       if smart =~ /^.*,(\d+),(\d+),(\d+)$/
419         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}:#{Regexp.last_match(3)}"
420       end
421     end
422   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
423     device = Regexp.last_match(1)
424     munin = device
425   elsif disk[:device]
426     device = disk[:device].sub("/dev/", "")
427     munin = device
428   end
429
430   next if device.nil?
431
432   Hash[
433     :device => device,
434     :smart => smart,
435     :munin => munin,
436     :hddtemp => munin.tr("-:", "_")
437   ]
438 end
439
440 disks = disks.compact.uniq
441
442 if disks.count.positive?
443   package "smartmontools"
444
445   template "/etc/cron.daily/update-smart-drivedb" do
446     source "update-smart-drivedb.erb"
447     owner "root"
448     group "root"
449     mode "755"
450   end
451
452   template "/usr/local/bin/smartd-mailer" do
453     source "smartd-mailer.erb"
454     owner "root"
455     group "root"
456     mode "755"
457   end
458
459   template "/etc/smartd.conf" do
460     source "smartd.conf.erb"
461     owner "root"
462     group "root"
463     mode "644"
464     variables :disks => disks
465   end
466
467   template "/etc/default/smartmontools" do
468     source "smartmontools.erb"
469     owner "root"
470     group "root"
471     mode "644"
472   end
473
474   service "smartmontools" do
475     action [:enable, :start]
476     subscribes :reload, "template[/etc/smartd.conf]"
477     subscribes :restart, "template[/etc/default/smartmontools]"
478   end
479
480   template "/etc/prometheus/collectors/smart.devices" do
481     source "smart.devices.erb"
482     owner "root"
483     group "root"
484     mode "644"
485     variables :disks => disks
486   end
487
488   prometheus_collector "smart" do
489     interval "15m"
490   end
491
492   # Don't try and do munin monitoring of disks behind
493   # an Areca controller as they only allow one thing to
494   # talk to the controller at a time and smartd will
495   # throw errors if it clashes with munin
496   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
497
498   disks.each do |disk|
499     munin_plugin "smart_#{disk[:munin]}" do
500       target "smart_"
501       conf "munin.smart.erb"
502       conf_variables :disk => disk
503     end
504   end
505 else
506   service "smartd" do
507     action [:stop, :disable]
508   end
509 end
510
511 if disks.count.positive?
512   munin_plugin "hddtemp_smartctl" do
513     conf "munin.hddtemp.erb"
514     conf_variables :disks => disks
515   end
516 else
517   munin_plugin "hddtemp_smartctl" do
518     action :delete
519     conf "munin.hddtemp.erb"
520   end
521 end
522
523 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
524           disks.map { |d| "smart_#{d[:munin]}" }
525
526 plugins.each do |plugin|
527   munin_plugin plugin do
528     action :delete
529     conf "munin.smart.erb"
530   end
531 end
532
533 if File.exist?("/etc/mdadm/mdadm.conf")
534   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
535     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
536
537     line
538   end
539
540   file "/etc/mdadm/mdadm.conf" do
541     owner "root"
542     group "root"
543     mode "644"
544     content mdadm_conf
545   end
546
547   service "mdadm" do
548     action :nothing
549     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
550   end
551 end
552
553 file "/etc/modules" do
554   action :delete
555 end
556
557 node[:hardware][:modules].each do |module_name|
558   kernel_module module_name do
559     action :install
560     not_if { kitchen? }
561   end
562 end
563
564 node[:hardware][:blacklisted_modules].each do |module_name|
565   kernel_module module_name do
566     action :blacklist
567   end
568 end
569
570 if node[:hardware][:watchdog]
571   package "watchdog"
572
573   template "/etc/default/watchdog" do
574     source "watchdog.erb"
575     owner "root"
576     group "root"
577     mode "644"
578     variables :module => node[:hardware][:watchdog]
579   end
580
581   service "watchdog" do
582     action [:enable, :start]
583   end
584 end
585
586 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
587   package "lm-sensors"
588
589   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
590     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
591     chip = format("coretemp-isa-%04d", cpu)
592
593     temps = if File.exist?("#{coretemp}/name")
594               Dir.glob("#{coretemp}/temp*_input").map do |temp|
595                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
596               end.sort
597             else
598               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
599                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
600               end.sort
601             end
602
603     if temps.first == 1
604       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
605       temps.shift
606     end
607
608     temps.each_with_index do |temp, index|
609       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
610     end
611   end
612
613   execute "/etc/sensors.d/chef.conf" do
614     action :nothing
615     command "/usr/bin/sensors -s"
616     user "root"
617     group "root"
618   end
619
620   template "/etc/sensors.d/chef.conf" do
621     source "sensors.conf.erb"
622     owner "root"
623     group "root"
624     mode "644"
625     notifies :run, "execute[/etc/sensors.d/chef.conf]"
626   end
627 end
628
629 if node[:hardware][:shm_size]
630   execute "remount-dev-shm" do
631     action :nothing
632     command "/bin/mount -o remount /dev/shm"
633     user "root"
634     group "root"
635   end
636
637   mount "/dev/shm" do
638     action :enable
639     device "tmpfs"
640     fstype "tmpfs"
641     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
642     notifies :run, "execute[remount-dev-shm]"
643   end
644 end