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