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