]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
119176cd6327597a9e6a2aca7e43bae9cde737f2
[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 %w[cciss-vol-status mpt-status sas2ircu-status megaclisas-status aacraid-status].each do |status_package|
343   if status_packages.include?(status_package)
344     package status_package
345
346     service "#{status_package}d" do
347       action [:stop, :disable]
348     end
349
350     file "/etc/default/#{status_package}d" do
351       action :delete
352     end
353   else
354     package status_package do
355       action :purge
356     end
357   end
358 end
359
360 systemd_service "cciss-vol-statusd" do
361   action :delete
362 end
363
364 template "/usr/local/bin/cciss-vol-statusd" do
365   action :delete
366 end
367
368 disks = if node[:hardware][:disk]
369           node[:hardware][:disk][:disks]
370         else
371           []
372         end
373
374 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
375
376 nvmes = if node[:hardware][:pci]
377           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
378         else
379           []
380         end
381
382 unless nvmes.empty?
383   package "nvme-cli"
384 end
385
386 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
387
388 if !intel_ssds.empty? || !intel_nvmes.empty?
389   package "unzip"
390
391   sst_tool_version = "1.3"
392   sst_package_version = "#{sst_tool_version}.208-0"
393
394   # remote_file "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
395   #   source "https://downloadmirror.intel.com/743764/SST_CLI_Linux_#{sst_tool_version}.zip"
396   # end
397
398   execute "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
399     command "unzip SST_CLI_Linux_#{sst_tool_version}.zip sst_#{sst_package_version}_amd64.deb"
400     cwd Chef::Config[:file_cache_path]
401     user "root"
402     group "root"
403     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb") }
404   end
405
406   dpkg_package "sst" do
407     version "#{sst_package_version}"
408     source "#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb"
409   end
410
411   dpkg_package "intelmas" do
412     action :purge
413   end
414 end
415
416 disks = disks.map do |disk|
417   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
418
419   if disk[:smart_device]
420     controller = node[:hardware][:disk][:controllers][disk[:controller]]
421
422     if controller && controller[:device]
423       device = controller[:device].sub("/dev/", "")
424       smart = disk[:smart_device]
425
426       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
427         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
428         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
429       elsif smart =~ /^.*,(\d+)$/
430         munin = "#{device}-#{Regexp.last_match(1)}"
431       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
432         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
433       end
434     elsif disk[:device]
435       device = disk[:device].sub("/dev/", "")
436       smart = disk[:smart_device]
437
438       if smart =~ /^.*,(\d+),(\d+),(\d+)$/
439         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}:#{Regexp.last_match(3)}"
440       end
441     end
442   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
443     device = Regexp.last_match(1)
444     munin = device
445   elsif disk[:device]
446     device = disk[:device].sub("/dev/", "")
447     munin = device
448   end
449
450   next if device.nil? || munin.nil?
451
452   Hash[
453     :device => device,
454     :smart => smart,
455     :munin => munin,
456     :hddtemp => munin.tr("-:", "_")
457   ]
458 end
459
460 disks = disks.compact.uniq
461
462 if disks.count.positive?
463   package "smartmontools"
464
465   template "/etc/cron.daily/update-smart-drivedb" do
466     source "update-smart-drivedb.erb"
467     owner "root"
468     group "root"
469     mode "755"
470   end
471
472   template "/usr/local/bin/smartd-mailer" do
473     source "smartd-mailer.erb"
474     owner "root"
475     group "root"
476     mode "755"
477   end
478
479   template "/etc/smartd.conf" do
480     source "smartd.conf.erb"
481     owner "root"
482     group "root"
483     mode "644"
484     variables :disks => disks
485   end
486
487   template "/etc/default/smartmontools" do
488     source "smartmontools.erb"
489     owner "root"
490     group "root"
491     mode "644"
492   end
493
494   service "smartmontools" do
495     action [:enable, :start]
496     subscribes :reload, "template[/etc/smartd.conf]"
497     subscribes :restart, "template[/etc/default/smartmontools]"
498   end
499
500   template "/etc/prometheus/collectors/smart.devices" do
501     source "smart.devices.erb"
502     owner "root"
503     group "root"
504     mode "644"
505     variables :disks => disks
506   end
507
508   prometheus_collector "smart" do
509     interval "15m"
510     user "root"
511     capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
512     private_devices false
513     private_users false
514     protect_clock false
515   end
516
517   # Don't try and do munin monitoring of disks behind
518   # an Areca controller as they only allow one thing to
519   # talk to the controller at a time and smartd will
520   # throw errors if it clashes with munin
521   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
522
523   disks.each do |disk|
524     munin_plugin "smart_#{disk[:munin]}" do
525       target "smart_"
526       conf "munin.smart.erb"
527       conf_variables :disk => disk
528     end
529   end
530 else
531   service "smartd" do
532     action [:stop, :disable]
533   end
534 end
535
536 if disks.count.positive?
537   munin_plugin "hddtemp_smartctl" do
538     conf "munin.hddtemp.erb"
539     conf_variables :disks => disks
540   end
541 else
542   munin_plugin "hddtemp_smartctl" do
543     action :delete
544     conf "munin.hddtemp.erb"
545   end
546 end
547
548 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
549           disks.map { |d| "smart_#{d[:munin]}" }
550
551 plugins.each do |plugin|
552   munin_plugin plugin do
553     action :delete
554     conf "munin.smart.erb"
555   end
556 end
557
558 if File.exist?("/etc/mdadm/mdadm.conf")
559   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
560     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
561
562     line
563   end
564
565   file "/etc/mdadm/mdadm.conf" do
566     owner "root"
567     group "root"
568     mode "644"
569     content mdadm_conf
570   end
571
572   service "mdmonitor" do
573     action :nothing
574     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
575   end
576 end
577
578 file "/etc/modules" do
579   action :delete
580 end
581
582 node[:hardware][:modules].each do |module_name|
583   kernel_module module_name do
584     action :install
585     not_if { kitchen? }
586   end
587 end
588
589 node[:hardware][:blacklisted_modules].each do |module_name|
590   kernel_module module_name do
591     action :blacklist
592   end
593 end
594
595 if node[:hardware][:watchdog]
596   package "watchdog"
597
598   template "/etc/default/watchdog" do
599     source "watchdog.erb"
600     owner "root"
601     group "root"
602     mode "644"
603     variables :module => node[:hardware][:watchdog]
604   end
605
606   service "watchdog" do
607     action [:enable, :start]
608   end
609 end
610
611 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
612   package "lm-sensors"
613
614   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
615     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
616     chip = format("coretemp-isa-%04d", cpu)
617
618     temps = if File.exist?("#{coretemp}/name")
619               Dir.glob("#{coretemp}/temp*_input").map do |temp|
620                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
621               end.sort
622             else
623               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
624                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
625               end.sort
626             end
627
628     if temps.first == 1
629       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
630       temps.shift
631     end
632
633     temps.each_with_index do |temp, index|
634       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
635     end
636   end
637
638   execute "/etc/sensors.d/chef.conf" do
639     action :nothing
640     command "/usr/bin/sensors -s"
641     user "root"
642     group "root"
643   end
644
645   template "/etc/sensors.d/chef.conf" do
646     source "sensors.conf.erb"
647     owner "root"
648     group "root"
649     mode "644"
650     notifies :run, "execute[/etc/sensors.d/chef.conf]"
651   end
652 end
653
654 if node[:hardware][:shm_size]
655   execute "remount-dev-shm" do
656     action :nothing
657     command "/bin/mount -o remount /dev/shm"
658     user "root"
659     group "root"
660   end
661
662   mount "/dev/shm" do
663     action :enable
664     device "tmpfs"
665     fstype "tmpfs"
666     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
667     notifies :run, "execute[remount-dev-shm]"
668   end
669 end
670
671 prometheus_collector "ohai" do
672   interval "15m"
673   user "root"
674   proc_subset "all"
675   capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
676   private_devices false
677   private_users false
678   protect_clock false
679   protect_kernel_modules false
680 end