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