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