]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Renice background services
[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 %w[bus cache dimm iomca page socket-memory unknown].each do |trigger|
229   file "/etc/mcelog/#{trigger}-error-trigger.local" do
230     action :delete
231   end
232 end
233
234 service "mcelog" do
235   action [:stop, :disable]
236 end
237
238 package "mcelog" do
239   action :purge
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 if status_packages.include?("cciss-vol-status")
317   template "/usr/local/bin/cciss-vol-statusd" do
318     source "cciss-vol-statusd.erb"
319     owner "root"
320     group "root"
321     mode "755"
322     notifies :restart, "service[cciss-vol-statusd]"
323   end
324
325   systemd_service "cciss-vol-statusd" do
326     description "Check cciss_vol_status values in the background"
327     exec_start "/usr/local/bin/cciss-vol-statusd"
328     nice 10
329     private_tmp true
330     protect_system "full"
331     protect_home true
332     no_new_privileges true
333     notifies :restart, "service[cciss-vol-statusd]"
334   end
335 else
336   systemd_service "cciss-vol-statusd" do
337     action :delete
338   end
339
340   template "/usr/local/bin/cciss-vol-statusd" do
341     action :delete
342   end
343 end
344
345 %w[cciss-vol-status mpt-status sas2ircu-status megaclisas-status aacraid-status].each do |status_package|
346   if status_packages.include?(status_package)
347     package status_package
348
349     template "/etc/default/#{status_package}d" do
350       source "raid.default.erb"
351       owner "root"
352       group "root"
353       mode "644"
354       variables :devices => status_packages[status_package]
355     end
356
357     service "#{status_package}d" do
358       action [:start, :enable]
359       supports :status => false, :restart => true, :reload => false
360       subscribes :restart, "template[/etc/default/#{status_package}d]"
361     end
362   else
363     package status_package do
364       action :purge
365     end
366
367     file "/etc/default/#{status_package}d" do
368       action :delete
369     end
370   end
371 end
372
373 disks = if node[:hardware][:disk]
374           node[:hardware][:disk][:disks]
375         else
376           []
377         end
378
379 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
380
381 nvmes = if node[:hardware][:pci]
382           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
383         else
384           []
385         end
386
387 unless nvmes.empty?
388   package "nvme-cli"
389 end
390
391 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
392
393 if !intel_ssds.empty? || !intel_nvmes.empty?
394   package "unzip"
395
396   intel_mas_tool_version = "1.10"
397   intel_mas_package_version = "#{intel_mas_tool_version}.155-0"
398
399   remote_file "#{Chef::Config[:file_cache_path]}/Intel_MAS_CLI_Tool_#{intel_mas_tool_version}_Linux.zip" do
400     source "https://downloadmirror.intel.com/646992/Intel_MAS_CLI_Tool_Linux_#{intel_mas_tool_version}-v2.zip"
401   end
402
403   execute "#{Chef::Config[:file_cache_path]}/Intel_MAS_CLI_Tool_#{intel_mas_tool_version}_Linux.zip" do
404     command "unzip Intel_MAS_CLI_Tool_#{intel_mas_tool_version}_Linux.zip intelmas_#{intel_mas_package_version}_amd64.deb"
405     cwd Chef::Config[:file_cache_path]
406     user "root"
407     group "root"
408     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/intelmas_#{intel_mas_package_version}_amd64.deb") }
409   end
410
411   dpkg_package "intelmas" do
412     version "#{intel_mas_package_version}"
413     source "#{Chef::Config[:file_cache_path]}/intelmas_#{intel_mas_package_version}_amd64.deb"
414   end
415
416   dpkg_package "isdct" do
417     action :purge
418   end
419 end
420
421 disks = disks.map do |disk|
422   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
423
424   if disk[:smart_device]
425     controller = node[:hardware][:disk][:controllers][disk[:controller]]
426
427     if controller && controller[:device]
428       device = controller[:device].sub("/dev/", "")
429       smart = disk[:smart_device]
430
431       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
432         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
433         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
434       elsif smart =~ /^.*,(\d+)$/
435         munin = "#{device}-#{Regexp.last_match(1)}"
436       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
437         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
438       end
439     elsif disk[:device]
440       device = disk[:device].sub("/dev/", "")
441       smart = disk[:smart_device]
442
443       if smart =~ /^.*,(\d+),(\d+),(\d+)$/
444         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}:#{Regexp.last_match(3)}"
445       end
446     end
447   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
448     device = Regexp.last_match(1)
449     munin = device
450   elsif disk[:device]
451     device = disk[:device].sub("/dev/", "")
452     munin = device
453   end
454
455   next if device.nil?
456
457   Hash[
458     :device => device,
459     :smart => smart,
460     :munin => munin,
461     :hddtemp => munin.tr("-:", "_")
462   ]
463 end
464
465 disks = disks.compact.uniq
466
467 if disks.count.positive?
468   package "smartmontools"
469
470   template "/etc/cron.daily/update-smart-drivedb" do
471     source "update-smart-drivedb.erb"
472     owner "root"
473     group "root"
474     mode "755"
475   end
476
477   template "/usr/local/bin/smartd-mailer" do
478     source "smartd-mailer.erb"
479     owner "root"
480     group "root"
481     mode "755"
482   end
483
484   template "/etc/smartd.conf" do
485     source "smartd.conf.erb"
486     owner "root"
487     group "root"
488     mode "644"
489     variables :disks => disks
490   end
491
492   template "/etc/default/smartmontools" do
493     source "smartmontools.erb"
494     owner "root"
495     group "root"
496     mode "644"
497   end
498
499   service "smartmontools" do
500     action [:enable, :start]
501     subscribes :reload, "template[/etc/smartd.conf]"
502     subscribes :restart, "template[/etc/default/smartmontools]"
503   end
504
505   template "/etc/prometheus/collectors/smart.devices" do
506     source "smart.devices.erb"
507     owner "root"
508     group "root"
509     mode "644"
510     variables :disks => disks
511   end
512
513   prometheus_collector "smart" do
514     interval "15m"
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 "mdadm" 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 end